Skip to content

Privacy-first password manager with built-in email aliasing. Fully encrypted and self-hostable.

Update datamodel upgrade logic for Android to fix transaction and pragma conflicts (#1576)

Leendert de Borstfecede2b9d1f
diff --git a/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/nativevaultmanager/NativeVaultManager.kt b/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/nativevaultmanager/NativeVaultManager.kt
index d9914a18f..e1396ad9d 100644
--- a/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/nativevaultmanager/NativeVaultManager.kt
+++ b/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/nativevaultmanager/NativeVaultManager.kt
@@ -485,6 +485,26 @@ class NativeVaultManager(reactContext: ReactApplicationContext) :
}
}
+ /**
+ * Persist the in-memory database to encrypted storage and mark as dirty.
+ * Used after migrations where SQL handles its own transactions but we need to persist and sync.
+ * @param promise The promise to resolve
+ */
+ @ReactMethod
+ override fun persistAndMarkDirty(promise: Promise) {
+ try {
+ vaultStore.persistAndMarkDirty()
+ promise.resolve(null)
+ } catch (e: Exception) {
+ Log.e(TAG, "Error persisting and marking dirty", e)
+ promise.reject(
+ "ERR_PERSIST_AND_MARK_DIRTY",
+ "Failed to persist and mark dirty: ${e.message}",
+ e,
+ )
+ }
+ }
+
/**
* Set the auto-lock timeout.
* @param timeout The timeout in seconds
diff --git a/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/vaultstore/VaultStore.kt b/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/vaultstore/VaultStore.kt
index 82236dcc7..a621c300a 100644
--- a/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/vaultstore/VaultStore.kt
+++ b/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/vaultstore/VaultStore.kt
@@ -334,9 +334,11 @@ class VaultStore(
/**
* Execute a raw SQL command on the vault without parameters.
- * Splits the query by semicolons to handle multiple statements and
- * skips transaction control statements (BEGIN TRANSACTION, COMMIT, ROLLBACK)
- * as those are handled externally by the caller.
+ * Splits the query by semicolons to handle multiple statements.
+ *
+ * Note: Migration SQL scripts handle their own transactions and PRAGMA statements.
+ * PRAGMA foreign_keys statements MUST be executed outside of transactions to take effect,
+ * which is why we execute them using rawQuery instead of compileStatement.
*/
fun executeRaw(queryString: String) {
val db = databaseComponent.dbConnection ?: error("Database not initialized")
@@ -344,24 +346,31 @@ class VaultStore(
// Strip BOM (U+FEFF) that may be present at the start of SQL strings.
val cleanedQuery = queryString.trimStart('\uFEFF')
- // Split by semicolons to handle multiple statements
- val statements = cleanedQuery.split(";")
-
- for (statement in statements) {
- val trimmed = statement.trim().trimStart('\uFEFF')
-
- // Skip empty statements and transaction control statements (handled externally)
- if (trimmed.isEmpty() ||
- trimmed.uppercase().startsWith("BEGIN TRANSACTION") ||
- trimmed.uppercase().startsWith("COMMIT") ||
- trimmed.uppercase().startsWith("ROLLBACK")
- ) {
- continue
+ // Split by semicolons to handle multiple statements and filter out empty ones
+ cleanedQuery.split(";")
+ .map { it.trim().trimStart('\uFEFF') }
+ .filter { it.isNotEmpty() && !it.startsWith("--") }
+ .forEach { trimmed ->
+ val upperTrimmed = trimmed.uppercase()
+ when {
+ // Handle PRAGMA statements using rawQuery (required for PRAGMA to work properly)
+ upperTrimmed.startsWith("PRAGMA") -> db.rawQuery(trimmed, null)?.close()
+ // Handle transaction control statements using execSQL
+ isTransactionControlStatement(upperTrimmed) -> db.execSQL(trimmed)
+ // Use compileStatement for all other SQL (DDL and DML)
+ else -> db.compileStatement(trimmed).execute()
+ }
}
+ }
- val stmt = db.compileStatement(trimmed)
- stmt.execute()
- }
+ /**
+ * Check if a SQL statement is a transaction control statement.
+ */
+ private fun isTransactionControlStatement(upperStatement: String): Boolean {
+ return upperStatement.startsWith("BEGIN TRANSACTION") ||
+ upperStatement.startsWith("BEGIN") ||
+ upperStatement.startsWith("COMMIT") ||
+ upperStatement.startsWith("ROLLBACK")
}
/**
@@ -392,6 +401,20 @@ class VaultStore(
databaseComponent.rollbackTransaction()
}
+ /**
+ * Persist the in-memory database to encrypted storage and mark as dirty.
+ * Used after migrations where SQL handles its own transactions but we need to persist and sync.
+ * This does NOT commit any SQL transaction - it just persists the current state of the database.
+ */
+ fun persistAndMarkDirty() {
+ databaseComponent.persistDatabaseToEncryptedStorage()
+
+ // Atomically mark vault as dirty and increment mutation sequence
+ // This ensures sync can properly detect local changes
+ metadata.setIsDirty(true)
+ metadata.incrementMutationSequence()
+ }
+
/**
* Get all items from the vault.
*/