181
* migrations complete the pragma is updated to the highest applied version.
182
*/
183
>
export async function runMigrations(db: Database, migrations: readonly ISessionDatabaseMigration[]): Promise<void> {
sessionDatabase.ts
184
>
// Enable foreign key enforcement — must be set outside a transaction
185
>
// and every time a connection is opened.
186
>
await dbExec(db, 'PRAGMA foreign_keys = ON');
187
>
188
>
const row = await dbGet(db, 'PRAGMA user_version', []);
189
>
const currentVersion = (row?.user_version as number | undefined) ?? 0;
190
>
191
>
const pending = migrations
192
>
.filter(m => m.version > currentVersion)
193
>
.sort((a, b) => a.version - b.version);
194
>
195
>
if (pending.length === 0) {
196
return;
197
}
199
>
await dbExec(db, 'BEGIN TRANSACTION');
200
>
try {
201
>
for (const migration of pending) {
202
>
await dbExec(db, migration.sql);
203
>
// PRAGMA cannot be parameterized; the version is a trusted literal.
204
>
await dbExec(db, `PRAGMA user_version = ${migration.version}`);
205
>
}
206
>
await dbExec(db, 'COMMIT');
207
>
} catch (err) {
208
await dbExec(db, 'ROLLBACK');
209
throw err;
210
}
212
213
/**