kpgalligan
12/03/2021, 4:05 PMAndrew O Hart
12/03/2021, 4:35 PMif (sqlite3_prepare_v2(encryptedDb.value, "PRAGMA user_version;", -1, sqliteStatementPointer.ptr, null) == SQLITE_OK ) {
while(sqlite3_step(sqliteStatementPointer.value) == SQLITE_ROW) {
sqlite3_bind_int(sqliteStatementPointer.value, 1, dbVersion)
}
}
Where dbVersion is retrieved from the unencrypted DB.
In this case the dbVersion is 1, so I am setting it to 1 here. You think it should dbVersion + 1?kpgalligan
12/03/2021, 4:38 PMkpgalligan
12/03/2021, 4:40 PMPRAGMA user_version = 1
directly to make it simpler.kpgalligan
12/03/2021, 4:41 PMAndrew O Hart
12/03/2021, 4:41 PMkpgalligan
12/03/2021, 4:43 PMAndrew O Hart
12/03/2021, 5:08 PMNativeSqliteDriver(DatabaseConfiguration(
name = DatabaseName,
version = 1,
create = { connection -> wrapConnection(connection) { DatabaseSample.Schema.create(it) } },
// upgrade = { connection, oldVersion, newVersion -> wrapConnection(connection) { DatabaseSample.Schema.migrate(it, oldVersion, newVersion) } },
// Workaround for DatabaseConnection.setCipherKey causing an exception on iOS 14
configConnection = { connection, _ ->
val statement = "PRAGMA key = \"$password\";"
connection.withStatement(statement) {
stringForQuery()
}
}
))
but got the same issue. I'm not sure do I need to include the upgrade line or not, as this return is done after migrating the db
Whole code for migration is this:
val unencryptedDb: CPointerVar<sqlite3> = allocPointerTo()
val encryptedDb: CPointerVar<sqlite3> = allocPointerTo()
if (sqlite3_open(databasePath, unencryptedDb.ptr) == SQLITE_OK) {
val exec1 = sqlite3_exec(unencryptedDb.value, "ATTACH DATABASE '$temporaryDatabasePath' AS encrypted KEY '$password';", null, null, null)
val exec2 = sqlite3_exec(unencryptedDb.value, "SELECT sqlcipher_export('encrypted')", null, null, null)
val exec3 = sqlite3_exec(unencryptedDb.value, "DETACH DATABASE encrypted;", null, null, null)
val dbVersion = getUserVersion(unencryptedDb)
sqlite3_close(unencryptedDb.value)
if (sqlite3_open(temporaryDatabasePath, encryptedDb.ptr) == SQLITE_OK && dbVersion != null) {
sqlite3_key(encryptedDb.value, password.cstr, password.cstr.size)
val sqliteStatementPointer: CPointerVar<sqlite3_stmt> = allocPointerTo()
if (sqlite3_prepare_v2(encryptedDb.value, "PRAGMA user_version;", -1, sqliteStatementPointer.ptr, null) == SQLITE_OK ) {
while(sqlite3_step(sqliteStatementPointer.value) == SQLITE_ROW) {
sqlite3_bind_int(sqliteStatementPointer.value, 1, dbVersion)
}
} else {
sqlite3_errmsg(encryptedDb.value)
NSLog("Error preparing: ${sqlite3_errmsg(encryptedDb.value)}")
}
sqlite3_finalize(sqliteStatementPointer.value)
}
sqlite3_close(unencryptedDb.value)
sqlite3_close(encryptedDb.value)
val error: ObjCObjectVar<NSError?> = alloc()
val removeResult = fileManager.removeItemAtPath(databasePath, error.ptr)
So I'm not sure if my issue is the returning part of NativeSqliteDriver(DatabaseConfiguration)
Sorry very long winded but thought might help understand the context of it morekpgalligan
12/03/2021, 5:13 PMNativeSqliteDriver(DatabaseConfiguration(
name = DatabaseName,
version = DatabaseSample.Schema.version,
create = { connection -> wrapConnection(connection) { DatabaseSample.Schema.create(it) } },
// upgrade = { connection, oldVersion, newVersion -> wrapConnection(connection) { DatabaseSample.Schema.migrate(it, oldVersion, newVersion) } },
// Workaround for DatabaseConnection.setCipherKey causing an exception on iOS 14
configConnection = { connection, _ ->
val statement = "PRAGMA key = \"$password\";"
connection.withStatement(statement) {
stringForQuery()
}
if(connection.getVersion() != 1)
throw IllegalStateException("wrong db version after migration")
}
))
kpgalligan
12/03/2021, 5:14 PMcreate
process run, so check it with
if(connection.getVersion() != 1)
throw IllegalStateException("wrong db version after migration")
kpgalligan
12/03/2021, 5:14 PMAndrew O Hart
12/04/2021, 12:56 AMif (sqlite3_prepare_v2(encryptedDb.value, "PRAGMA user_version;", -1, sqliteStatementPointer.ptr, null) == SQLITE_OK ) {
while(sqlite3_step(sqliteStatementPointer.value) == SQLITE_ROW) {
sqlite3_bind_int(sqliteStatementPointer.value, 1, dbVersion)
}
as far as I know this should set the version to be 1, as the dbVersion is 1