Fix scope restoration for promoted scripts
This commit is contained in:
@@ -57,7 +57,6 @@ export class ScriptError extends Error {
|
||||
| 'path-empty'
|
||||
| 'no-such-script'
|
||||
| 'no-such-version'
|
||||
| 'scope-requires-beta'
|
||||
| 'invalid-scope'
|
||||
| 'in-use-current'
|
||||
| 'in-use-beta'
|
||||
@@ -290,21 +289,24 @@ export interface SetScopeInput {
|
||||
scopeValue: string | null;
|
||||
}
|
||||
|
||||
// Setting or changing a scope while the script already has a promoted version
|
||||
// requires a beta first (per plan §"Beta trigger"). Un-scoping is free.
|
||||
// Scope edits preserve the promoted version and do not deploy files. An active
|
||||
// beta must finish first because promotion reads the script's current scope.
|
||||
export function setScope(input: SetScopeInput): ScriptRow {
|
||||
const script = getScript(input.scriptId);
|
||||
if (!script) throw new ScriptError(`no such script (id=${input.scriptId})`, 'no-such-script');
|
||||
validateScope(input.scopeType, input.scopeValue);
|
||||
|
||||
const changed =
|
||||
input.scopeType !== script.scope_type || (input.scopeValue ?? null) !== (script.scope_value ?? null);
|
||||
const isSetOrChange = input.scopeType !== null && changed;
|
||||
|
||||
if (isSetOrChange && script.current_version_id !== null) {
|
||||
const activeBeta = db()
|
||||
.prepare(
|
||||
`SELECT b.id FROM betas b
|
||||
JOIN beta_scripts bs ON bs.beta_id = b.id
|
||||
WHERE bs.script_id = ? AND b.state IN ('running','ready-promote') LIMIT 1`
|
||||
)
|
||||
.get(input.scriptId) as { id: number } | undefined;
|
||||
if (activeBeta) {
|
||||
throw new ScriptError(
|
||||
'setting or changing scope requires opening a beta first',
|
||||
'scope-requires-beta'
|
||||
`rollback or promote beta ${activeBeta.id} before changing scope`,
|
||||
'has-active-beta'
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user