Skip to content

Commit 80810b5

Browse files
committed
Fix ensureLocalDeviceId migration
1 parent 3816487 commit 80810b5

File tree

1 file changed

+5
-30
lines changed

1 file changed

+5
-30
lines changed

SignalServiceKit/Storage/Database/GRDBSchemaMigrator.swift

Lines changed: 5 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -4824,38 +4824,13 @@ public class GRDBSchemaMigrator {
48244824
}
48254825

48264826
migrator.registerMigration(.dataMigration_ensureLocalDeviceId) { tx in
4827-
let localAciSql = """
4828-
SELECT VALUE FROM keyvalue
4829-
WHERE collection = 'TSStorageUserAccountCollection'
4830-
AND KEY = 'TSStorageRegisteredUUIDKey'
4831-
"""
4832-
if
4833-
let localAciArchive = try Data.fetchOne(tx.database, sql: localAciSql),
4834-
let object = try? NSKeyedUnarchiver.unarchiveTopLevelObjectWithData(localAciArchive),
4835-
object is String
4836-
{
4837-
// If we have an aci, we must be registered.
4838-
let localDeviceIdSql = """
4839-
SELECT * FROM keyvalue
4840-
WHERE collection = 'TSStorageUserAccountCollection'
4841-
AND KEY = 'TSAccountManager_DeviceId'
4842-
"""
4843-
let localDeviceId = try Row.fetchOne(tx.database, sql: localDeviceIdSql)
4827+
let store = NewKeyValueStore(collection: "TSStorageUserAccountCollection")
4828+
let localAci = try store.fetchValueOrThrow(String.self, forKey: "TSStorageRegisteredUUIDKey", tx: tx)
4829+
if localAci != nil {
4830+
let localDeviceId = try store.fetchValueOrThrow(Int64.self, forKey: "TSAccountManager_DeviceId", tx: tx)
48444831
if localDeviceId == nil {
48454832
// If we don't have a device id written, put the primary device id.
4846-
let deviceIdToInsert: UInt32 = 1
4847-
let archiveData = try NSKeyedArchiver.archivedData(
4848-
withRootObject: NSNumber(value: deviceIdToInsert),
4849-
requiringSecureCoding: false
4850-
)
4851-
try tx.database.execute(
4852-
sql: """
4853-
INSERT OR REPLACE INTO keyvalue
4854-
(KEY,collection,VALUE)
4855-
VALUES ('TSAccountManager_DeviceId','TSStorageUserAccountCollection',?)
4856-
""",
4857-
arguments: [archiveData]
4858-
)
4833+
try store.writeValueOrThrow(1, forKey: "TSAccountManager_DeviceId", tx: tx)
48594834
}
48604835
}
48614836
return .success(())

0 commit comments

Comments
 (0)