(
db: Database.Database,
forceRepair = false,
options: DesktopMigrationOptions = {}
)
| 71 | * with shared migration definitions from @openchatlab/node-runtime. |
| 72 | */ |
| 73 | export function migrateDatabase( |
| 74 | db: Database.Database, |
| 75 | forceRepair = false, |
| 76 | options: DesktopMigrationOptions = {} |
| 77 | ): boolean { |
| 78 | const adapter = new BetterSqliteAdapter(db) |
| 79 | |
| 80 | const integrity = checkDatabaseIntegrity(adapter) |
| 81 | if (!integrity.valid) { |
| 82 | throw new Error(integrity.error) |
| 83 | } |
| 84 | |
| 85 | const beforeVersion = getSchemaVersion(adapter) |
| 86 | const migrations = getChatDbMigrations({ tokenizeForFts }) |
| 87 | const migrated = runMigrations(adapter, migrations, forceRepair) |
| 88 | if (!migrated || !options.pathProvider || !options.runtime) return migrated |
| 89 | |
| 90 | const afterVersion = getSchemaVersion(adapter) |
| 91 | for (const compatibilityRaise of CHAT_DB_COMPATIBILITY_RAISES) { |
| 92 | if (beforeVersion >= compatibilityRaise.migrationVersion || afterVersion < compatibilityRaise.migrationVersion) { |
| 93 | continue |
| 94 | } |
| 95 | |
| 96 | raiseDataDirMinRuntimeVersion(options.pathProvider, { |
| 97 | minRuntimeVersion: compatibilityRaise.minRuntimeVersion, |
| 98 | dataCompatibilityVersion: compatibilityRaise.dataCompatibilityVersion, |
| 99 | reason: compatibilityRaise.reason, |
| 100 | runtime: options.runtime, |
| 101 | module: compatibilityRaise.module, |
| 102 | }) |
| 103 | } |
| 104 | |
| 105 | return migrated |
| 106 | } |
| 107 | |
| 108 | export function needsMigration(db: Database.Database): boolean { |
| 109 | const adapter = new BetterSqliteAdapter(db) |
no test coverage detected