biggreentree
biggreentree

Reputation: 1913

in swift how to access to database to GRDB.swft/SQLCipher on signal app fork?

I'm currently working on a fork from signal messaging app, here the original 5.22 version my app is coming from. please take a look at the repo. I'm not the original developer of this fork, and have no experience with databases.

MORE INFO:

I want to perform a local encrypted and zipped, backup, in order for the user to store it on a pc, disk or else. I'm doing it by copying database, WAL, attachment and so on, enclosing everything in a file. At this point user already installed app and started the "restore backup" path, all the files of the backup are stored in a new folder.

App is using GRDB.swift/SQLCipher pod for managing database.

old DB and new one are crated the same way, I suppose they use same passphrase, set in Configuration() init

GOAL: I should read the old database and write data to the one the installation just created, selecting what to overwrite (chat) and keep (settings, keys, etc).

since I suppose I should open database session on "imported" database first, I try to do this:

do {
var configuration = Configuration()
let dbQueueImported = try DatabaseQueue(path: importedSqlite.path) //here is crashing

//then I don't know what to do, some copy action maybe.

} catch {
print("error \(error)")
}

but dbQueueImported triggers this assert

extension Database {

//MARK: - database closing

///this method must be called before database deallocation

func close() {

SchedulingWatchdog.precodnitionValidQueue(self)
assert(!isClosed)

//rest of the code....

last thing, if I comment the assert I get the error:

SQLite error 1 with statement 'SELECT * FROM sqlite_master LIMIT 1': unsupported file format.

I suppose I should do something in order to open/close connection

even if you could tell me if there is someone at signal I could ask for help could be useful

UPDATE:

adding a swift error breakpoint I get:

2023-09-12 14:28:52:702 Signal[8970:3357545] ๐Ÿงก [SDSDatabaseStorage.swift:355 write(file:function:line:block:)]: Database write on main thread.
2023-09-12 14:28:52:702 Signal[8970:3357545] ๐Ÿ’› [GRDBDatabaseStorageAdapter.swift:820 dbQueryLog(_:)]: BEGIN IMMEDIATE TRANSACTION
2023-09-12 14:28:52:704 Signal[8970:3357545] ๐Ÿ’› [GRDBDatabaseStorageAdapter.swift:820 dbQueryLog(_:)]:     INSERT INTO keyvalue (
        key,
        collection,
        value
    ) VALUES ('TSAccountManager_IsTransferInProgressKey', 'TSStorageUserAccountCollection', x'<139 byte>')
    ON CONFLICT (
        key,
        collection
    ) DO UPDATE
    SET value = x'<139 byte>'

localized in the throw in Atomics.swift here:

extension AtomicValue where T: Equatable {
    // Sets value to "toValue" IFF it currently has "fromValue",
    // otherwise throws.
    public func transition(from fromValue: T, to toValue: T) throws {
        try lock.perform {
            guard self.value == fromValue else {
                throw AtomicError.invalidTransition
            }
            self.value = toValue
        }
    }
}

Upvotes: 0

Views: 240

Answers (1)

Gwendal Rou&#233;
Gwendal Rou&#233;

Reputation: 4044

The assert(!isClosed) assertion was removed in GRDB 5.0.3: consider upgrading your copy of GRDB.

Since you're probably not running GRDB 5, see the migrations guides:

Upvotes: 0

Related Questions