Fix delete-script FK error; trigger drift on scope change

- deleteScript: null out deployments.script_id/script_version_id/prior_version_id
  and deployment_scripts.prior_version_id inside a transaction before the
  cascade. These are historical audit FKs (NO ACTION), so the cascade from
  script_versions was blowing up any delete of a promoted script.
- Scope endpoint fires runDriftDeployment in the background after any scope
  mutation so orphan/missing files from the change get reconciled.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
2026-09-12 23:46:56 -04:00
co-authored by Claude Opus 4.7
parent f716036d89
commit 34dc2ff00f
2 changed files with 47 additions and 1 deletions
+33 -1
View File
@@ -337,7 +337,39 @@ export function deleteScript(scriptId: number): void {
)
.get(scriptId) as { id: number } | undefined;
if (beta) throw new ScriptError(`beta ${beta.id} is still running against this script`, 'has-active-beta');
db().prepare('DELETE FROM scripts WHERE id = ?').run(scriptId);
// Null out deployment-history FKs that would otherwise block the cascade.
// deployments.script_id/script_version_id/prior_version_id and
// deployment_scripts.prior_version_id are historical audit references
// with no ON DELETE action; keep the deployment rows intact but drop
// the FK to the script so we can remove it.
const handle = db();
const tx = handle.transaction((id: number) => {
handle
.prepare(
`UPDATE deployments
SET script_version_id = NULL
WHERE script_version_id IN (SELECT id FROM script_versions WHERE script_id = ?)`
)
.run(id);
handle
.prepare(
`UPDATE deployments
SET prior_version_id = NULL
WHERE prior_version_id IN (SELECT id FROM script_versions WHERE script_id = ?)`
)
.run(id);
handle.prepare('UPDATE deployments SET script_id = NULL WHERE script_id = ?').run(id);
handle
.prepare(
`UPDATE deployment_scripts
SET prior_version_id = NULL
WHERE prior_version_id IN (SELECT id FROM script_versions WHERE script_id = ?)`
)
.run(id);
handle.prepare('DELETE FROM scripts WHERE id = ?').run(id);
});
tx(scriptId);
}
// Servers that match a script's scope. Empty for unscoped scripts — those are