Skip to content

Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.

Go

fix(migrations): add the Triage column CHECK without scanning the issue table (#8449)

Bohan Jiangdd785a5cd818
diff --git a/server/internal/migrations/issue_triage_state_migration_test.go b/server/internal/migrations/issue_triage_state_migration_test.go
new file mode 100644
index 0000000000..cbafa67a4b
--- /dev/null
+++ b/server/internal/migrations/issue_triage_state_migration_test.go
@@ -0,0 +1,113 @@
+package migrations
+
+import (
+ "context"
+ "testing"
+)
+
+// TestIssueTriageStateMigration pins the lock shape of the Triage column: 483
+// registers the CHECK without validating it, so its ACCESS EXCLUSIVE lock never
+// covers a scan of the issue table, and 485 runs that scan afterwards under
+// SHARE UPDATE EXCLUSIVE, which readers and writers pass straight through.
+// Skipping the scan is only acceptable while both halves still reject an
+// unknown triage_state on every write, so each step asserts that too.
+func TestIssueTriageStateMigration(t *testing.T) {
+ ctx := context.Background()
+ s := newTriageMigrationSandbox(t, "issue_triage_state_migration_test")
+
+ // Rows that exist before the column does are exactly what a validated CHECK
+ // would have forced the ALTER to scan.
+ if _, err := s.pool.Exec(ctx, `
+ INSERT INTO issue (workspace_id, title, status) VALUES
+ ($1, 'existing issue', 'todo'),
+ ($1, 'another existing issue', 'in_progress')
+ `, triageWSClean); err != nil {
+ t.Fatalf("seed pre-existing issues: %v", err)
+ }
+
+ applyMigrationFile(t, ctx, s.pool, "483_issue_triage_state.up.sql")
+
+ if s.triageStateValidated(t, ctx) {
+ t.Error("483 validated the CHECK, scanning the issue table under ACCESS EXCLUSIVE")
+ }
+ if hasDefault, notNull := s.triageStateColumn(t, ctx); hasDefault || notNull {
+ t.Errorf("483 added triage_state with default=%v not_null=%v, want a nullable column with no default so the add stays catalog-only", hasDefault, notNull)
+ }
+ s.assertTriageStateEnforced(t, ctx, "483")
+
+ applyMigrationFile(t, ctx, s.pool, "485_issue_triage_state_validate.up.sql")
+
+ if !s.triageStateValidated(t, ctx) {
+ t.Error("485 left the CHECK NOT VALID")
+ }
+ s.assertTriageStateEnforced(t, ctx, "485")
+
+ // Rolling back the validation cannot restore a validated constraint, so it
+ // has to leave the state 483 produced rather than no constraint at all.
+ applyMigrationFile(t, ctx, s.pool, "485_issue_triage_state_validate.down.sql")
+
+ if s.triageStateValidated(t, ctx) {
+ t.Error("485 down left the CHECK validated")
+ }
+ s.assertTriageStateEnforced(t, ctx, "485 down")
+
+ applyMigrationFile(t, ctx, s.pool, "483_issue_triage_state.down.sql")
+
+ var columnExists bool
+ if err := s.pool.QueryRow(ctx, `
+ SELECT EXISTS (
+ SELECT 1 FROM pg_attribute
+ WHERE attrelid = (quote_ident($1) || '.issue')::regclass
+ AND attname = 'triage_state'
+ AND NOT attisdropped
+ )
+ `, s.schema).Scan(&columnExists); err != nil {
+ t.Fatalf("read triage_state column after rollback: %v", err)
+ }
+ if columnExists {
+ t.Error("483 down left triage_state behind")
+ }
+}
+
+// assertTriageStateEnforced checks that the constraint governs new writes at
+// this point in the sequence — the guarantee NOT VALID keeps and the reason
+// deferring the scan costs nothing.
+func (s *triageMigrationSandbox) assertTriageStateEnforced(t *testing.T, ctx context.Context, step string) {
+ t.Helper()
+ assertInsertCheckViolation(t, ctx, s.pool, `
+ INSERT INTO issue (workspace_id, title, status, triage_state) VALUES ($1, 'rejected', 'todo', 'bogus')
+ `, triageWSClean)
+ if _, err := s.pool.Exec(ctx, `
+ INSERT INTO issue (workspace_id, title, status, triage_state) VALUES
+ ($1, 'in triage', 'todo', 'pending'),
+ ($1, 'ordinary issue', 'todo', NULL)
+ `, triageWSClean); err != nil {
+ t.Fatalf("insert known triage_state values after %s: %v", step, err)
+ }
+}
+
+func (s *triageMigrationSandbox) triageStateValidated(t *testing.T, ctx context.Context) bool {
+ t.Helper()
+ var validated bool
+ if err := s.pool.QueryRow(ctx, `
+ SELECT convalidated FROM pg_constraint
+ WHERE conname = 'issue_triage_state_known'
+ AND conrelid = (quote_ident($1) || '.issue')::regclass
+ `, s.schema).Scan(&validated); err != nil {
+ t.Fatalf("read triage state constraint: %v", err)
+ }
+ return validated
+}
+
+func (s *triageMigrationSandbox) triageStateColumn(t *testing.T, ctx context.Context) (hasDefault, notNull bool) {
+ t.Helper()
+ if err := s.pool.QueryRow(ctx, `
+ SELECT atthasdef, attnotnull FROM pg_attribute
+ WHERE attrelid = (quote_ident($1) || '.issue')::regclass
+ AND attname = 'triage_state'
+ AND NOT attisdropped
+ `, s.schema).Scan(&hasDefault, &notNull); err != nil {
+ t.Fatalf("read triage_state column: %v", err)
+ }
+ return hasDefault, notNull
+}
diff --git a/server/migrations/483_issue_triage_state.up.sql b/server/migrations/483_issue_triage_state.up.sql
index e0d7148614..c9c768321e 100644
--- a/server/migrations/483_issue_triage_state.up.sql
+++ b/server/migrations/483_issue_triage_state.up.sql
@@ -14,6 +14,21 @@
-- records (declined, merged into another issue) are their own sub-issue and
-- extend this CHECK when they land; keeping the constraint narrow now is what
-- makes that a deliberate change rather than a silent typo.
+--
+-- The CHECK is added NOT VALID: a validated CHECK scans every existing issue
+-- while holding the ALTER's ACCESS EXCLUSIVE lock, which stops all issue reads
+-- and writes for the length of that scan even though the new column needs no
+-- backfill. NOT VALID enforces the constraint on every write from this point
+-- on and skips the scan; migration 485 validates the rows already on disk under
+-- SHARE UPDATE EXCLUSIVE, which readers and writers run straight through.
+--
+-- The runner sends this file as one implicit transaction. Bound lock
+-- acquisition and execution so this catalog-only change fails fast and retries
+-- on the next run rather than parking a pending ACCESS EXCLUSIVE lock in front
+-- of every issue query.
+SET LOCAL lock_timeout = '2s';
+SET LOCAL statement_timeout = '10s';
+
ALTER TABLE issue
ADD COLUMN triage_state TEXT,
- ADD CONSTRAINT issue_triage_state_known CHECK (triage_state IS NULL OR triage_state IN ('pending'));
+ ADD CONSTRAINT issue_triage_state_known CHECK (triage_state IS NULL OR triage_state IN ('pending')) NOT VALID;
diff --git a/server/migrations/485_issue_triage_state_validate.down.sql b/server/migrations/485_issue_triage_state_validate.down.sql
new file mode 100644
index 0000000000..5eca15294f
--- /dev/null
+++ b/server/migrations/485_issue_triage_state_validate.down.sql
@@ -0,0 +1,7 @@
+-- PostgreSQL cannot mark a validated constraint NOT VALID again, so recreate
+-- the state migration 483 left: enforcing every new write, not yet validated.
+-- Both statements are catalog-only, so the ACCESS EXCLUSIVE lock is held only
+-- long enough to swap the catalog rows.
+ALTER TABLE issue DROP CONSTRAINT IF EXISTS issue_triage_state_known;
+ALTER TABLE issue ADD CONSTRAINT issue_triage_state_known
+ CHECK (triage_state IS NULL OR triage_state IN ('pending')) NOT VALID;
diff --git a/server/migrations/485_issue_triage_state_validate.up.sql b/server/migrations/485_issue_triage_state_validate.up.sql
new file mode 100644
index 0000000000..a8ee60dce5
--- /dev/null
+++ b/server/migrations/485_issue_triage_state_validate.up.sql
@@ -0,0 +1,8 @@
+-- Validate the CHECK migration 483 added NOT VALID.
+--
+-- Split out so the scan runs under SHARE UPDATE EXCLUSIVE — readers and writers
+-- continue — instead of inheriting migration 483's ACCESS EXCLUSIVE lock, which
+-- would stop the issue table for the length of the scan. Migration 483 added
+-- the column with no default and no backfill, so every row this scan reads is
+-- NULL: the work is marking the constraint trusted, not repairing data.
+ALTER TABLE issue VALIDATE CONSTRAINT issue_triage_state_known;