fix: Identify and fix migration tracking issue with proper parameter binding

- Root cause: Migration names were not being properly inserted into migrations table
- Fixed parameter binding in Capacitor platform service migration functions
- Added detailed debugging to track SQL execution and parameter passing
- Reverted migrations back to proper form (without IF NOT EXISTS workarounds)
- Enhanced extractMigrationNames to handle Capacitor SQLite result format

The migration system should now properly track applied migrations and avoid
re-running them on subsequent app starts.
This commit is contained in:
Matthew Raymer
2025-06-30 07:09:35 +00:00
parent 4120f5a94e
commit 4a01df509b
3 changed files with 78 additions and 90 deletions

View File

@@ -242,17 +242,50 @@ export class CapacitorPlatformService implements PlatformService {
throw new Error("Database not initialized");
}
const sqlExec: (sql: string) => Promise<capSQLiteChanges> =
this.db.execute.bind(this.db);
const sqlQuery: (sql: string) => Promise<DBSQLiteValues> =
this.db.query.bind(this.db);
const extractMigrationNames: (result: DBSQLiteValues) => Set<string> = (
result,
) => {
const names =
result.values?.map((row: { name: string }) => row.name) || [];
const sqlExec = async (sql: string, params?: unknown[]): Promise<capSQLiteChanges> => {
console.log(`🔧 [CapacitorMigration] Executing SQL:`, sql);
console.log(`📋 [CapacitorMigration] With params:`, params);
if (params && params.length > 0) {
// Use run method for parameterized queries
const result = await this.db!.run(sql, params);
console.log(`✅ [CapacitorMigration] Run result:`, result);
return result;
} else {
// Use execute method for non-parameterized queries
const result = await this.db!.execute(sql);
console.log(`✅ [CapacitorMigration] Execute result:`, result);
return result;
}
};
const sqlQuery = async (sql: string, params?: unknown[]): Promise<DBSQLiteValues> => {
console.log(`🔍 [CapacitorMigration] Querying SQL:`, sql);
console.log(`📋 [CapacitorMigration] With params:`, params);
const result = await this.db!.query(sql, params);
console.log(`📊 [CapacitorMigration] Query result:`, result);
return result;
};
const extractMigrationNames = (result: DBSQLiteValues): Set<string> => {
console.log(`🔍 [CapacitorMigration] Extracting migration names from:`, result);
// Handle the Capacitor SQLite result format
const names = result.values?.map((row: any) => {
// The row could be an object with 'name' property or an array where name is first element
if (typeof row === 'object' && row.name !== undefined) {
return row.name;
} else if (Array.isArray(row) && row.length > 0) {
return row[0];
}
return null;
}).filter(name => name !== null) || [];
console.log(`📋 [CapacitorMigration] Extracted names:`, names);
return new Set(names);
};
await runMigrations(sqlExec, sqlQuery, extractMigrationNames);
}