Repair startup FK recovery and paired turn runtime fixes
This commit is contained in:
@@ -3,6 +3,7 @@ import fs from 'fs';
|
||||
import path from 'path';
|
||||
|
||||
import { ASSISTANT_NAME, STORE_DIR } from '../config.js';
|
||||
import { logger } from '../logger.js';
|
||||
import { applyBaseSchema } from './base-schema.js';
|
||||
import { applySchemaMigrations } from './schema.js';
|
||||
|
||||
@@ -10,6 +11,19 @@ function setForeignKeys(database: Database, enabled: boolean): void {
|
||||
database.exec(`PRAGMA foreign_keys = ${enabled ? 'ON' : 'OFF'}`);
|
||||
}
|
||||
|
||||
function pruneOrphanTaskRunLogs(database: Database): number {
|
||||
const result = database
|
||||
.prepare(
|
||||
`DELETE FROM task_run_logs
|
||||
WHERE NOT EXISTS (
|
||||
SELECT 1 FROM scheduled_tasks
|
||||
WHERE scheduled_tasks.id = task_run_logs.task_id
|
||||
)`,
|
||||
)
|
||||
.run();
|
||||
return result.changes;
|
||||
}
|
||||
|
||||
function assertNoForeignKeyViolations(database: Database): void {
|
||||
const violations = database
|
||||
.prepare('PRAGMA foreign_key_check')
|
||||
@@ -35,8 +49,15 @@ export function initializeDatabaseSchema(database: Database): void {
|
||||
applySchemaMigrations(database, {
|
||||
assistantName: ASSISTANT_NAME,
|
||||
});
|
||||
const prunedTaskRunLogs = pruneOrphanTaskRunLogs(database);
|
||||
setForeignKeys(database, true);
|
||||
assertNoForeignKeyViolations(database);
|
||||
if (prunedTaskRunLogs > 0) {
|
||||
logger.warn(
|
||||
{ count: prunedTaskRunLogs },
|
||||
'Pruned orphan task_run_logs rows during database initialization',
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
export function openPersistentDatabase(): Database {
|
||||
|
||||
Reference in New Issue
Block a user