fix config

This commit is contained in:
2026-09-12 19:41:13 -04:00
parent b86b3d5cc1
commit bb1e5c6a95
2 changed files with 34 additions and 21 deletions
+31 -17
View File
@@ -153,24 +153,38 @@ export function db(): Database.Database {
if (_db) return _db;
mkdirSync(dirname(resolve(config.dbPath)), { recursive: true });
_db = new Database(config.dbPath);
_db.pragma('journal_mode = WAL');
_db.pragma('foreign_keys = ON');
dropLegacyBetaTables(_db);
_db.exec(schemaSql);
migrateServers(_db);
migrateTable(_db, 'scripts', SCRIPTS_COLUMNS);
migrateTable(_db, 'script_versions', SCRIPT_VERSIONS_COLUMNS);
migrateTable(_db, 'beta_scripts', BETA_SCRIPTS_COLUMNS);
migrateTable(_db, 'deployment_scripts', DEPLOYMENT_SCRIPTS_COLUMNS);
migrateTable(_db, 'deployments', DEPLOYMENTS_COLUMNS);
_db.exec(
`CREATE UNIQUE INDEX IF NOT EXISTS idx_scripts_one_baseline_per_scope
ON scripts(scope_type, scope_value) WHERE kind = 'baseline'`
);
_db.exec(`CREATE INDEX IF NOT EXISTS idx_scripts_kind ON scripts(kind)`);
backfillMultiScriptJoins(_db);
const handle = new Database(config.dbPath);
// Do all the risky init on a local handle first — only assign the module
// singleton once migrations succeed. A half-migrated handle that's cached
// silently breaks every subsequent query without a chance to retry.
try {
handle.pragma('journal_mode = WAL');
handle.pragma('foreign_keys = ON');
dropLegacyBetaTables(handle);
handle.exec(schemaSql);
migrateServers(handle);
// Column additions must run BEFORE any statement that references the
// new columns. schema.sql cannot include those references — see the
// comment there.
migrateTable(handle, 'scripts', SCRIPTS_COLUMNS);
migrateTable(handle, 'script_versions', SCRIPT_VERSIONS_COLUMNS);
migrateTable(handle, 'beta_scripts', BETA_SCRIPTS_COLUMNS);
migrateTable(handle, 'deployment_scripts', DEPLOYMENT_SCRIPTS_COLUMNS);
migrateTable(handle, 'deployments', DEPLOYMENTS_COLUMNS);
// Kind-referencing indexes: safe here because migrateTable above just
// ALTER'd the column into existence if it was missing.
handle.exec(
`CREATE UNIQUE INDEX IF NOT EXISTS idx_scripts_one_baseline_per_scope
ON scripts(scope_type, scope_value) WHERE kind = 'baseline'`
);
handle.exec(`CREATE INDEX IF NOT EXISTS idx_scripts_kind ON scripts(kind)`);
backfillMultiScriptJoins(handle);
} catch (err) {
handle.close();
throw err;
}
_db = handle;
return _db;
}