Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
-- SHA-2492: closed_at MUST pair with a closed status (archived/cleanup_failed).
--
-- Added with NOT VALID so the migration is fast and does not lock the table
-- while validating every existing row. The constraint takes effect for all
-- INSERTs and UPDATEs immediately. Existing violator rows (1037 known on
-- prod paperclip DB at filing time) must be backfilled to status='archived'
-- before running `ALTER TABLE ... VALIDATE CONSTRAINT
-- execution_workspaces_closed_state_consistency` as a follow-up.
ALTER TABLE "execution_workspaces"
ADD CONSTRAINT "execution_workspaces_closed_state_consistency"
CHECK ("closed_at" IS NULL OR "status" IN ('archived', 'cleanup_failed'))
NOT VALID;
7 changes: 7 additions & 0 deletions packages/db/src/migrations/meta/_journal.json
Original file line number Diff line number Diff line change
Expand Up @@ -540,6 +540,13 @@
"when": 1776541198667,
"tag": "0076_budget_incidents_dedupe_open_only",
"breakpoints": true
},
{
"idx": 77,
"version": "7",
"when": 1778800000000,
"tag": "0077_workspace_closed_state_consistency",
"breakpoints": true
}
]
}
7 changes: 7 additions & 0 deletions packages/db/src/schema/execution_workspaces.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { sql } from "drizzle-orm";
import {
type AnyPgColumn,
check,
index,
jsonb,
pgTable,
Expand Down Expand Up @@ -64,5 +66,10 @@ export const executionWorkspaces = pgTable(
table.companyId,
table.branchName,
),
// closed_at MUST pair with a closed status. See SHA-2492.
closedStateConsistency: check(
"execution_workspaces_closed_state_consistency",
sql`${table.closedAt} IS NULL OR ${table.status} IN ('archived', 'cleanup_failed')`,
),
}),
);