Add repair_migrations Tauri command that deletes stale migration 1 checksum from _sqlx_migrations before Database.load(). Migration 1 is idempotent (CREATE IF NOT EXISTS) so re-applying is safe. Fixes "migration 1 was previously applied but has been modified". Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
46 lines
1.3 KiB
TypeScript
46 lines
1.3 KiB
TypeScript
import Database from "@tauri-apps/plugin-sql";
|
|
import { invoke } from "@tauri-apps/api/core";
|
|
|
|
let dbInstance: Database | null = null;
|
|
|
|
export async function getDb(): Promise<Database> {
|
|
if (!dbInstance) {
|
|
throw new Error("No database connection. Call connectToProfile() first.");
|
|
}
|
|
return dbInstance;
|
|
}
|
|
|
|
export async function connectToProfile(dbFilename: string): Promise<void> {
|
|
if (dbInstance) {
|
|
await dbInstance.close();
|
|
dbInstance = null;
|
|
}
|
|
// Repair migration checksums before loading (fixes "migration was modified" error)
|
|
try {
|
|
const repaired = await invoke<boolean>("repair_migrations", { dbFilename });
|
|
if (repaired) {
|
|
console.warn("Migration checksums repaired for", dbFilename);
|
|
}
|
|
} catch (e) {
|
|
console.error("Migration repair failed:", e);
|
|
}
|
|
dbInstance = await Database.load(`sqlite:${dbFilename}`);
|
|
}
|
|
|
|
export async function initializeNewProfileDb(dbFilename: string, sqlStatements: string[]): Promise<void> {
|
|
if (dbInstance) {
|
|
await dbInstance.close();
|
|
dbInstance = null;
|
|
}
|
|
dbInstance = await Database.load(`sqlite:${dbFilename}`);
|
|
for (const sql of sqlStatements) {
|
|
await dbInstance.execute(sql);
|
|
}
|
|
}
|
|
|
|
export async function closeDb(): Promise<void> {
|
|
if (dbInstance) {
|
|
await dbInstance.close();
|
|
dbInstance = null;
|
|
}
|
|
}
|