Skip to content

Commit 6ae71ae

Browse files
chore(db): squash managed MCP migration
1 parent d1ac994 commit 6ae71ae

6 files changed

Lines changed: 146 additions & 57 deletions

packages/db/migrations/0315_credential_group_managed_mcp.sql

Lines changed: 0 additions & 3 deletions
This file was deleted.

packages/db/migrations/0316_credential_group_managed_mcp_constraints.sql

Lines changed: 0 additions & 47 deletions
This file was deleted.
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
-- Pure expand: every new column is nullable, and no managed_mcp row can predate this migration.
2+
-- Every pre-COMMIT statement is replay-safe because a concurrent index failure leaves this file
3+
-- unjournaled while preserving the committed schema changes.
4+
ALTER TYPE "public"."credential_type" ADD VALUE IF NOT EXISTS 'managed_mcp' BEFORE 'env_workspace';--> statement-breakpoint
5+
ALTER TABLE "credential" ADD COLUMN IF NOT EXISTS "mcp_server_id" text;--> statement-breakpoint
6+
ALTER TABLE "credential" ADD COLUMN IF NOT EXISTS "mcp_tools" jsonb;--> statement-breakpoint
7+
ALTER TABLE "credential" ADD COLUMN IF NOT EXISTS "mcp_tools_refreshed_at" timestamp;--> statement-breakpoint
8+
ALTER TABLE "mcp_servers" ADD COLUMN IF NOT EXISTS "credential_group_id" text;--> statement-breakpoint
9+
10+
-- PostgreSQL has no ADD CONSTRAINT IF NOT EXISTS, so replay guards are scoped to each table.
11+
-- NOT VALID keeps foreign-key installation to a metadata change before validation.
12+
DO $$ BEGIN
13+
IF NOT EXISTS (
14+
SELECT 1 FROM "pg_constraint"
15+
WHERE "conname" = 'credential_mcp_server_id_mcp_servers_id_fk'
16+
AND "conrelid" = '"credential"'::regclass
17+
) THEN
18+
ALTER TABLE "credential" ADD CONSTRAINT "credential_mcp_server_id_mcp_servers_id_fk" FOREIGN KEY ("mcp_server_id") REFERENCES "public"."mcp_servers"("id") ON DELETE cascade ON UPDATE no action NOT VALID;
19+
END IF;
20+
END $$;--> statement-breakpoint
21+
ALTER TABLE "credential" VALIDATE CONSTRAINT "credential_mcp_server_id_mcp_servers_id_fk";--> statement-breakpoint
22+
DO $$ BEGIN
23+
IF NOT EXISTS (
24+
SELECT 1 FROM "pg_constraint"
25+
WHERE "conname" = 'mcp_servers_credential_group_id_credential_group_id_fk'
26+
AND "conrelid" = '"mcp_servers"'::regclass
27+
) THEN
28+
ALTER TABLE "mcp_servers" ADD CONSTRAINT "mcp_servers_credential_group_id_credential_group_id_fk" FOREIGN KEY ("credential_group_id") REFERENCES "public"."credential_group"("id") ON DELETE set null ON UPDATE no action NOT VALID;
29+
END IF;
30+
END $$;--> statement-breakpoint
31+
ALTER TABLE "mcp_servers" VALIDATE CONSTRAINT "mcp_servers_credential_group_id_credential_group_id_fk";--> statement-breakpoint
32+
33+
DO $$ BEGIN
34+
IF NOT EXISTS (
35+
SELECT 1 FROM "pg_constraint"
36+
WHERE "conname" = 'credential_managed_mcp_source_check'
37+
AND "conrelid" = '"credential"'::regclass
38+
) THEN
39+
ALTER TABLE "credential" ADD CONSTRAINT "credential_managed_mcp_source_check" CHECK ((type::text <> 'managed_mcp') OR (
40+
id LIKE 'mcp-cg-%'
41+
AND account_id IS NULL
42+
AND provider_id IS NULL
43+
AND authorization_app_id IS NULL
44+
AND credential_group_enrollment_id IS NOT NULL
45+
AND credential_group_option_id IS NULL
46+
AND mcp_server_id IS NOT NULL
47+
AND managed_oauth_status IS NOT NULL
48+
AND (managed_oauth_status <> 'active' OR (
49+
encrypted_oauth_token_set IS NOT NULL
50+
AND mcp_tools IS NOT NULL
51+
))
52+
AND granted_at IS NOT NULL
53+
AND managed_oauth_scope_version IS NULL
54+
AND provider_subject_id IS NULL
55+
AND provider_tenant_id IS NULL
56+
AND granted_scopes IS NULL
57+
AND provider_metadata IS NULL
58+
AND created_by IS NULL
59+
AND env_key IS NULL
60+
AND env_owner_user_id IS NULL
61+
AND encrypted_service_account_key IS NULL
62+
AND unredacted = false
63+
)) NOT VALID;
64+
END IF;
65+
END $$;--> statement-breakpoint
66+
ALTER TABLE "credential" VALIDATE CONSTRAINT "credential_managed_mcp_source_check";--> statement-breakpoint
67+
DO $$ BEGIN
68+
IF NOT EXISTS (
69+
SELECT 1 FROM "pg_constraint"
70+
WHERE "conname" = 'credential_creator_source_check'
71+
AND "conrelid" = '"credential"'::regclass
72+
) THEN
73+
ALTER TABLE "credential" ADD CONSTRAINT "credential_creator_source_check" CHECK ((type::text = 'managed_mcp') OR created_by IS NOT NULL) NOT VALID;
74+
END IF;
75+
END $$;--> statement-breakpoint
76+
ALTER TABLE "credential" VALIDATE CONSTRAINT "credential_creator_source_check";--> statement-breakpoint
77+
ALTER TABLE "credential" ALTER COLUMN "created_by" DROP NOT NULL;--> statement-breakpoint
78+
79+
-- The commit makes the new enum label visible and moves index builds outside the migration
80+
-- runner's transaction, as required by PostgreSQL for the partial and concurrent indexes.
81+
COMMIT;--> statement-breakpoint
82+
SET lock_timeout = 0;--> statement-breakpoint
83+
-- A failed concurrent build leaves an invalid index behind, so each replay removes it first.
84+
DROP INDEX CONCURRENTLY IF EXISTS "credential_mcp_server_idx";--> statement-breakpoint
85+
CREATE INDEX CONCURRENTLY IF NOT EXISTS "credential_mcp_server_idx" ON "credential" USING btree ("mcp_server_id");--> statement-breakpoint
86+
DROP INDEX CONCURRENTLY IF EXISTS "credential_managed_mcp_enrollment_server_unique";--> statement-breakpoint
87+
CREATE UNIQUE INDEX CONCURRENTLY IF NOT EXISTS "credential_managed_mcp_enrollment_server_unique" ON "credential" USING btree ("credential_group_enrollment_id","mcp_server_id") WHERE "credential"."type" = 'managed_mcp';--> statement-breakpoint
88+
DROP INDEX CONCURRENTLY IF EXISTS "mcp_servers_credential_group_idx";--> statement-breakpoint
89+
CREATE INDEX CONCURRENTLY IF NOT EXISTS "mcp_servers_credential_group_idx" ON "mcp_servers" USING btree ("credential_group_id");--> statement-breakpoint
90+
SET lock_timeout = '5s';

