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
+28 -14
View File
@@ -153,24 +153,38 @@ export function db(): Database.Database {
if (_db) return _db; if (_db) return _db;
mkdirSync(dirname(resolve(config.dbPath)), { recursive: true }); mkdirSync(dirname(resolve(config.dbPath)), { recursive: true });
_db = new Database(config.dbPath); const handle = new Database(config.dbPath);
_db.pragma('journal_mode = WAL'); // Do all the risky init on a local handle first — only assign the module
_db.pragma('foreign_keys = ON'); // singleton once migrations succeed. A half-migrated handle that's cached
dropLegacyBetaTables(_db); // silently breaks every subsequent query without a chance to retry.
_db.exec(schemaSql); try {
migrateServers(_db); handle.pragma('journal_mode = WAL');
migrateTable(_db, 'scripts', SCRIPTS_COLUMNS); handle.pragma('foreign_keys = ON');
migrateTable(_db, 'script_versions', SCRIPT_VERSIONS_COLUMNS); dropLegacyBetaTables(handle);
migrateTable(_db, 'beta_scripts', BETA_SCRIPTS_COLUMNS); handle.exec(schemaSql);
migrateTable(_db, 'deployment_scripts', DEPLOYMENT_SCRIPTS_COLUMNS); migrateServers(handle);
migrateTable(_db, 'deployments', DEPLOYMENTS_COLUMNS); // Column additions must run BEFORE any statement that references the
_db.exec( // 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 `CREATE UNIQUE INDEX IF NOT EXISTS idx_scripts_one_baseline_per_scope
ON scripts(scope_type, scope_value) WHERE kind = 'baseline'` ON scripts(scope_type, scope_value) WHERE kind = 'baseline'`
); );
_db.exec(`CREATE INDEX IF NOT EXISTS idx_scripts_kind ON scripts(kind)`); handle.exec(`CREATE INDEX IF NOT EXISTS idx_scripts_kind ON scripts(kind)`);
backfillMultiScriptJoins(_db); backfillMultiScriptJoins(handle);
} catch (err) {
handle.close();
throw err;
}
_db = handle;
return _db; return _db;
} }
+3 -4
View File
@@ -45,10 +45,9 @@ CREATE TABLE IF NOT EXISTS scripts (
created_at TEXT NOT NULL DEFAULT (strftime('%Y-%m-%dT%H:%M:%fZ','now')) created_at TEXT NOT NULL DEFAULT (strftime('%Y-%m-%dT%H:%M:%fZ','now'))
); );
CREATE INDEX IF NOT EXISTS idx_scripts_scope ON scripts(scope_type, scope_value); CREATE INDEX IF NOT EXISTS idx_scripts_scope ON scripts(scope_type, scope_value);
CREATE INDEX IF NOT EXISTS idx_scripts_kind ON scripts(kind); -- kind-referencing indexes are created in db/index.ts AFTER migrateTable adds
-- At most one baseline per (scope_type, scope_value). Partial unique index. -- the column to legacy DBs. Putting them here would fail with "no such column:
CREATE UNIQUE INDEX IF NOT EXISTS idx_scripts_one_baseline_per_scope -- kind" on an existing pre-kind DB, breaking boot.
ON scripts(scope_type, scope_value) WHERE kind = 'baseline';
CREATE TABLE IF NOT EXISTS script_versions ( CREATE TABLE IF NOT EXISTS script_versions (
id INTEGER PRIMARY KEY AUTOINCREMENT, id INTEGER PRIMARY KEY AUTOINCREMENT,