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:
@@ -1,12 +1,24 @@
|
||||
import { json } from '@sveltejs/kit';
|
||||
import { getScript, setScope, ScriptError, type ScopeType } from '$lib/server/fleet/scripts.js';
|
||||
import { unsetScopeAndCleanup } from '$lib/server/fleet/beta.js';
|
||||
import { runDriftDeployment } from '$lib/server/fleet/drift.js';
|
||||
import { kick } from '$lib/server/pipeline/worker.js';
|
||||
|
||||
function parseId(raw: string): number | null {
|
||||
const n = Number.parseInt(raw, 10);
|
||||
return Number.isFinite(n) ? n : null;
|
||||
}
|
||||
|
||||
// Fire-and-forget: after a scope mutation, reconcile any drift the change may
|
||||
// have introduced (orphan files on servers that no longer match, or missing
|
||||
// files on servers that now do). Runs in the background so the API returns
|
||||
// promptly; the deployment will surface on /deployments if there's real drift.
|
||||
function triggerDrift(): void {
|
||||
void runDriftDeployment({ triggeredBy: 'ui' })
|
||||
.then((result) => { if (result.deploymentId) void kick(); })
|
||||
.catch(() => { /* surfaced on /deployments if it fails */ });
|
||||
}
|
||||
|
||||
// POST body: { scopeType: 'game' | 'map' | null, scopeValue?: string }
|
||||
// Setting/changing scope on a script with a current_version_id returns a
|
||||
// 409 with kind='scope-requires-beta'; unsetting cleans the file off matching
|
||||
@@ -28,9 +40,11 @@ export async function POST({ params, request }: { params: { id: string }; reques
|
||||
try {
|
||||
if (scopeType === null) {
|
||||
const cleaned = await unsetScopeAndCleanup(scriptId);
|
||||
triggerDrift();
|
||||
return json({ ok: true, cleaned });
|
||||
}
|
||||
const updated = setScope({ scriptId, scopeType, scopeValue });
|
||||
triggerDrift();
|
||||
return json({ ok: true, script: updated });
|
||||
} catch (err) {
|
||||
if (err instanceof ScriptError) {
|
||||
|
||||
Reference in New Issue
Block a user