packages/db/migrations/0317_managed_mcp_external_identity.sql

Lines changed: 0 additions & 3 deletions
This file was deleted.

packages/db/migrations/meta/0317_snapshot.json

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
2-
"id": "3e774f5a-401e-40b7-a59e-c97238a721b9",
3-
"prevId": "cfa8e676-67d0-48f3-97ea-c5a3c3dfa594",
2+
"id": "7d3eae54-bbe1-4cdb-b9d6-bf430c8396c6",
3+
"prevId": "1fafbd78-3e0f-4cc8-9a51-053a3b11af9e",
44
"version": "7",
55
"dialect": "postgresql",
66
"tables": {
@@ -13037,6 +13037,12 @@
1303713037
"primaryKey": false,
1303813038
"notNull": false
1303913039
},
13040+
"capability_governed_user_id": {
13041+
"name": "capability_governed_user_id",
13042+
"type": "text",
13043+
"primaryKey": false,
13044+
"notNull": false
13045+
},
1304013046
"enrichment_details": {
1304113047
"name": "enrichment_details",
1304213048
"type": "jsonb",
@@ -13130,6 +13136,15 @@
1313013136
"columnsTo": ["id"],
1313113137
"onDelete": "cascade",
1313213138
"onUpdate": "no action"
13139+
},
13140+
"table_row_executions_capability_governed_user_id_user_id_fk": {
13141+
"name": "table_row_executions_capability_governed_user_id_user_id_fk",
13142+
"tableFrom": "table_row_executions",
13143+
"tableTo": "user",
13144+
"columnsFrom": ["capability_governed_user_id"],
13145+
"columnsTo": ["id"],
13146+
"onDelete": "set null",
13147+
"onUpdate": "no action"
1313313148
}
1313413149
},
1313513150
"compositePrimaryKeys": {
@@ -13223,6 +13238,12 @@
1322313238
"primaryKey": false,
1322413239
"notNull": false
1322513240
},
13241+
"capability_governed_user_id": {
13242+
"name": "capability_governed_user_id",
13243+
"type": "text",
13244+
"primaryKey": false,
13245+
"notNull": false
13246+
},
1322613247
"requested_at": {
1322713248
"name": "requested_at",
1322813249
"type": "timestamp",
@@ -13291,6 +13312,28 @@
1329113312
"concurrently": false,
1329213313
"method": "btree",
1329313314
"with": {}
13315+
},
13316+
"table_run_dispatches_governed_active_idx": {
13317+
"name": "table_run_dispatches_governed_active_idx",
13318+
"columns": [
13319+
{
13320+
"expression": "capability_governed_user_id",
13321+
"isExpression": false,
13322+
"asc": true,
13323+
"nulls": "last"
13324+
},
13325+
{
13326+
"expression": "status",
13327+
"isExpression": false,
13328+
"asc": true,
13329+
"nulls": "last"
13330+
}
13331+
],
13332+
"isUnique": false,
13333+
"where": "\"table_run_dispatches\".\"status\" IN ('pending', 'dispatching')",
13334+
"concurrently": false,
13335+
"method": "btree",
13336+
"with": {}
1329413337
}
1329513338
},
1329613339
"foreignKeys": {
@@ -13320,6 +13363,15 @@
1332013363
"columnsTo": ["id"],
1332113364
"onDelete": "set null",
1332213365
"onUpdate": "no action"
13366+
},
13367+
"table_run_dispatches_capability_governed_user_id_user_id_fk": {
13368+
"name": "table_run_dispatches_capability_governed_user_id_user_id_fk",
13369+
"tableFrom": "table_run_dispatches",
13370+
"tableTo": "user",
13371+
"columnsFrom": ["capability_governed_user_id"],
13372+
"columnsTo": ["id"],
13373+
"onDelete": "set null",
13374+
"onUpdate": "no action"
1332313375
}
1332413376
},
1332513377
"compositePrimaryKeys": {},

packages/db/migrations/meta/_journal.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2217,8 +2217,8 @@
22172217
{
22182218
"idx": 317,
22192219
"version": "7",
2220-
"when": 1788256060672,
2221-
"tag": "0317_managed_mcp_external_identity",
2220+
"when": 1788284109673,
2221+
"tag": "0317_credential_group_managed_mcp",
22222222
"breakpoints": true
22232223
}
22242224
]

0 commit comments

Comments
 (0)