Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.
Go
fix(db): make issue property migrations deploy-safe (#5456)
Multica Evef46d5d7ba55f
diff --git a/server/internal/handler/workspace_test.go b/server/internal/handler/workspace_test.goindex bc87fe2b37..f7042185a9 100644--- a/server/internal/handler/workspace_test.go+++ b/server/internal/handler/workspace_test.go@@ -209,6 +209,17 @@ VALUES ($1, 123456789, 'multica-ai', 'multica', 3366, 987654321, 'abc123', 15368`, wsID); err != nil {t.Fatalf("create pending check suite: %v", err)}+ var propertyID string+ if err := testPool.QueryRow(ctx, `+INSERT INTO issue_property (workspace_id, name, type)+VALUES ($1, 'Delete cleanup property', 'text')+RETURNING id+`, wsID).Scan(&propertyID); err != nil {+ t.Fatalf("create issue property: %v", err)+ }+ t.Cleanup(func() {+ _, _ = testPool.Exec(context.Background(), `DELETE FROM issue_property WHERE id = $1`, propertyID)+ })w := httptest.NewRecorder()req := newRequest("DELETE", "/api/workspaces/"+wsID, nil)@@ -234,6 +245,14 @@ VALUES ($1, 123456789, 'multica-ai', 'multica', 3366, 987654321, 'abc123', 15368if pendingCount != 0 {t.Fatalf("pending check suites were not cleaned up for deleted workspace: %d", pendingCount)}++ var propertyCount int+ if err := testPool.QueryRow(ctx, `SELECT COUNT(*) FROM issue_property WHERE id = $1`, propertyID).Scan(&propertyCount); err != nil {+ t.Fatalf("verify issue property cleanup: %v", err)+ }+ if propertyCount != 0 {+ t.Fatalf("issue properties were not cleaned up for deleted workspace: %d", propertyCount)+ }}// TestUpdateWorkspace_AvatarURL covers the avatar_url field added todiff --git a/server/migrations/191_issue_properties.up.sql b/server/migrations/191_issue_properties.up.sqlindex d5b5ec9886..ac92fb8a3c 100644--- a/server/migrations/191_issue_properties.up.sql+++ b/server/migrations/191_issue_properties.up.sql@@ -14,7 +14,7 @@CREATE TABLE issue_property (id UUID PRIMARY KEY DEFAULT gen_random_uuid(),- workspace_id UUID NOT NULL REFERENCES workspace(id) ON DELETE CASCADE,+ workspace_id UUID NOT NULL,name TEXT NOT NULL,type TEXT NOT NULL CHECK (type IN ('text', 'number', 'select', 'multi_select', 'date', 'checkbox', 'url')),description TEXT NOT NULL DEFAULT '',@@ -26,9 +26,6 @@ CREATE TABLE issue_property (updated_at TIMESTAMPTZ NOT NULL DEFAULT now());-CREATE UNIQUE INDEX idx_issue_property_ws_name ON issue_property (workspace_id, LOWER(name));-CREATE INDEX idx_issue_property_workspace ON issue_property (workspace_id);-ALTER TABLE issue ADD COLUMN properties JSONB NOT NULL DEFAULT '{}'::jsonb;-- NOT VALID + VALIDATE keeps the ACCESS EXCLUSIVE lock instantaneous; the-- validation pass then runs under SHARE UPDATE EXCLUSIVE without blocking@@ -41,7 +38,6 @@ ALTER TABLE issue VALIDATE CONSTRAINT issue_properties_is_object;ALTER TABLE issue ADD CONSTRAINT issue_properties_size_limitCHECK (pg_column_size(properties) <= 16384) NOT VALID;ALTER TABLE issue VALIDATE CONSTRAINT issue_properties_size_limit;--- The GIN index on issue.properties lives in the follow-up migration--- (192_issue_properties_gin_index): CREATE INDEX CONCURRENTLY cannot share a--- migration with other statements, and a plain CREATE INDEX would lock writes--- on the hot issue table for the duration of the build.+-- All indexes live in follow-up, single-statement migrations because CREATE+-- INDEX CONCURRENTLY cannot share a migration with other statements. The GIN+-- index is in 192; the issue_property indexes are in 194 and 195.diff --git a/server/migrations/193_issue_property_drop_workspace_fk.down.sql b/server/migrations/193_issue_property_drop_workspace_fk.down.sqlnew file mode 100644index 0000000000..6d6f433576--- /dev/null+++ b/server/migrations/193_issue_property_drop_workspace_fk.down.sql@@ -0,0 +1,2 @@+-- Deliberately do not restore the pre-release foreign key. Workspace deletion+-- remains application-owned across both migration directions.diff --git a/server/migrations/193_issue_property_drop_workspace_fk.up.sql b/server/migrations/193_issue_property_drop_workspace_fk.up.sqlnew file mode 100644index 0000000000..a1c63f5871--- /dev/null+++ b/server/migrations/193_issue_property_drop_workspace_fk.up.sql@@ -0,0 +1,5 @@+-- A short-lived pre-release build created this foreign key in migration 191.+-- Fresh databases no longer create it; this idempotent cleanup brings databases+-- that applied the earlier migration back to the application-owned lifecycle.+ALTER TABLE issue_property+ DROP CONSTRAINT IF EXISTS issue_property_workspace_id_fkey;diff --git a/server/migrations/194_issue_property_workspace_name_index.down.sql b/server/migrations/194_issue_property_workspace_name_index.down.sqlnew file mode 100644index 0000000000..86d7955d27--- /dev/null+++ b/server/migrations/194_issue_property_workspace_name_index.down.sql@@ -0,0 +1 @@+DROP INDEX CONCURRENTLY IF EXISTS idx_issue_property_ws_name;diff --git a/server/migrations/194_issue_property_workspace_name_index.up.sql b/server/migrations/194_issue_property_workspace_name_index.up.sqlnew file mode 100644index 0000000000..c1c4645d15--- /dev/null+++ b/server/migrations/194_issue_property_workspace_name_index.up.sql@@ -0,0 +1,5 @@+-- Enforce case-insensitive property-name uniqueness without blocking writes.+-- Keep this as the migration's only statement: PostgreSQL rejects CREATE INDEX+-- CONCURRENTLY inside a transaction or multi-command string.+CREATE UNIQUE INDEX CONCURRENTLY IF NOT EXISTS idx_issue_property_ws_name+ ON issue_property (workspace_id, LOWER(name));diff --git a/server/migrations/195_issue_property_workspace_index.down.sql b/server/migrations/195_issue_property_workspace_index.down.sqlnew file mode 100644index 0000000000..ab0e65a6af--- /dev/null+++ b/server/migrations/195_issue_property_workspace_index.down.sql@@ -0,0 +1 @@+DROP INDEX CONCURRENTLY IF EXISTS idx_issue_property_workspace;diff --git a/server/migrations/195_issue_property_workspace_index.up.sql b/server/migrations/195_issue_property_workspace_index.up.sqlnew file mode 100644index 0000000000..13ea58b333--- /dev/null+++ b/server/migrations/195_issue_property_workspace_index.up.sql@@ -0,0 +1,5 @@+-- Support workspace-scoped property listing and counts without blocking writes.+-- Keep this as the migration's only statement: PostgreSQL rejects CREATE INDEX+-- CONCURRENTLY inside a transaction or multi-command string.+CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_issue_property_workspace+ ON issue_property (workspace_id);diff --git a/server/pkg/db/generated/workspace.sql.go b/server/pkg/db/generated/workspace.sql.goindex 4ce02976ef..456c0e34d4 100644--- a/server/pkg/db/generated/workspace.sql.go+++ b/server/pkg/db/generated/workspace.sql.go@@ -110,16 +110,20 @@ cleared_binding_tokens AS (cleared_installations AS (DELETE FROM channel_installation WHERE workspace_id = $1),+cleared_issue_properties AS (+ DELETE FROM issue_property WHERE workspace_id = $1+),deleted_pending_check_suites AS (DELETE FROM github_pending_check_suite WHERE workspace_id = $1)DELETE FROM workspace WHERE workspace.id = $1`-// The channel_* tables (MUL-3515 §4) and resource-label junctions carry NO FK to-// workspace, so — unlike the CASCADE-backed tables the DELETE below sweeps —-// they are not cleaned up implicitly. Remove their workspace-owned rows here so-// they commit or roll back atomically with the workspace row.+// The channel_* tables (MUL-3515 §4), resource-label junctions, and custom issue+// property definitions carry NO FK to workspace, so — unlike the CASCADE-backed+// tables the DELETE below sweeps — they are not cleaned up implicitly. Remove+// their workspace-owned rows here so they commit or roll back atomically with+// the workspace row.func (q *Queries) DeleteWorkspace(ctx context.Context, id pgtype.UUID) error {_, err := q.db.Exec(ctx, deleteWorkspace, id)return errdiff --git a/server/pkg/db/queries/workspace.sql b/server/pkg/db/queries/workspace.sqlindex 926dd2abde..e8d65adff5 100644--- a/server/pkg/db/queries/workspace.sql+++ b/server/pkg/db/queries/workspace.sql@@ -91,10 +91,11 @@ SELECT id FROM workspace WHERE id = $1 FOR UPDATE;SELECT id FROM workspace WHERE id = $1 FOR KEY SHARE;-- name: DeleteWorkspace :exec--- The channel_* tables (MUL-3515 §4) and resource-label junctions carry NO FK to--- workspace, so — unlike the CASCADE-backed tables the DELETE below sweeps —--- they are not cleaned up implicitly. Remove their workspace-owned rows here so--- they commit or roll back atomically with the workspace row.+-- The channel_* tables (MUL-3515 §4), resource-label junctions, and custom issue+-- property definitions carry NO FK to workspace, so — unlike the CASCADE-backed+-- tables the DELETE below sweeps — they are not cleaned up implicitly. Remove+-- their workspace-owned rows here so they commit or roll back atomically with+-- the workspace row.WITH ws_installations AS (SELECT id FROM channel_installation WHERE workspace_id = $1),@@ -152,6 +153,9 @@ cleared_binding_tokens AS (cleared_installations AS (DELETE FROM channel_installation WHERE workspace_id = $1),+cleared_issue_properties AS (+ DELETE FROM issue_property WHERE workspace_id = $1+),deleted_pending_check_suites AS (DELETE FROM github_pending_check_suite WHERE workspace_id = $1)