multi script beta
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
@@ -67,9 +67,12 @@ CREATE INDEX IF NOT EXISTS idx_server_scripts_server ON server_scripts(server);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS betas (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
-- DEPRECATED: multi-script betas are represented by beta_scripts. These
|
||||
-- three columns are populated from the first bundled script to satisfy
|
||||
-- NOT NULL on legacy schemas; the fleet code doesn't read them anymore.
|
||||
script_id INTEGER NOT NULL REFERENCES scripts(id) ON DELETE CASCADE,
|
||||
script_version_id INTEGER NOT NULL REFERENCES script_versions(id) ON DELETE CASCADE,
|
||||
trigger_kind TEXT NOT NULL, -- 'mark-required' | 'version-update' | 'unscoped'
|
||||
trigger_kind TEXT NOT NULL,
|
||||
wait_for_health INTEGER NOT NULL DEFAULT 1,
|
||||
wait_for_match INTEGER NOT NULL DEFAULT 0,
|
||||
state TEXT NOT NULL, -- 'running' | 'ready-promote' | 'promoted' | 'rolled-back'
|
||||
@@ -83,8 +86,8 @@ CREATE INDEX IF NOT EXISTS idx_betas_script ON betas(script_id);
|
||||
CREATE TABLE IF NOT EXISTS beta_servers (
|
||||
beta_id INTEGER NOT NULL REFERENCES betas(id) ON DELETE CASCADE,
|
||||
server TEXT NOT NULL REFERENCES servers(name),
|
||||
had_original INTEGER NOT NULL,
|
||||
prior_version_id INTEGER REFERENCES script_versions(id),
|
||||
had_original INTEGER NOT NULL, -- deprecated; see beta_server_scripts
|
||||
prior_version_id INTEGER REFERENCES script_versions(id), -- deprecated; see beta_server_scripts
|
||||
stage TEXT NOT NULL, -- 'deploying'|'awaiting-empty'|'restarting'|'awaiting-health'|'awaiting-match'|'ready'|'failed'
|
||||
error TEXT,
|
||||
deployed_at TEXT,
|
||||
@@ -96,6 +99,30 @@ CREATE TABLE IF NOT EXISTS beta_servers (
|
||||
);
|
||||
CREATE INDEX IF NOT EXISTS idx_beta_servers_stage ON beta_servers(stage);
|
||||
|
||||
-- Many-to-many: which scripts (and pinned versions) are bundled into a beta.
|
||||
-- Replaces the deprecated betas.script_id / betas.script_version_id columns.
|
||||
CREATE TABLE IF NOT EXISTS beta_scripts (
|
||||
beta_id INTEGER NOT NULL REFERENCES betas(id) ON DELETE CASCADE,
|
||||
script_id INTEGER NOT NULL REFERENCES scripts(id) ON DELETE CASCADE,
|
||||
script_version_id INTEGER NOT NULL REFERENCES script_versions(id) ON DELETE CASCADE,
|
||||
PRIMARY KEY (beta_id, script_id)
|
||||
);
|
||||
CREATE INDEX IF NOT EXISTS idx_beta_scripts_beta ON beta_scripts(beta_id);
|
||||
CREATE INDEX IF NOT EXISTS idx_beta_scripts_script ON beta_scripts(script_id);
|
||||
|
||||
-- Per (beta, server, script) prior-file state. A server row in beta_servers
|
||||
-- covers restart progress; this table covers what each script's target
|
||||
-- looked like on that server before the beta wrote its -beta.gsc.
|
||||
CREATE TABLE IF NOT EXISTS beta_server_scripts (
|
||||
beta_id INTEGER NOT NULL REFERENCES betas(id) ON DELETE CASCADE,
|
||||
server TEXT NOT NULL REFERENCES servers(name),
|
||||
script_id INTEGER NOT NULL REFERENCES scripts(id) ON DELETE CASCADE,
|
||||
had_original INTEGER NOT NULL,
|
||||
prior_version_id INTEGER REFERENCES script_versions(id),
|
||||
PRIMARY KEY (beta_id, server, script_id)
|
||||
);
|
||||
CREATE INDEX IF NOT EXISTS idx_beta_server_scripts_beta ON beta_server_scripts(beta_id);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS deployments (
|
||||
id TEXT PRIMARY KEY,
|
||||
action TEXT NOT NULL, -- 'drift' | 'promote' | 'rollback'
|
||||
@@ -140,6 +167,18 @@ CREATE TABLE IF NOT EXISTS deployment_files (
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_deployment_files_dep ON deployment_files(deployment_id);
|
||||
|
||||
-- Per-script rows on aggregate promote/rollback deployments. For legacy
|
||||
-- single-script deployments this may also be backfilled from the deprecated
|
||||
-- deployments.script_id / script_version_id / prior_version_id columns.
|
||||
CREATE TABLE IF NOT EXISTS deployment_scripts (
|
||||
deployment_id TEXT NOT NULL REFERENCES deployments(id) ON DELETE CASCADE,
|
||||
script_id INTEGER NOT NULL REFERENCES scripts(id) ON DELETE CASCADE,
|
||||
script_version_id INTEGER NOT NULL REFERENCES script_versions(id) ON DELETE CASCADE,
|
||||
prior_version_id INTEGER REFERENCES script_versions(id),
|
||||
PRIMARY KEY (deployment_id, script_id)
|
||||
);
|
||||
CREATE INDEX IF NOT EXISTS idx_deployment_scripts_dep ON deployment_scripts(deployment_id);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS settings (
|
||||
key TEXT PRIMARY KEY,
|
||||
value TEXT NOT NULL
|
||||
|
||||
Reference in New Issue
Block a user