Skip to content

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

Fix Android upgrade flow

Leendert de Borst9d1c3e5b856a
diff --git a/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/vaultstore/VaultDatabase.kt b/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/vaultstore/VaultDatabase.kt
index b6d98c7a4..3a49c4132 100644
--- a/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/vaultstore/VaultDatabase.kt
+++ b/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/vaultstore/VaultDatabase.kt
@@ -220,7 +220,8 @@ class VaultDatabase(
* Begin a SQL transaction on the vault.
*/
fun beginTransaction() {
- dbConnection?.beginTransaction()
+ val db = dbConnection ?: error(IllegalStateException("Database not initialized"))
+ db.compileStatement("BEGIN TRANSACTION").execute()
}
/**
@@ -248,11 +249,9 @@ class VaultDatabase(
// Retry up to 5 times if we hit transient locking errors
for (attempt in 1..5) {
try {
- // VACUUM cannot run inside a transaction
- if (db.inTransaction()) {
- Log.w(TAG, "Database was in a transaction; ending before VACUUM")
- db.endTransaction()
- }
+ // VACUUM cannot run inside a transaction.
+ // End any lingering transaction (no-op if none).
+ try { db.compileStatement("END").execute() } catch (_: Exception) {}
db.compileStatement(vacuumIntoSql).use { it.execute() }
break // Success, exit the loop
@@ -298,8 +297,8 @@ class VaultDatabase(
* Commit a SQL transaction and persist the encrypted vault.
*/
fun commitTransaction() {
- dbConnection?.setTransactionSuccessful()
- dbConnection?.endTransaction()
+ val db = dbConnection ?: error(IllegalStateException("Database not initialized"))
+ db.compileStatement("COMMIT").execute()
persistDatabaseToEncryptedStorage()
}
@@ -307,7 +306,8 @@ class VaultDatabase(
* Rollback a SQL transaction on the vault.
*/
fun rollbackTransaction() {
- dbConnection?.endTransaction()
+ val db = dbConnection ?: error(IllegalStateException("Database not initialized"))
+ db.compileStatement("ROLLBACK").execute()
}
// endregion
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 921835a5c..82236dcc7 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,11 +334,34 @@ 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.
*/
fun executeRaw(queryString: String) {
val db = databaseComponent.dbConnection ?: error("Database not initialized")
- val stmt = db.compileStatement(queryString)
- stmt.execute()
+
+ // 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
+ }
+
+ val stmt = db.compileStatement(trimmed)
+ stmt.execute()
+ }
}
/**
diff --git a/apps/mobile-app/app/upgrade.tsx b/apps/mobile-app/app/upgrade.tsx
index 507d0a59b..8ede7e4dc 100644
--- a/apps/mobile-app/app/upgrade.tsx
+++ b/apps/mobile-app/app/upgrade.tsx
@@ -43,6 +43,8 @@ export default function UpgradeScreen() : React.ReactNode {
const { syncVault } = useVaultSync();
const { showAlert, showConfirm } = useDialog();
+ const [isSyncingOnLoad, setIsSyncingOnLoad] = useState(true);
+
// Initialize upgrade status with translation
useEffect(() => {
setUpgradeStatus(t('upgrade.status.preparingUpgrade'));
@@ -64,9 +66,53 @@ export default function UpgradeScreen() : React.ReactNode {
}
}, [sqliteClient]);
+ /**
+ * Try to sync with the server on load. If the vault was already upgraded
+ * on another device, the server may have a newer vault that doesn't need
+ * local migration, allowing us to skip the upgrade entirely.
+ */
useEffect(() => {
- loadVersionInfo();
- }, [loadVersionInfo]);
+ /**
+ * Sync on load to check if server has an already-upgraded vault.
+ */
+ const syncOnLoad = async (): Promise<void> => {
+ let skipVersionLoad = false;
+
+ try {
+ setUpgradeStatus(t('vault.checkingVaultUpdates'));
+ await syncVault({
+ /**
+ * Handle the status update.
+ */
+ onStatus: (message) => setUpgradeStatus(message),
+ /**
+ * Handle successful sync and check if upgrade is still needed.
+ */
+ onSuccess: async () => {
+ // After sync, check if we still need to upgrade
+ if (!(await dbContext.hasPendingMigrations())) {
+ // Server had an upgraded vault, no local upgrade needed
+ skipVersionLoad = true;
+ dbContext.setDatabaseAvailable();
+ router.replace('/(tabs)/items');
+ }
+ },
+ });
+ } catch {
+ // On any error, fall through to local upgrade flow
+ } finally {
+ // Always load version info unless we're navigating away
+ if (!skipVersionLoad) {
+ await loadVersionInfo();
+ }
+ setIsSyncingOnLoad(false);
+ setUpgradeStatus(t('upgrade.status.preparingUpgrade'));
+ }
+ };
+
+ syncOnLoad();
+ // eslint-disable-next-line react-hooks/exhaustive-deps
+ }, []);
/**
* Handle the vault upgrade.
@@ -395,7 +441,7 @@ export default function UpgradeScreen() : React.ReactNode {
return (
<ThemedView style={styles.container}>
- {(isLoading || isVaultMutationLoading) ? (
+ {(isLoading || isVaultMutationLoading || isSyncingOnLoad) ? (
<View style={styles.loadingContainer}>
<LoadingIndicator status={syncStatus || upgradeStatus} />
</View>