multi script beta

This commit is contained in:
2026-09-12 18:23:56 -04:00
parent 30e59c900c
commit 85bcb05a7d
16 changed files with 1053 additions and 516 deletions
+34
View File
@@ -70,6 +70,39 @@ function migrateTable(handle: Database.Database, table: string, cols: Array<{ na
}
}
// Backfill the multi-script join tables from the legacy single-script columns.
// Idempotent — every INSERT is guarded by a NOT EXISTS clause on the target
// row's primary key. Safe to run on every boot even after the deprecated
// columns are eventually dropped.
function backfillMultiScriptJoins(handle: Database.Database): void {
handle.exec(
`INSERT INTO beta_scripts (beta_id, script_id, script_version_id)
SELECT b.id, b.script_id, b.script_version_id
FROM betas b
WHERE b.script_id IS NOT NULL
AND NOT EXISTS (SELECT 1 FROM beta_scripts bs WHERE bs.beta_id = b.id)`
);
handle.exec(
`INSERT INTO beta_server_scripts (beta_id, server, script_id, had_original, prior_version_id)
SELECT bs.beta_id, bs.server, b.script_id, bs.had_original, bs.prior_version_id
FROM beta_servers bs
JOIN betas b ON b.id = bs.beta_id
WHERE b.script_id IS NOT NULL
AND NOT EXISTS (
SELECT 1 FROM beta_server_scripts x
WHERE x.beta_id = bs.beta_id AND x.server = bs.server AND x.script_id = b.script_id
)`
);
handle.exec(
`INSERT INTO deployment_scripts (deployment_id, script_id, script_version_id, prior_version_id)
SELECT d.id, d.script_id, d.script_version_id, d.prior_version_id
FROM deployments d
WHERE d.script_id IS NOT NULL
AND d.script_version_id IS NOT NULL
AND NOT EXISTS (SELECT 1 FROM deployment_scripts ds WHERE ds.deployment_id = d.id)`
);
}
// The pre-script-model beta tables had entirely different shapes (slug PK,
// beta_scripts, beta_deploys). schema.sql defines the new shape; CREATE TABLE
// IF NOT EXISTS is a no-op if the old tables already exist, so we have to
@@ -115,6 +148,7 @@ export function db(): Database.Database {
migrateServers(_db);
migrateTable(_db, 'script_versions', SCRIPT_VERSIONS_COLUMNS);
migrateTable(_db, 'deployments', DEPLOYMENTS_COLUMNS);
backfillMultiScriptJoins(_db);
return _db;
}