History

Replay the legacy v1 schema chain before the v1->v2 data migration (#927)

A database last touched by a release older than v1-final crashed 1.5.0 startup with "no such table: plugin_storage": the v1->v2 data migration reads the final v1 schema, and the legacy drizzle replay that used to bring old databases forward was dropped in the rework. Vendor the v1 migration chain (apps/local/drizzle-legacy-v1, embedded into the binary) and replay it when the database's drizzle history is a prefix of the bundled chain, before reading the v1 snapshot.

Rhys Sullivan committed 5 months ago

Commit 059eb231 parent 346cf8918 files · +2804 −6Browse files

Unified · Split

18 files
  1. .changeset/legacy-v1-migration-replay.md +5 −0
  2. apps/cli/src/build.ts +5 −3
  3. apps/local/drizzle-legacy-v1/0000_overconfident_sharon_carter.sql +180 −0
  4. apps/local/drizzle-legacy-v1/0001_sour_catseye.sql +1 −0
  5. apps/local/drizzle-legacy-v1/0002_lively_sue_storm.sql +20 −0
  6. apps/local/drizzle-legacy-v1/0003_little_silk_fever.sql +16 −0
  7. apps/local/drizzle-legacy-v1/0004_add_tool_policy.sql +17 −0
  8. apps/local/drizzle-legacy-v1/0005_repair_mcp_oauth_session.sql +10 −0
  9. apps/local/drizzle-legacy-v1/0006_neat_terror.sql +23 −0
  10. apps/local/drizzle-legacy-v1/0007_normalize_plugin_secret_refs.sql +579 −0
  11. apps/local/drizzle-legacy-v1/0008_scoped_credentials_cutover.sql +1220 −0
  12. apps/local/drizzle-legacy-v1/0009_repair_openapi_oauth_cutover_residue.sql +253 −0
  13. apps/local/drizzle-legacy-v1/0010_add_credential_binding_secret_scope.sql +2 −0
  14. apps/local/drizzle-legacy-v1/0011_plugin_storage_sources.sql +215 −0
  15. apps/local/drizzle-legacy-v1/0012_connection_identity_override.sql +1 −0
  16. apps/local/drizzle-legacy-v1/meta/_journal.json +97 −0
  17. apps/local/src/db/v1-v2-migration.test.ts +79 −0
  18. apps/local/src/db/v1-v2-migration.ts +81 −3
.changeset/legacy-v1-migration-replay.md+5−0
@@ -0,0 +1,5 @@
1+---
2+"executor": patch
3+---
4+
5+Fix `executor web` crashing with `no such table: plugin_storage` when upgrading from an older v1 release. The v1 → v2 data migration now replays the bundled legacy schema migrations first, so databases last touched by any pre-1.5 version are brought up to the final v1 schema before their data is migrated.
apps/cli/src/build.ts+5−3
@@ -252,13 +252,15 @@ const createEmbeddedWebUISource = async (mode: BuildMode) => { · 251 unmodified lines
252252 };
253253
254254 // ---------------------------------------------------------------------------
255−// Embedded drizzle migrations — inlined as text imports so drizzle's
255+// Embedded legacy v1 drizzle migrations — inlined as text imports so drizzle's
256256 // `migrate()` (which reads a folder from disk) can be given a tmpdir
257−// populated from the inlined contents at runtime.
257+// populated from the inlined contents at runtime. The v1→v2 data migration
258+// replays this chain to bring an older v1 database up to v1-final before
259+// reading it (v2's own schema is created from FumaDB DDL, not this folder).
258260 // ---------------------------------------------------------------------------
259261
260262 const createEmbeddedMigrationsSource = async () => {
261− const migrationsDir = resolve(webRoot, "drizzle");
263+ const migrationsDir = resolve(webRoot, "drizzle-legacy-v1");
262264 const files = (await Array.fromAsync(new Bun.Glob("**/*").scan({ cwd: migrationsDir })))
263265 .map((f) => f.replaceAll("\\", "/"))
264266 .sort();
apps/local/drizzle-legacy-v1/0000_overconfident_sharon_carter.sql+180−0
@@ -0,0 +1,180 @@
1+CREATE TABLE `blob` (
2+ `namespace` text NOT NULL,
3+ `key` text NOT NULL,
4+ `value` text NOT NULL,
5+ PRIMARY KEY(`namespace`, `key`)
6+);
7+--> statement-breakpoint
8+CREATE TABLE `definition` (
9+ `id` text NOT NULL,
10+ `scope_id` text NOT NULL,
11+ `source_id` text NOT NULL,
12+ `plugin_id` text NOT NULL,
13+ `name` text NOT NULL,
14+ `schema` text NOT NULL,
15+ `created_at` integer NOT NULL,
16+ PRIMARY KEY(`scope_id`, `id`)
17+);
18+--> statement-breakpoint
19+CREATE INDEX `definition_scope_id_idx` ON `definition` (`scope_id`);--> statement-breakpoint
20+CREATE INDEX `definition_source_id_idx` ON `definition` (`source_id`);--> statement-breakpoint
21+CREATE INDEX `definition_plugin_id_idx` ON `definition` (`plugin_id`);--> statement-breakpoint
22+CREATE TABLE `google_discovery_binding` (
23+ `id` text NOT NULL,
24+ `scope_id` text NOT NULL,
25+ `source_id` text NOT NULL,
26+ `binding` text NOT NULL,
27+ `created_at` integer NOT NULL,
28+ PRIMARY KEY(`scope_id`, `id`)
29+);
30+--> statement-breakpoint
31+CREATE INDEX `google_discovery_binding_scope_id_idx` ON `google_discovery_binding` (`scope_id`);--> statement-breakpoint
32+CREATE INDEX `google_discovery_binding_source_id_idx` ON `google_discovery_binding` (`source_id`);--> statement-breakpoint
33+CREATE TABLE `google_discovery_oauth_session` (
34+ `id` text NOT NULL,
35+ `scope_id` text NOT NULL,
36+ `session` text NOT NULL,
37+ `expires_at` integer NOT NULL,
38+ PRIMARY KEY(`scope_id`, `id`)
39+);
40+--> statement-breakpoint
41+CREATE INDEX `google_discovery_oauth_session_scope_id_idx` ON `google_discovery_oauth_session` (`scope_id`);--> statement-breakpoint
42+CREATE TABLE `google_discovery_source` (
43+ `id` text NOT NULL,
44+ `scope_id` text NOT NULL,
45+ `name` text NOT NULL,
46+ `config` text NOT NULL,
47+ `created_at` integer NOT NULL,
48+ `updated_at` integer NOT NULL,
49+ PRIMARY KEY(`scope_id`, `id`)
50+);
51+--> statement-breakpoint
52+CREATE INDEX `google_discovery_source_scope_id_idx` ON `google_discovery_source` (`scope_id`);--> statement-breakpoint
53+CREATE TABLE `graphql_operation` (
54+ `id` text NOT NULL,
55+ `scope_id` text NOT NULL,
56+ `source_id` text NOT NULL,
57+ `binding` text NOT NULL,
58+ PRIMARY KEY(`scope_id`, `id`)
59+);
60+--> statement-breakpoint
61+CREATE INDEX `graphql_operation_scope_id_idx` ON `graphql_operation` (`scope_id`);--> statement-breakpoint
62+CREATE INDEX `graphql_operation_source_id_idx` ON `graphql_operation` (`source_id`);--> statement-breakpoint
63+CREATE TABLE `graphql_source` (
64+ `id` text NOT NULL,
65+ `scope_id` text NOT NULL,
66+ `name` text NOT NULL,
67+ `endpoint` text NOT NULL,
68+ `headers` text,
69+ PRIMARY KEY(`scope_id`, `id`)
70+);
71+--> statement-breakpoint
72+CREATE INDEX `graphql_source_scope_id_idx` ON `graphql_source` (`scope_id`);--> statement-breakpoint
73+CREATE TABLE `mcp_binding` (
74+ `id` text NOT NULL,
75+ `scope_id` text NOT NULL,
76+ `source_id` text NOT NULL,
77+ `binding` text NOT NULL,
78+ `created_at` integer NOT NULL,
79+ PRIMARY KEY(`scope_id`, `id`)
80+);
81+--> statement-breakpoint
82+CREATE INDEX `mcp_binding_scope_id_idx` ON `mcp_binding` (`scope_id`);--> statement-breakpoint
83+CREATE INDEX `mcp_binding_source_id_idx` ON `mcp_binding` (`source_id`);--> statement-breakpoint
84+CREATE TABLE `mcp_oauth_session` (
85+ `id` text NOT NULL,
86+ `scope_id` text NOT NULL,
87+ `session` text NOT NULL,
88+ `expires_at` integer NOT NULL,
89+ `created_at` integer NOT NULL,
90+ PRIMARY KEY(`scope_id`, `id`)
91+);
92+--> statement-breakpoint
93+CREATE INDEX `mcp_oauth_session_scope_id_idx` ON `mcp_oauth_session` (`scope_id`);--> statement-breakpoint
94+CREATE TABLE `mcp_source` (
95+ `id` text NOT NULL,
96+ `scope_id` text NOT NULL,
97+ `name` text NOT NULL,
98+ `config` text NOT NULL,
99+ `created_at` integer NOT NULL,
100+ PRIMARY KEY(`scope_id`, `id`)
101+);
102+--> statement-breakpoint
103+CREATE INDEX `mcp_source_scope_id_idx` ON `mcp_source` (`scope_id`);--> statement-breakpoint
104+CREATE TABLE `openapi_oauth_session` (
105+ `id` text NOT NULL,
106+ `scope_id` text NOT NULL,
107+ `session` text NOT NULL,
108+ `created_at` integer NOT NULL,
109+ PRIMARY KEY(`scope_id`, `id`)
110+);
111+--> statement-breakpoint
112+CREATE INDEX `openapi_oauth_session_scope_id_idx` ON `openapi_oauth_session` (`scope_id`);--> statement-breakpoint
113+CREATE TABLE `openapi_operation` (
114+ `id` text NOT NULL,
115+ `scope_id` text NOT NULL,
116+ `source_id` text NOT NULL,
117+ `binding` text NOT NULL,
118+ PRIMARY KEY(`scope_id`, `id`)
119+);
120+--> statement-breakpoint
121+CREATE INDEX `openapi_operation_scope_id_idx` ON `openapi_operation` (`scope_id`);--> statement-breakpoint
122+CREATE INDEX `openapi_operation_source_id_idx` ON `openapi_operation` (`source_id`);--> statement-breakpoint
123+CREATE TABLE `openapi_source` (
124+ `id` text NOT NULL,
125+ `scope_id` text NOT NULL,
126+ `name` text NOT NULL,
127+ `spec` text NOT NULL,
128+ `base_url` text,
129+ `headers` text,
130+ `oauth2` text,
131+ `invocation_config` text NOT NULL,
132+ PRIMARY KEY(`scope_id`, `id`)
133+);
134+--> statement-breakpoint
135+CREATE INDEX `openapi_source_scope_id_idx` ON `openapi_source` (`scope_id`);--> statement-breakpoint
136+CREATE TABLE `secret` (
137+ `id` text NOT NULL,
138+ `scope_id` text NOT NULL,
139+ `name` text NOT NULL,
140+ `provider` text NOT NULL,
141+ `created_at` integer NOT NULL,
142+ PRIMARY KEY(`scope_id`, `id`)
143+);
144+--> statement-breakpoint
145+CREATE INDEX `secret_scope_id_idx` ON `secret` (`scope_id`);--> statement-breakpoint
146+CREATE INDEX `secret_provider_idx` ON `secret` (`provider`);--> statement-breakpoint
147+CREATE TABLE `source` (
148+ `id` text NOT NULL,
149+ `scope_id` text NOT NULL,
150+ `plugin_id` text NOT NULL,
151+ `kind` text NOT NULL,
152+ `name` text NOT NULL,
153+ `url` text,
154+ `can_remove` integer DEFAULT true NOT NULL,
155+ `can_refresh` integer DEFAULT false NOT NULL,
156+ `can_edit` integer DEFAULT false NOT NULL,
157+ `created_at` integer NOT NULL,
158+ `updated_at` integer NOT NULL,
159+ PRIMARY KEY(`scope_id`, `id`)
160+);
161+--> statement-breakpoint
162+CREATE INDEX `source_scope_id_idx` ON `source` (`scope_id`);--> statement-breakpoint
163+CREATE INDEX `source_plugin_id_idx` ON `source` (`plugin_id`);--> statement-breakpoint
164+CREATE TABLE `tool` (
165+ `id` text NOT NULL,
166+ `scope_id` text NOT NULL,
167+ `source_id` text NOT NULL,
168+ `plugin_id` text NOT NULL,
169+ `name` text NOT NULL,
170+ `description` text NOT NULL,
171+ `input_schema` text,
172+ `output_schema` text,
173+ `created_at` integer NOT NULL,
174+ `updated_at` integer NOT NULL,
175+ PRIMARY KEY(`scope_id`, `id`)
176+);
177+--> statement-breakpoint
178+CREATE INDEX `tool_scope_id_idx` ON `tool` (`scope_id`);--> statement-breakpoint
179+CREATE INDEX `tool_source_id_idx` ON `tool` (`source_id`);--> statement-breakpoint
180+CREATE INDEX `tool_plugin_id_idx` ON `tool` (`plugin_id`);
apps/local/drizzle-legacy-v1/0001_sour_catseye.sql+1−0
@@ -0,0 +1 @@
1+ALTER TABLE `openapi_source` ADD `source_url` text;
apps/local/drizzle-legacy-v1/0002_lively_sue_storm.sql+20−0
@@ -0,0 +1,20 @@
1+CREATE TABLE `connection` (
2+ `id` text NOT NULL,
3+ `scope_id` text NOT NULL,
4+ `provider` text NOT NULL,
5+ `kind` text NOT NULL,
6+ `identity_label` text,
7+ `access_token_secret_id` text NOT NULL,
8+ `refresh_token_secret_id` text,
9+ `expires_at` integer,
10+ `scope` text,
11+ `provider_state` text,
12+ `created_at` integer NOT NULL,
13+ `updated_at` integer NOT NULL,
14+ PRIMARY KEY(`scope_id`, `id`)
15+);
16+--> statement-breakpoint
17+CREATE INDEX `connection_scope_id_idx` ON `connection` (`scope_id`);--> statement-breakpoint
18+CREATE INDEX `connection_provider_idx` ON `connection` (`provider`);--> statement-breakpoint
19+ALTER TABLE `secret` ADD `owned_by_connection_id` text;--> statement-breakpoint
20+CREATE INDEX `secret_owned_by_connection_id_idx` ON `secret` (`owned_by_connection_id`);
apps/local/drizzle-legacy-v1/0003_little_silk_fever.sql+16−0
@@ -0,0 +1,16 @@
1+CREATE TABLE `openapi_source_binding` (
2+ `id` text PRIMARY KEY NOT NULL,
3+ `source_id` text NOT NULL,
4+ `source_scope_id` text NOT NULL,
5+ `target_scope_id` text NOT NULL,
6+ `slot` text NOT NULL,
7+ `value` text NOT NULL,
8+ `created_at` integer NOT NULL,
9+ `updated_at` integer NOT NULL
10+);
11+--> statement-breakpoint
12+CREATE INDEX `openapi_source_binding_source_id_idx` ON `openapi_source_binding` (`source_id`);--> statement-breakpoint
13+CREATE INDEX `openapi_source_binding_source_scope_id_idx` ON `openapi_source_binding` (`source_scope_id`);--> statement-breakpoint
14+CREATE INDEX `openapi_source_binding_target_scope_id_idx` ON `openapi_source_binding` (`target_scope_id`);--> statement-breakpoint
15+CREATE INDEX `openapi_source_binding_slot_idx` ON `openapi_source_binding` (`slot`);--> statement-breakpoint
16+ALTER TABLE `connection` DROP COLUMN `kind`;
apps/local/drizzle-legacy-v1/0004_add_tool_policy.sql+17−0
@@ -0,0 +1,17 @@
1+CREATE TABLE `tool_policy` (
2+ `id` text NOT NULL,
3+ `scope_id` text NOT NULL,
4+ `pattern` text NOT NULL,
5+ `action` text NOT NULL,
6+ -- Fractional-indexing key (Jira lexorank style). Lex-ordered text;
7+ -- always subdivisible by lengthening, so reorders never run out of
8+ -- room.
9+ `position` text NOT NULL,
10+ `created_at` integer NOT NULL,
11+ `updated_at` integer NOT NULL,
12+ PRIMARY KEY(`scope_id`, `id`)
13+);
14+--> statement-breakpoint
15+-- List queries are always `WHERE scope_id = ? ORDER BY position`, so the
16+-- composite index serves both the filter and the sort from one btree.
17+CREATE INDEX `tool_policy_scope_id_position_idx` ON `tool_policy` (`scope_id`, `position`);
apps/local/drizzle-legacy-v1/0005_repair_mcp_oauth_session.sql+10−0
@@ -0,0 +1,10 @@
1+CREATE TABLE IF NOT EXISTS `mcp_oauth_session` (
2+ `id` text NOT NULL,
3+ `scope_id` text NOT NULL,
4+ `session` text NOT NULL,
5+ `expires_at` integer NOT NULL,
6+ `created_at` integer NOT NULL,
7+ PRIMARY KEY(`scope_id`, `id`)
8+);
9+--> statement-breakpoint
10+CREATE INDEX IF NOT EXISTS `mcp_oauth_session_scope_id_idx` ON `mcp_oauth_session` (`scope_id`);
apps/local/drizzle-legacy-v1/0006_neat_terror.sql+23−0
@@ -0,0 +1,23 @@
1+CREATE TABLE `oauth2_session` (
2+ `id` text NOT NULL,
3+ `scope_id` text NOT NULL,
4+ `plugin_id` text NOT NULL,
5+ `strategy` text NOT NULL,
6+ `connection_id` text NOT NULL,
7+ `token_scope` text NOT NULL,
8+ `redirect_url` text NOT NULL,
9+ `payload` text NOT NULL,
10+ `expires_at` integer NOT NULL,
11+ `created_at` integer NOT NULL,
12+ PRIMARY KEY(`scope_id`, `id`)
13+);
14+--> statement-breakpoint
15+CREATE INDEX `oauth2_session_scope_id_idx` ON `oauth2_session` (`scope_id`);--> statement-breakpoint
16+CREATE INDEX `oauth2_session_plugin_id_idx` ON `oauth2_session` (`plugin_id`);--> statement-breakpoint
17+CREATE INDEX `oauth2_session_connection_id_idx` ON `oauth2_session` (`connection_id`);--> statement-breakpoint
18+DROP TABLE `google_discovery_oauth_session`;--> statement-breakpoint
19+DROP TABLE `mcp_oauth_session`;--> statement-breakpoint
20+DROP TABLE `openapi_oauth_session`;--> statement-breakpoint
21+ALTER TABLE `graphql_source` ADD `query_params` text;--> statement-breakpoint
22+ALTER TABLE `graphql_source` ADD `auth` text;--> statement-breakpoint
23+ALTER TABLE `openapi_source` ADD `query_params` text;
apps/local/drizzle-legacy-v1/0007_normalize_plugin_secret_refs.sql+579−0
@@ -0,0 +1,579 @@
1+-- Normalize all plugin secret/connection refs out of JSON columns
2+-- into proper relational shape: graphql, openapi, mcp,
3+-- google-discovery. Each block is a self-contained backfill of one
4+-- plugin's tables; collected here so the schema lands in one
5+-- migration step instead of four sequential ones.
6+--
7+-- Old shape (per plugin):
8+-- {plugin}_source.{auth, headers, query_params, ...}: json blobs
9+-- openapi_source_binding.value: json union
10+-- {plugin}_source.config.{auth, headers, queryParams}: nested json
11+--
12+-- New shape (per plugin):
13+-- Flat auth_* columns on the parent (auth_kind enum + per-kind id
14+-- columns, all indexed where they hold refs).
15+-- Child tables for SecretBackedMap entries: one row per (source,
16+-- header_name) keyed by JSON tuple [source_id,name] so user keys
17+-- with separators don't collide. `secret_id` indexed for the
18+-- usagesForSecret query.
19+
20+-- ============================================================
21+-- graphql
22+-- ============================================================
23+
24+-- Normalize graphql plugin: move secret/connection refs out of JSON
25+-- columns into proper relational shape so usagesForSecret /
26+-- usagesForConnection are one indexed SELECT instead of a JSON scan.
27+--
28+-- Old shape:
29+-- graphql_source.headers json Record<name, string | {secretId,prefix?}>
30+-- graphql_source.query_params json Record<name, string | {secretId,prefix?}>
31+-- graphql_source.auth json {kind:"none"} | {kind:"oauth2", connectionId}
32+--
33+-- New shape:
34+-- graphql_source.auth_kind enum("none","oauth2") NOT NULL
35+-- graphql_source.auth_connection_id text indexed nullable
36+-- graphql_source_header(scope_id, id, source_id, name, kind, text_value, secret_id, secret_prefix)
37+-- graphql_source_query_param(scope_id, id, source_id, name, kind, text_value, secret_id, secret_prefix)
38+
39+CREATE TABLE `graphql_source_header` (
40+ `id` text NOT NULL,
41+ `scope_id` text NOT NULL,
42+ `source_id` text NOT NULL,
43+ `name` text NOT NULL,
44+ `kind` text NOT NULL,
45+ `text_value` text,
46+ `secret_id` text,
47+ `secret_prefix` text,
48+ PRIMARY KEY(`scope_id`, `id`)
49+);
50+--> statement-breakpoint
51+CREATE INDEX `graphql_source_header_scope_id_idx` ON `graphql_source_header` (`scope_id`);--> statement-breakpoint
52+CREATE INDEX `graphql_source_header_source_id_idx` ON `graphql_source_header` (`source_id`);--> statement-breakpoint
53+CREATE INDEX `graphql_source_header_secret_id_idx` ON `graphql_source_header` (`secret_id`);--> statement-breakpoint
54+
55+CREATE TABLE `graphql_source_query_param` (
56+ `id` text NOT NULL,
57+ `scope_id` text NOT NULL,
58+ `source_id` text NOT NULL,
59+ `name` text NOT NULL,
60+ `kind` text NOT NULL,
61+ `text_value` text,
62+ `secret_id` text,
63+ `secret_prefix` text,
64+ PRIMARY KEY(`scope_id`, `id`)
65+);
66+--> statement-breakpoint
67+CREATE INDEX `graphql_source_query_param_scope_id_idx` ON `graphql_source_query_param` (`scope_id`);--> statement-breakpoint
68+CREATE INDEX `graphql_source_query_param_source_id_idx` ON `graphql_source_query_param` (`source_id`);--> statement-breakpoint
69+CREATE INDEX `graphql_source_query_param_secret_id_idx` ON `graphql_source_query_param` (`secret_id`);--> statement-breakpoint
70+
71+-- New auth columns. `auth_kind` defaults to "none" so existing rows that
72+-- predate this migration are valid even if the json was null.
73+ALTER TABLE `graphql_source` ADD `auth_kind` text DEFAULT 'none' NOT NULL;--> statement-breakpoint
74+ALTER TABLE `graphql_source` ADD `auth_connection_id` text;--> statement-breakpoint
75+CREATE INDEX `graphql_source_auth_connection_id_idx` ON `graphql_source` (`auth_connection_id`);--> statement-breakpoint
76+
77+-- Backfill auth from the JSON column. json_extract returns NULL for
78+-- missing paths, so a row with auth=NULL or kind="none" leaves
79+-- auth_connection_id NULL and auth_kind defaulted to "none".
80+UPDATE `graphql_source`
81+SET
82+ `auth_kind` = COALESCE(json_extract(`auth`, '$.kind'), 'none'),
83+ `auth_connection_id` = json_extract(`auth`, '$.connectionId')
84+WHERE `auth` IS NOT NULL;--> statement-breakpoint
85+
86+-- Backfill headers. For each (source, header_name) pair: if the value
87+-- is a json object with .secretId, write a kind=secret row; otherwise
88+-- write a kind=text row with the literal string. json_each iterates
89+-- the keys of the headers object.
90+INSERT OR IGNORE INTO `graphql_source_header`
91+ (`scope_id`, `id`, `source_id`, `name`, `kind`, `text_value`, `secret_id`, `secret_prefix`)
92+SELECT
93+ s.`scope_id`,
94+ json_array(s.`id`, h.`key`),
95+ s.`id`,
96+ h.`key`,
97+ CASE
98+ WHEN h.`type` = 'object' AND json_extract(h.`value`, '$.secretId') IS NOT NULL THEN 'secret'
99+ ELSE 'text'
100+ END,
101+ CASE WHEN h.`type` = 'object' THEN NULL ELSE h.`value` END,
102+ CASE WHEN h.`type` = 'object' THEN json_extract(h.`value`, '$.secretId') ELSE NULL END,
103+ CASE WHEN h.`type` = 'object' THEN json_extract(h.`value`, '$.prefix') ELSE NULL END
104+FROM `graphql_source` s, json_each(s.`headers`) h
105+WHERE s.`headers` IS NOT NULL;--> statement-breakpoint
106+
107+-- Same for query_params.
108+INSERT OR IGNORE INTO `graphql_source_query_param`
109+ (`scope_id`, `id`, `source_id`, `name`, `kind`, `text_value`, `secret_id`, `secret_prefix`)
110+SELECT
111+ s.`scope_id`,
112+ json_array(s.`id`, q.`key`),
113+ s.`id`,
114+ q.`key`,
115+ CASE
116+ WHEN q.`type` = 'object' AND json_extract(q.`value`, '$.secretId') IS NOT NULL THEN 'secret'
117+ ELSE 'text'
118+ END,
119+ CASE WHEN q.`type` = 'object' THEN NULL ELSE q.`value` END,
120+ CASE WHEN q.`type` = 'object' THEN json_extract(q.`value`, '$.secretId') ELSE NULL END,
121+ CASE WHEN q.`type` = 'object' THEN json_extract(q.`value`, '$.prefix') ELSE NULL END
122+FROM `graphql_source` s, json_each(s.`query_params`) q
123+WHERE s.`query_params` IS NOT NULL;--> statement-breakpoint
124+
125+-- Drop the old JSON columns. SQLite ≥ 3.35 supports ALTER TABLE DROP
126+-- COLUMN directly; bun's bundled SQLite is well past that.
127+ALTER TABLE `graphql_source` DROP COLUMN `headers`;--> statement-breakpoint
128+ALTER TABLE `graphql_source` DROP COLUMN `query_params`;--> statement-breakpoint
129+ALTER TABLE `graphql_source` DROP COLUMN `auth`;
130+
131+--> statement-breakpoint
132+
133+-- ============================================================
134+-- openapi
135+-- ============================================================
136+
137+-- Normalize openapi plugin: move every direct secret/connection ref out
138+-- of JSON columns into proper relational shape.
139+--
140+-- Old shape:
141+-- openapi_source.query_params json Record<name, string | {secretId,prefix?}>
142+-- openapi_source.invocation_config json { specFetchCredentials?: { headers, queryParams } }
143+-- openapi_source_binding.value json discriminated union
144+-- {kind:"secret",secretId} | {kind:"connection",connectionId} | {kind:"text",text}
145+--
146+-- New shape:
147+-- openapi_source_binding gains kind/secret_id/connection_id/text_value columns.
148+-- `headers` / `oauth2` on openapi_source stay JSON because they hold
149+-- slot names, not direct refs — the actual credentials reach those
150+-- slots through openapi_source_binding rows, which ARE normalized.
151+-- openapi_source_query_param: child table, secret-backed entries.
152+-- openapi_source_spec_fetch_header / spec_fetch_query_param: child
153+-- tables for the equivalent maps inside specFetchCredentials.
154+
155+CREATE TABLE `openapi_source_query_param` (
156+ `id` text NOT NULL,
157+ `scope_id` text NOT NULL,
158+ `source_id` text NOT NULL,
159+ `name` text NOT NULL,
160+ `kind` text NOT NULL,
161+ `text_value` text,
162+ `secret_id` text,
163+ `secret_prefix` text,
164+ PRIMARY KEY(`scope_id`, `id`)
165+);
166+--> statement-breakpoint
167+CREATE INDEX `openapi_source_query_param_scope_id_idx` ON `openapi_source_query_param` (`scope_id`);--> statement-breakpoint
168+CREATE INDEX `openapi_source_query_param_source_id_idx` ON `openapi_source_query_param` (`source_id`);--> statement-breakpoint
169+CREATE INDEX `openapi_source_query_param_secret_id_idx` ON `openapi_source_query_param` (`secret_id`);--> statement-breakpoint
170+
171+CREATE TABLE `openapi_source_spec_fetch_header` (
172+ `id` text NOT NULL,
173+ `scope_id` text NOT NULL,
174+ `source_id` text NOT NULL,
175+ `name` text NOT NULL,
176+ `kind` text NOT NULL,
177+ `text_value` text,
178+ `secret_id` text,
179+ `secret_prefix` text,
180+ PRIMARY KEY(`scope_id`, `id`)
181+);
182+--> statement-breakpoint
183+CREATE INDEX `openapi_source_spec_fetch_header_scope_id_idx` ON `openapi_source_spec_fetch_header` (`scope_id`);--> statement-breakpoint
184+CREATE INDEX `openapi_source_spec_fetch_header_source_id_idx` ON `openapi_source_spec_fetch_header` (`source_id`);--> statement-breakpoint
185+CREATE INDEX `openapi_source_spec_fetch_header_secret_id_idx` ON `openapi_source_spec_fetch_header` (`secret_id`);--> statement-breakpoint
186+
187+CREATE TABLE `openapi_source_spec_fetch_query_param` (
188+ `id` text NOT NULL,
189+ `scope_id` text NOT NULL,
190+ `source_id` text NOT NULL,
191+ `name` text NOT NULL,
192+ `kind` text NOT NULL,
193+ `text_value` text,
194+ `secret_id` text,
195+ `secret_prefix` text,
196+ PRIMARY KEY(`scope_id`, `id`)
197+);
198+--> statement-breakpoint
199+CREATE INDEX `openapi_source_spec_fetch_query_param_scope_id_idx` ON `openapi_source_spec_fetch_query_param` (`scope_id`);--> statement-breakpoint
200+CREATE INDEX `openapi_source_spec_fetch_query_param_source_id_idx` ON `openapi_source_spec_fetch_query_param` (`source_id`);--> statement-breakpoint
201+CREATE INDEX `openapi_source_spec_fetch_query_param_secret_id_idx` ON `openapi_source_spec_fetch_query_param` (`secret_id`);--> statement-breakpoint
202+
203+-- New columns on openapi_source_binding to flatten the value json.
204+-- `kind` defaults to 'text' so the ALTER works on existing rows; the
205+-- backfill below stamps the real value.
206+ALTER TABLE `openapi_source_binding` ADD `kind` text DEFAULT 'text' NOT NULL;--> statement-breakpoint
207+ALTER TABLE `openapi_source_binding` ADD `secret_id` text;--> statement-breakpoint
208+ALTER TABLE `openapi_source_binding` ADD `connection_id` text;--> statement-breakpoint
209+ALTER TABLE `openapi_source_binding` ADD `text_value` text;--> statement-breakpoint
210+CREATE INDEX `openapi_source_binding_secret_id_idx` ON `openapi_source_binding` (`secret_id`);--> statement-breakpoint
211+CREATE INDEX `openapi_source_binding_connection_id_idx` ON `openapi_source_binding` (`connection_id`);--> statement-breakpoint
212+
213+-- Backfill the binding columns from the legacy `value` JSON. We pull
214+-- $.kind into `kind` directly; for each kind the matching id field
215+-- (`secretId` / `connectionId` / `text`) gets copied into the matching
216+-- column. Rows whose value JSON is malformed or missing $.kind fall
217+-- through to kind='text' with a NULL text_value — same as a missing
218+-- text binding, the source will surface "binding not configured" at
219+-- invoke time rather than crashing the migration.
220+UPDATE `openapi_source_binding`
221+SET
222+ `kind` = COALESCE(json_extract(`value`, '$.kind'), 'text'),
223+ `secret_id` = CASE WHEN json_extract(`value`, '$.kind') = 'secret' THEN json_extract(`value`, '$.secretId') ELSE NULL END,
224+ `connection_id` = CASE WHEN json_extract(`value`, '$.kind') = 'connection' THEN json_extract(`value`, '$.connectionId') ELSE NULL END,
225+ `text_value` = CASE WHEN json_extract(`value`, '$.kind') = 'text' THEN json_extract(`value`, '$.text') ELSE NULL END
226+WHERE `value` IS NOT NULL;--> statement-breakpoint
227+
228+-- Backfill openapi_source_query_param from openapi_source.query_params.
229+-- json_each iterates the keys of the query_params object. For each
230+-- entry: if the value is an object with .secretId, write a kind=secret
231+-- row; otherwise write a kind=text row with the literal string.
232+INSERT OR IGNORE INTO `openapi_source_query_param`
233+ (`scope_id`, `id`, `source_id`, `name`, `kind`, `text_value`, `secret_id`, `secret_prefix`)
234+SELECT
235+ s.`scope_id`,
236+ json_array(s.`id`, q.`key`),
237+ s.`id`,
238+ q.`key`,
239+ CASE
240+ WHEN q.`type` = 'object' AND json_extract(q.`value`, '$.secretId') IS NOT NULL THEN 'secret'
241+ ELSE 'text'
242+ END,
243+ CASE WHEN q.`type` = 'object' THEN NULL ELSE q.`value` END,
244+ CASE WHEN q.`type` = 'object' THEN json_extract(q.`value`, '$.secretId') ELSE NULL END,
245+ CASE WHEN q.`type` = 'object' THEN json_extract(q.`value`, '$.prefix') ELSE NULL END
246+FROM `openapi_source` s, json_each(s.`query_params`) q
247+WHERE s.`query_params` IS NOT NULL;--> statement-breakpoint
248+
249+-- Backfill openapi_source_spec_fetch_header from
250+-- openapi_source.invocation_config.specFetchCredentials.headers. Same
251+-- shape as query_params; the JSON path is one level deeper.
252+INSERT OR IGNORE INTO `openapi_source_spec_fetch_header`
253+ (`scope_id`, `id`, `source_id`, `name`, `kind`, `text_value`, `secret_id`, `secret_prefix`)
254+SELECT
255+ s.`scope_id`,
256+ json_array(s.`id`, h.`key`),
257+ s.`id`,
258+ h.`key`,
259+ CASE
260+ WHEN h.`type` = 'object' AND json_extract(h.`value`, '$.secretId') IS NOT NULL THEN 'secret'
261+ ELSE 'text'
262+ END,
263+ CASE WHEN h.`type` = 'object' THEN NULL ELSE h.`value` END,
264+ CASE WHEN h.`type` = 'object' THEN json_extract(h.`value`, '$.secretId') ELSE NULL END,
265+ CASE WHEN h.`type` = 'object' THEN json_extract(h.`value`, '$.prefix') ELSE NULL END
266+FROM `openapi_source` s, json_each(json_extract(s.`invocation_config`, '$.specFetchCredentials.headers')) h
267+WHERE json_extract(s.`invocation_config`, '$.specFetchCredentials.headers') IS NOT NULL;--> statement-breakpoint
268+
269+INSERT OR IGNORE INTO `openapi_source_spec_fetch_query_param`
270+ (`scope_id`, `id`, `source_id`, `name`, `kind`, `text_value`, `secret_id`, `secret_prefix`)
271+SELECT
272+ s.`scope_id`,
273+ json_array(s.`id`, q.`key`),
274+ s.`id`,
275+ q.`key`,
276+ CASE
277+ WHEN q.`type` = 'object' AND json_extract(q.`value`, '$.secretId') IS NOT NULL THEN 'secret'
278+ ELSE 'text'
279+ END,
280+ CASE WHEN q.`type` = 'object' THEN NULL ELSE q.`value` END,
281+ CASE WHEN q.`type` = 'object' THEN json_extract(q.`value`, '$.secretId') ELSE NULL END,
282+ CASE WHEN q.`type` = 'object' THEN json_extract(q.`value`, '$.prefix') ELSE NULL END
283+FROM `openapi_source` s, json_each(json_extract(s.`invocation_config`, '$.specFetchCredentials.queryParams')) q
284+WHERE json_extract(s.`invocation_config`, '$.specFetchCredentials.queryParams') IS NOT NULL;--> statement-breakpoint
285+
286+-- Preserve any legacy OAuth payload from invocation_config.oauth2 into
287+-- the still-existing oauth2 column before we drop invocation_config.
288+-- migrateLegacyConnections runs after drizzle migrations and reads
289+-- oauth2 to detect the legacy shape; without this, rows that only had
290+-- their OAuth payload under invocation_config.oauth2 would lose it.
291+UPDATE `openapi_source`
292+SET `oauth2` = json_extract(`invocation_config`, '$.oauth2')
293+WHERE `oauth2` IS NULL
294+ AND json_extract(`invocation_config`, '$.oauth2') IS NOT NULL;--> statement-breakpoint
295+
296+-- Drop the legacy JSON columns now that everything is normalized.
297+ALTER TABLE `openapi_source_binding` DROP COLUMN `value`;--> statement-breakpoint
298+ALTER TABLE `openapi_source` DROP COLUMN `query_params`;--> statement-breakpoint
299+ALTER TABLE `openapi_source` DROP COLUMN `invocation_config`;
300+
301+--> statement-breakpoint
302+
303+-- ============================================================
304+-- mcp
305+-- ============================================================
306+
307+-- Normalize mcp plugin: lift the McpConnectionAuth secret/connection
308+-- refs and the SecretBackedMap headers/query_params out of
309+-- mcp_source.config JSON into proper columns / child tables.
310+--
311+-- Old shape:
312+-- mcp_source.config (json) — McpStoredSourceData discriminated union
313+-- remote: { transport, endpoint, remoteTransport?, queryParams?,
314+-- headers?, auth: McpConnectionAuth }
315+-- stdio: { transport, command, args?, env?, cwd? }
316+--
317+-- New shape:
318+-- mcp_source gains: auth_kind enum, auth_header_name, auth_secret_id,
319+-- auth_secret_prefix, auth_connection_id, auth_client_id_secret_id,
320+-- auth_client_secret_secret_id. The remaining structural fields
321+-- stay in `config` as JSON because they're plugin-private and
322+-- vary by transport.
323+-- mcp_source_header / mcp_source_query_param: child tables for
324+-- remote sources' SecretBackedMap entries (same column shape as
325+-- graphql_source_header / openapi_source_query_param).
326+
327+CREATE TABLE `mcp_source_header` (
328+ `id` text NOT NULL,
329+ `scope_id` text NOT NULL,
330+ `source_id` text NOT NULL,
331+ `name` text NOT NULL,
332+ `kind` text NOT NULL,
333+ `text_value` text,
334+ `secret_id` text,
335+ `secret_prefix` text,
336+ PRIMARY KEY(`scope_id`, `id`)
337+);
338+--> statement-breakpoint
339+CREATE INDEX `mcp_source_header_scope_id_idx` ON `mcp_source_header` (`scope_id`);--> statement-breakpoint
340+CREATE INDEX `mcp_source_header_source_id_idx` ON `mcp_source_header` (`source_id`);--> statement-breakpoint
341+CREATE INDEX `mcp_source_header_secret_id_idx` ON `mcp_source_header` (`secret_id`);--> statement-breakpoint
342+
343+CREATE TABLE `mcp_source_query_param` (
344+ `id` text NOT NULL,
345+ `scope_id` text NOT NULL,
346+ `source_id` text NOT NULL,
347+ `name` text NOT NULL,
348+ `kind` text NOT NULL,
349+ `text_value` text,
350+ `secret_id` text,
351+ `secret_prefix` text,
352+ PRIMARY KEY(`scope_id`, `id`)
353+);
354+--> statement-breakpoint
355+CREATE INDEX `mcp_source_query_param_scope_id_idx` ON `mcp_source_query_param` (`scope_id`);--> statement-breakpoint
356+CREATE INDEX `mcp_source_query_param_source_id_idx` ON `mcp_source_query_param` (`source_id`);--> statement-breakpoint
357+CREATE INDEX `mcp_source_query_param_secret_id_idx` ON `mcp_source_query_param` (`secret_id`);--> statement-breakpoint
358+
359+-- New auth columns. `auth_kind` defaults to "none" so the ALTER passes
360+-- on existing rows; the backfill below stamps the real value.
361+ALTER TABLE `mcp_source` ADD `auth_kind` text DEFAULT 'none' NOT NULL;--> statement-breakpoint
362+ALTER TABLE `mcp_source` ADD `auth_header_name` text;--> statement-breakpoint
363+ALTER TABLE `mcp_source` ADD `auth_secret_id` text;--> statement-breakpoint
364+ALTER TABLE `mcp_source` ADD `auth_secret_prefix` text;--> statement-breakpoint
365+ALTER TABLE `mcp_source` ADD `auth_connection_id` text;--> statement-breakpoint
366+ALTER TABLE `mcp_source` ADD `auth_client_id_secret_id` text;--> statement-breakpoint
367+ALTER TABLE `mcp_source` ADD `auth_client_secret_secret_id` text;--> statement-breakpoint
368+CREATE INDEX `mcp_source_auth_secret_id_idx` ON `mcp_source` (`auth_secret_id`);--> statement-breakpoint
369+CREATE INDEX `mcp_source_auth_connection_id_idx` ON `mcp_source` (`auth_connection_id`);--> statement-breakpoint
370+CREATE INDEX `mcp_source_auth_client_id_secret_id_idx` ON `mcp_source` (`auth_client_id_secret_id`);--> statement-breakpoint
371+CREATE INDEX `mcp_source_auth_client_secret_secret_id_idx` ON `mcp_source` (`auth_client_secret_secret_id`);--> statement-breakpoint
372+
373+-- Backfill auth columns from config.auth — but only for rows whose
374+-- config.auth matches the *current* shape:
375+-- - kind=none (no extra fields)
376+-- - kind=header (secretId present)
377+-- - kind=oauth2 (connectionId present)
378+-- Truly-legacy rows (inline OAuth shape with accessTokenSecretId etc.)
379+-- are left untouched here so the post-migrate `migrateLegacyConnections`
380+-- script can convert them to a Connection and write the resulting
381+-- pointer to these columns. Setting auth_kind explicitly to NULL/none
382+-- on those rows would lose the legacy payload before it gets converted.
383+UPDATE `mcp_source`
384+SET
385+ `auth_kind` = json_extract(`config`, '$.auth.kind'),
386+ `auth_header_name` = json_extract(`config`, '$.auth.headerName'),
387+ `auth_secret_id` = json_extract(`config`, '$.auth.secretId'),
388+ `auth_secret_prefix` = json_extract(`config`, '$.auth.prefix'),
389+ `auth_connection_id` = json_extract(`config`, '$.auth.connectionId'),
390+ `auth_client_id_secret_id` = json_extract(`config`, '$.auth.clientIdSecretId'),
391+ `auth_client_secret_secret_id` = json_extract(`config`, '$.auth.clientSecretSecretId')
392+WHERE `config` IS NOT NULL
393+ AND (
394+ -- kind=none and "no auth at all" both leave auth_kind defaulted to
395+ -- 'none' (the column DEFAULT), so we only UPDATE rows that have a
396+ -- non-trivial current-shape auth payload to extract.
397+ (
398+ json_extract(`config`, '$.auth.kind') = 'header'
399+ AND json_extract(`config`, '$.auth.secretId') IS NOT NULL
400+ )
401+ OR (
402+ json_extract(`config`, '$.auth.kind') = 'oauth2'
403+ AND json_extract(`config`, '$.auth.connectionId') IS NOT NULL
404+ )
405+ );--> statement-breakpoint
406+
407+-- Backfill mcp_source_header from config.headers. Remote sources only;
408+-- stdio's config has no `.headers` key so json_each returns nothing.
409+INSERT OR IGNORE INTO `mcp_source_header`
410+ (`scope_id`, `id`, `source_id`, `name`, `kind`, `text_value`, `secret_id`, `secret_prefix`)
411+SELECT
412+ s.`scope_id`,
413+ json_array(s.`id`, h.`key`),
414+ s.`id`,
415+ h.`key`,
416+ CASE
417+ WHEN h.`type` = 'object' AND json_extract(h.`value`, '$.secretId') IS NOT NULL THEN 'secret'
418+ ELSE 'text'
419+ END,
420+ CASE WHEN h.`type` = 'object' THEN NULL ELSE h.`value` END,
421+ CASE WHEN h.`type` = 'object' THEN json_extract(h.`value`, '$.secretId') ELSE NULL END,
422+ CASE WHEN h.`type` = 'object' THEN json_extract(h.`value`, '$.prefix') ELSE NULL END
423+FROM `mcp_source` s, json_each(json_extract(s.`config`, '$.headers')) h
424+WHERE json_extract(s.`config`, '$.headers') IS NOT NULL;--> statement-breakpoint
425+
426+INSERT OR IGNORE INTO `mcp_source_query_param`
427+ (`scope_id`, `id`, `source_id`, `name`, `kind`, `text_value`, `secret_id`, `secret_prefix`)
428+SELECT
429+ s.`scope_id`,
430+ json_array(s.`id`, q.`key`),
431+ s.`id`,
432+ q.`key`,
433+ CASE
434+ WHEN q.`type` = 'object' AND json_extract(q.`value`, '$.secretId') IS NOT NULL THEN 'secret'
435+ ELSE 'text'
436+ END,
437+ CASE WHEN q.`type` = 'object' THEN NULL ELSE q.`value` END,
438+ CASE WHEN q.`type` = 'object' THEN json_extract(q.`value`, '$.secretId') ELSE NULL END,
439+ CASE WHEN q.`type` = 'object' THEN json_extract(q.`value`, '$.prefix') ELSE NULL END
440+FROM `mcp_source` s, json_each(json_extract(s.`config`, '$.queryParams')) q
441+WHERE json_extract(s.`config`, '$.queryParams') IS NOT NULL;--> statement-breakpoint
442+
443+-- Strip the now-extracted fields from the legacy config JSON. Skip
444+-- rows whose config.auth still holds a legacy inline-OAuth payload —
445+-- migrateLegacyConnections needs to read it to mint the matching
446+-- Connection. headers/queryParams are always safe to strip (already
447+-- copied to child tables). SQLite's json_remove returns the input
448+-- unchanged when a path is missing, so stdio rows pass through
449+-- cleanly.
450+UPDATE `mcp_source`
451+SET `config` = json_remove(`config`, '$.headers', '$.queryParams')
452+WHERE `config` IS NOT NULL;--> statement-breakpoint
453+
454+UPDATE `mcp_source`
455+SET `config` = json_remove(`config`, '$.auth')
456+WHERE `config` IS NOT NULL
457+ AND (
458+ json_extract(`config`, '$.auth.kind') = 'none'
459+ OR (
460+ json_extract(`config`, '$.auth.kind') = 'header'
461+ AND json_extract(`config`, '$.auth.secretId') IS NOT NULL
462+ )
463+ OR (
464+ json_extract(`config`, '$.auth.kind') = 'oauth2'
465+ AND json_extract(`config`, '$.auth.connectionId') IS NOT NULL
466+ )
467+ );
468+
469+--> statement-breakpoint
470+
471+-- ============================================================
472+-- google-discovery
473+-- ============================================================
474+
475+-- Normalize google-discovery plugin: lift the GoogleDiscoveryAuth and
476+-- the credentials.{headers,queryParams} SecretBackedMaps out of
477+-- google_discovery_source.config JSON.
478+--
479+-- Old shape:
480+-- google_discovery_source.config (json) — GoogleDiscoveryStoredSourceData
481+-- with `auth: {kind:"none"} | {kind:"oauth2", connectionId, clientId..., clientSecret..., scopes}`
482+-- and optional `credentials: { headers?, queryParams? }`
483+--
484+-- New shape:
485+-- google_discovery_source gains: auth_kind, auth_connection_id,
486+-- auth_client_id_secret_id, auth_client_secret_secret_id, auth_scopes.
487+-- google_discovery_source_credential_header / _query_param: child
488+-- tables for the SecretBackedMap entries.
489+
490+CREATE TABLE `google_discovery_source_credential_header` (
491+ `id` text NOT NULL,
492+ `scope_id` text NOT NULL,
493+ `source_id` text NOT NULL,
494+ `name` text NOT NULL,
495+ `kind` text NOT NULL,
496+ `text_value` text,
497+ `secret_id` text,
498+ `secret_prefix` text,
499+ PRIMARY KEY(`scope_id`, `id`)
500+);
501+--> statement-breakpoint
502+CREATE INDEX `google_discovery_source_credential_header_scope_id_idx` ON `google_discovery_source_credential_header` (`scope_id`);--> statement-breakpoint
503+CREATE INDEX `google_discovery_source_credential_header_source_id_idx` ON `google_discovery_source_credential_header` (`source_id`);--> statement-breakpoint
504+CREATE INDEX `google_discovery_source_credential_header_secret_id_idx` ON `google_discovery_source_credential_header` (`secret_id`);--> statement-breakpoint
505+
506+CREATE TABLE `google_discovery_source_credential_query_param` (
507+ `id` text NOT NULL,
508+ `scope_id` text NOT NULL,
509+ `source_id` text NOT NULL,
510+ `name` text NOT NULL,
511+ `kind` text NOT NULL,
512+ `text_value` text,
513+ `secret_id` text,
514+ `secret_prefix` text,
515+ PRIMARY KEY(`scope_id`, `id`)
516+);
517+--> statement-breakpoint
518+CREATE INDEX `google_discovery_source_credential_query_param_scope_id_idx` ON `google_discovery_source_credential_query_param` (`scope_id`);--> statement-breakpoint
519+CREATE INDEX `google_discovery_source_credential_query_param_source_id_idx` ON `google_discovery_source_credential_query_param` (`source_id`);--> statement-breakpoint
520+CREATE INDEX `google_discovery_source_credential_query_param_secret_id_idx` ON `google_discovery_source_credential_query_param` (`secret_id`);--> statement-breakpoint
521+
522+ALTER TABLE `google_discovery_source` ADD `auth_kind` text DEFAULT 'none' NOT NULL;--> statement-breakpoint
523+ALTER TABLE `google_discovery_source` ADD `auth_connection_id` text;--> statement-breakpoint
524+ALTER TABLE `google_discovery_source` ADD `auth_client_id_secret_id` text;--> statement-breakpoint
525+ALTER TABLE `google_discovery_source` ADD `auth_client_secret_secret_id` text;--> statement-breakpoint
526+ALTER TABLE `google_discovery_source` ADD `auth_scopes` text;--> statement-breakpoint
527+CREATE INDEX `google_discovery_source_auth_connection_id_idx` ON `google_discovery_source` (`auth_connection_id`);--> statement-breakpoint
528+CREATE INDEX `google_discovery_source_auth_client_id_secret_id_idx` ON `google_discovery_source` (`auth_client_id_secret_id`);--> statement-breakpoint
529+CREATE INDEX `google_discovery_source_auth_client_secret_secret_id_idx` ON `google_discovery_source` (`auth_client_secret_secret_id`);--> statement-breakpoint
530+
531+-- Backfill auth columns from config.auth.
532+UPDATE `google_discovery_source`
533+SET
534+ `auth_kind` = COALESCE(json_extract(`config`, '$.auth.kind'), 'none'),
535+ `auth_connection_id` = json_extract(`config`, '$.auth.connectionId'),
536+ `auth_client_id_secret_id` = json_extract(`config`, '$.auth.clientIdSecretId'),
537+ `auth_client_secret_secret_id` = json_extract(`config`, '$.auth.clientSecretSecretId'),
538+ `auth_scopes` = json_extract(`config`, '$.auth.scopes')
539+WHERE `config` IS NOT NULL;--> statement-breakpoint
540+
541+-- Backfill credential header / query_param child rows.
542+INSERT OR IGNORE INTO `google_discovery_source_credential_header`
543+ (`scope_id`, `id`, `source_id`, `name`, `kind`, `text_value`, `secret_id`, `secret_prefix`)
544+SELECT
545+ s.`scope_id`,
546+ json_array(s.`id`, h.`key`),
547+ s.`id`,
548+ h.`key`,
549+ CASE
550+ WHEN h.`type` = 'object' AND json_extract(h.`value`, '$.secretId') IS NOT NULL THEN 'secret'
551+ ELSE 'text'
552+ END,
553+ CASE WHEN h.`type` = 'object' THEN NULL ELSE h.`value` END,
554+ CASE WHEN h.`type` = 'object' THEN json_extract(h.`value`, '$.secretId') ELSE NULL END,
555+ CASE WHEN h.`type` = 'object' THEN json_extract(h.`value`, '$.prefix') ELSE NULL END
556+FROM `google_discovery_source` s, json_each(json_extract(s.`config`, '$.credentials.headers')) h
557+WHERE json_extract(s.`config`, '$.credentials.headers') IS NOT NULL;--> statement-breakpoint
558+
559+INSERT OR IGNORE INTO `google_discovery_source_credential_query_param`
560+ (`scope_id`, `id`, `source_id`, `name`, `kind`, `text_value`, `secret_id`, `secret_prefix`)
561+SELECT
562+ s.`scope_id`,
563+ json_array(s.`id`, q.`key`),
564+ s.`id`,
565+ q.`key`,
566+ CASE
567+ WHEN q.`type` = 'object' AND json_extract(q.`value`, '$.secretId') IS NOT NULL THEN 'secret'
568+ ELSE 'text'
569+ END,
570+ CASE WHEN q.`type` = 'object' THEN NULL ELSE q.`value` END,
571+ CASE WHEN q.`type` = 'object' THEN json_extract(q.`value`, '$.secretId') ELSE NULL END,
572+ CASE WHEN q.`type` = 'object' THEN json_extract(q.`value`, '$.prefix') ELSE NULL END
573+FROM `google_discovery_source` s, json_each(json_extract(s.`config`, '$.credentials.queryParams')) q
574+WHERE json_extract(s.`config`, '$.credentials.queryParams') IS NOT NULL;--> statement-breakpoint
575+
576+-- Strip the extracted fields from the legacy config JSON.
577+UPDATE `google_discovery_source`
578+SET `config` = json_remove(`config`, '$.auth', '$.credentials')
579+WHERE `config` IS NOT NULL;
apps/local/drizzle-legacy-v1/0008_scoped_credentials_cutover.sql+1220−0
@@ -0,0 +1,1220 @@
1+--
2+-- Slug-normalization staging table.
3+--
4+-- 0008 originally inlined a 41-deep `replace(replace(... lower(...) ...))`
5+-- chain everywhere it derived a slot_key from a header / param / oauth
6+-- scheme name. bun:sqlite's lemon parser stack overflows on that depth on
7+-- platforms with smaller thread stacks (notably the compiled CLI binary on
8+-- macOS), so we precompute slugs once into a temp table and reference them
9+-- via flat scalar subqueries below.
10+--
11+CREATE TEMP TABLE `__slug_norm` (
12+ `raw` text PRIMARY KEY,
13+ `slug` text NOT NULL DEFAULT ''
14+);--> statement-breakpoint
15+
16+INSERT OR IGNORE INTO `__slug_norm` (`raw`)
17+SELECT DISTINCT `raw` FROM (
18+ SELECT h.`key` AS `raw` FROM `openapi_source` s, json_each(s.`headers`) h
19+ WHERE s.`headers` IS NOT NULL AND h.`type` = 'object'
20+ UNION
21+ SELECT json_extract(s.`oauth2`, '$.securitySchemeName') FROM `openapi_source` s
22+ WHERE s.`oauth2` IS NOT NULL
23+ UNION
24+ SELECT `name` FROM `openapi_source_query_param` WHERE `name` IS NOT NULL
25+ UNION
26+ SELECT `name` FROM `openapi_source_spec_fetch_header` WHERE `name` IS NOT NULL
27+ UNION
28+ SELECT `name` FROM `openapi_source_spec_fetch_query_param` WHERE `name` IS NOT NULL
29+ UNION
30+ SELECT `name` FROM `graphql_source_header` WHERE `name` IS NOT NULL
31+ UNION
32+ SELECT `name` FROM `graphql_source_query_param` WHERE `name` IS NOT NULL
33+ UNION
34+ SELECT `name` FROM `mcp_source_header` WHERE `name` IS NOT NULL
35+ UNION
36+ SELECT `name` FROM `mcp_source_query_param` WHERE `name` IS NOT NULL
37+) WHERE `raw` IS NOT NULL AND `raw` != '';--> statement-breakpoint
38+
39+UPDATE `__slug_norm` SET `slug` = lower(trim(`raw`));--> statement-breakpoint
40+UPDATE `__slug_norm` SET `slug` = replace(`slug`, char(9), '-');--> statement-breakpoint
41+UPDATE `__slug_norm` SET `slug` = replace(`slug`, char(10), '-');--> statement-breakpoint
42+UPDATE `__slug_norm` SET `slug` = replace(`slug`, char(13), '-');--> statement-breakpoint
43+UPDATE `__slug_norm` SET `slug` = replace(`slug`, ' ', '-');--> statement-breakpoint
44+UPDATE `__slug_norm` SET `slug` = replace(`slug`, '_', '-');--> statement-breakpoint
45+UPDATE `__slug_norm` SET `slug` = replace(`slug`, '.', '-');--> statement-breakpoint
46+UPDATE `__slug_norm` SET `slug` = replace(`slug`, ':', '-');--> statement-breakpoint
47+UPDATE `__slug_norm` SET `slug` = replace(`slug`, '/', '-');--> statement-breakpoint
48+UPDATE `__slug_norm` SET `slug` = replace(`slug`, '@', '-');--> statement-breakpoint
49+UPDATE `__slug_norm` SET `slug` = replace(`slug`, '+', '-');--> statement-breakpoint
50+UPDATE `__slug_norm` SET `slug` = replace(`slug`, '&', '-');--> statement-breakpoint
51+UPDATE `__slug_norm` SET `slug` = replace(`slug`, '?', '-');--> statement-breakpoint
52+UPDATE `__slug_norm` SET `slug` = replace(`slug`, '#', '-');--> statement-breakpoint
53+UPDATE `__slug_norm` SET `slug` = replace(`slug`, '%', '-');--> statement-breakpoint
54+UPDATE `__slug_norm` SET `slug` = replace(`slug`, '$', '-');--> statement-breakpoint
55+UPDATE `__slug_norm` SET `slug` = replace(`slug`, ',', '-');--> statement-breakpoint
56+UPDATE `__slug_norm` SET `slug` = replace(`slug`, ';', '-');--> statement-breakpoint
57+UPDATE `__slug_norm` SET `slug` = replace(`slug`, '(', '-');--> statement-breakpoint
58+UPDATE `__slug_norm` SET `slug` = replace(`slug`, ')', '-');--> statement-breakpoint
59+UPDATE `__slug_norm` SET `slug` = replace(`slug`, '[', '-');--> statement-breakpoint
60+UPDATE `__slug_norm` SET `slug` = replace(`slug`, ']', '-');--> statement-breakpoint
61+UPDATE `__slug_norm` SET `slug` = replace(`slug`, '{', '-');--> statement-breakpoint
62+UPDATE `__slug_norm` SET `slug` = replace(`slug`, '}', '-');--> statement-breakpoint
63+UPDATE `__slug_norm` SET `slug` = replace(`slug`, '=', '-');--> statement-breakpoint
64+UPDATE `__slug_norm` SET `slug` = replace(`slug`, '~', '-');--> statement-breakpoint
65+UPDATE `__slug_norm` SET `slug` = replace(`slug`, '!', '-');--> statement-breakpoint
66+UPDATE `__slug_norm` SET `slug` = replace(`slug`, char(34), '-');--> statement-breakpoint
67+UPDATE `__slug_norm` SET `slug` = replace(`slug`, char(39), '-');--> statement-breakpoint
68+UPDATE `__slug_norm` SET `slug` = replace(`slug`, char(92), '-');--> statement-breakpoint
69+UPDATE `__slug_norm` SET `slug` = replace(`slug`, '|', '-');--> statement-breakpoint
70+UPDATE `__slug_norm` SET `slug` = replace(`slug`, '*', '-');--> statement-breakpoint
71+UPDATE `__slug_norm` SET `slug` = replace(`slug`, '<', '-');--> statement-breakpoint
72+UPDATE `__slug_norm` SET `slug` = replace(`slug`, '>', '-');--> statement-breakpoint
73+UPDATE `__slug_norm` SET `slug` = replace(`slug`, '--', '-');--> statement-breakpoint
74+UPDATE `__slug_norm` SET `slug` = replace(`slug`, '--', '-');--> statement-breakpoint
75+UPDATE `__slug_norm` SET `slug` = replace(`slug`, '--', '-');--> statement-breakpoint
76+UPDATE `__slug_norm` SET `slug` = replace(`slug`, '--', '-');--> statement-breakpoint
77+UPDATE `__slug_norm` SET `slug` = replace(`slug`, '--', '-');--> statement-breakpoint
78+UPDATE `__slug_norm` SET `slug` = replace(`slug`, '--', '-');--> statement-breakpoint
79+UPDATE `__slug_norm` SET `slug` = replace(`slug`, '--', '-');--> statement-breakpoint
80+UPDATE `__slug_norm` SET `slug` = replace(`slug`, '--', '-');--> statement-breakpoint
81+UPDATE `__slug_norm` SET `slug` = trim(`slug`, '-');--> statement-breakpoint
82+UPDATE `__slug_norm` SET `slug` = CASE WHEN `slug` = '' THEN 'default' ELSE `slug` END;--> statement-breakpoint
83+
84+-- local scoped credential/source-slot/OAuth cutover.
85+-- Squashes the PR-local migration chain into one runtime schema transition.
86+-- 0008_add_credential_binding.sql
87+CREATE TABLE `credential_binding` (
88+ `id` text NOT NULL,
89+ `scope_id` text NOT NULL,
90+ `plugin_id` text NOT NULL,
91+ `source_id` text NOT NULL,
92+ `source_scope_id` text NOT NULL,
93+ `slot_key` text NOT NULL,
94+ `kind` text NOT NULL,
95+ `text_value` text,
96+ `secret_id` text,
97+ `connection_id` text,
98+ `created_at` integer NOT NULL,
99+ `updated_at` integer NOT NULL,
100+ PRIMARY KEY(`scope_id`, `id`)
101+);
102+--> statement-breakpoint
103+CREATE INDEX `credential_binding_scope_id_idx` ON `credential_binding` (`scope_id`);--> statement-breakpoint
104+CREATE INDEX `credential_binding_plugin_id_idx` ON `credential_binding` (`plugin_id`);--> statement-breakpoint
105+CREATE INDEX `credential_binding_source_id_idx` ON `credential_binding` (`source_id`);--> statement-breakpoint
106+CREATE INDEX `credential_binding_source_scope_id_idx` ON `credential_binding` (`source_scope_id`);--> statement-breakpoint
107+CREATE INDEX `credential_binding_slot_key_idx` ON `credential_binding` (`slot_key`);--> statement-breakpoint
108+CREATE INDEX `credential_binding_kind_idx` ON `credential_binding` (`kind`);--> statement-breakpoint
109+CREATE INDEX `credential_binding_secret_id_idx` ON `credential_binding` (`secret_id`);--> statement-breakpoint
110+CREATE INDEX `credential_binding_connection_id_idx` ON `credential_binding` (`connection_id`);--> statement-breakpoint
111+PRAGMA foreign_keys=OFF;--> statement-breakpoint
112+CREATE TABLE `__new_openapi_source_binding` (
113+ `id` text PRIMARY KEY NOT NULL,
114+ `source_id` text NOT NULL,
115+ `source_scope_id` text NOT NULL,
116+ `target_scope_id` text NOT NULL,
117+ `slot` text NOT NULL,
118+ `kind` text NOT NULL,
119+ `secret_id` text,
120+ `connection_id` text,
121+ `text_value` text,
122+ `created_at` integer NOT NULL,
123+ `updated_at` integer NOT NULL
124+);
125+--> statement-breakpoint
126+INSERT INTO `__new_openapi_source_binding`("id", "source_id", "source_scope_id", "target_scope_id", "slot", "kind", "secret_id", "connection_id", "text_value", "created_at", "updated_at") SELECT "id", "source_id", "source_scope_id", "target_scope_id", "slot", "kind", "secret_id", "connection_id", "text_value", "created_at", "updated_at" FROM `openapi_source_binding`;--> statement-breakpoint
127+DROP TABLE `openapi_source_binding`;--> statement-breakpoint
128+ALTER TABLE `__new_openapi_source_binding` RENAME TO `openapi_source_binding`;--> statement-breakpoint
129+PRAGMA foreign_keys=ON;--> statement-breakpoint
130+CREATE INDEX `openapi_source_binding_source_id_idx` ON `openapi_source_binding` (`source_id`);--> statement-breakpoint
131+CREATE INDEX `openapi_source_binding_source_scope_id_idx` ON `openapi_source_binding` (`source_scope_id`);--> statement-breakpoint
132+CREATE INDEX `openapi_source_binding_target_scope_id_idx` ON `openapi_source_binding` (`target_scope_id`);--> statement-breakpoint
133+CREATE INDEX `openapi_source_binding_slot_idx` ON `openapi_source_binding` (`slot`);--> statement-breakpoint
134+CREATE INDEX `openapi_source_binding_secret_id_idx` ON `openapi_source_binding` (`secret_id`);--> statement-breakpoint
135+CREATE INDEX `openapi_source_binding_connection_id_idx` ON `openapi_source_binding` (`connection_id`);--> statement-breakpoint
136+
137+-- 0009_migrate_openapi_source_bindings.sql
138+INSERT OR REPLACE INTO `credential_binding` (
139+ `id`,
140+ `scope_id`,
141+ `plugin_id`,
142+ `source_id`,
143+ `source_scope_id`,
144+ `slot_key`,
145+ `kind`,
146+ `text_value`,
147+ `secret_id`,
148+ `connection_id`,
149+ `created_at`,
150+ `updated_at`
151+)
152+SELECT
153+ json_array('openapi', `source_scope_id`, `source_id`, `slot`),
154+ `target_scope_id`,
155+ 'openapi',
156+ `source_id`,
157+ `source_scope_id`,
158+ `slot`,
159+ `kind`,
160+ `text_value`,
161+ `secret_id`,
162+ `connection_id`,
163+ `created_at`,
164+ `updated_at`
165+FROM `openapi_source_binding`;--> statement-breakpoint
166+DROP TABLE `openapi_source_binding`;--> statement-breakpoint
167+
168+-- 0010_openapi_credential_slots.sql
169+-- Convert OpenAPI's remaining direct credential references to source-owned
170+-- slot structure plus shared core credential_binding rows. Runtime code only
171+-- reads the final slot model; this migration is the one-shot bridge.
172+
173+-- Existing header JSON may still contain SecretBackedValue objects. Preserve
174+-- the source-owned header names, move secret ids to credential_binding, and
175+-- rewrite each object to { kind: "binding", slot, prefix? }.
176+CREATE TEMP TABLE `__openapi_header_slot_preflight` (
177+ `scope_id` text NOT NULL,
178+ `source_id` text NOT NULL,
179+ `slot_key` text NOT NULL,
180+ PRIMARY KEY (`scope_id`, `source_id`, `slot_key`)
181+);--> statement-breakpoint
182+
183+INSERT INTO `__openapi_header_slot_preflight` (`scope_id`, `source_id`, `slot_key`)
184+SELECT
185+ s.`scope_id`,
186+ s.`id`,
187+ 'header:' || COALESCE((SELECT `slug` FROM `__slug_norm` WHERE `raw` = h.`key`), 'default') AS `slot_key`
188+FROM `openapi_source` s, json_each(s.`headers`) h
189+WHERE s.`headers` IS NOT NULL
190+ AND h.`type` = 'object'
191+ AND json_extract(h.`value`, '$.kind') IS NULL
192+ AND json_extract(h.`value`, '$.secretId') IS NOT NULL;--> statement-breakpoint
193+
194+DROP TABLE `__openapi_header_slot_preflight`;--> statement-breakpoint
195+
196+WITH header_rows AS (
197+ SELECT
198+ s.`scope_id`,
199+ s.`id` AS `source_id`,
200+ h.`key` AS `name`,
201+ 'header:' || COALESCE((SELECT `slug` FROM `__slug_norm` WHERE `raw` = h.`key`), 'default') AS `slot_key`,
202+ json_extract(h.`value`, '$.secretId') AS `secret_id`
203+ FROM `openapi_source` s, json_each(s.`headers`) h
204+ WHERE s.`headers` IS NOT NULL
205+ AND h.`type` = 'object'
206+ AND json_extract(h.`value`, '$.kind') IS NULL
207+ AND json_extract(h.`value`, '$.secretId') IS NOT NULL
208+)
209+INSERT OR REPLACE INTO `credential_binding` (
210+ `id`, `scope_id`, `plugin_id`, `source_id`, `source_scope_id`, `slot_key`,
211+ `kind`, `text_value`, `secret_id`, `connection_id`, `created_at`, `updated_at`
212+)
213+SELECT
214+ json_array('openapi', r.`scope_id`, r.`source_id`, r.`slot_key`),
215+ r.`scope_id`,
216+ 'openapi',
217+ r.`source_id`,
218+ r.`scope_id`,
219+ r.`slot_key`,
220+ 'secret',
221+ NULL,
222+ r.`secret_id`,
223+ NULL,
224+ CAST(strftime('%s', 'now') AS INTEGER) * 1000,
225+ CAST(strftime('%s', 'now') AS INTEGER) * 1000
226+FROM header_rows r
227+WHERE NOT EXISTS (
228+ SELECT 1 FROM `credential_binding` b
229+ WHERE b.`scope_id` = r.`scope_id`
230+ AND b.`plugin_id` = 'openapi'
231+ AND b.`source_id` = r.`source_id`
232+ AND b.`source_scope_id` = r.`scope_id`
233+ AND b.`slot_key` = r.`slot_key`
234+);--> statement-breakpoint
235+
236+UPDATE `openapi_source`
237+SET `headers` = (
238+ SELECT json_group_object(
239+ h.`key`,
240+ CASE
241+ WHEN h.`type` = 'object'
242+ AND json_extract(h.`value`, '$.kind') IS NULL
243+ AND json_extract(h.`value`, '$.secretId') IS NOT NULL
244+ AND json_extract(h.`value`, '$.prefix') IS NOT NULL
245+ THEN json_object(
246+ 'kind', 'binding',
247+ 'slot', 'header:' || COALESCE((SELECT `slug` FROM `__slug_norm` WHERE `raw` = h.`key`), 'default'),
248+ 'prefix', json_extract(h.`value`, '$.prefix')
249+ )
250+ WHEN h.`type` = 'object'
251+ AND json_extract(h.`value`, '$.kind') IS NULL
252+ AND json_extract(h.`value`, '$.secretId') IS NOT NULL
253+ THEN json_object(
254+ 'kind', 'binding',
255+ 'slot', 'header:' || COALESCE((SELECT `slug` FROM `__slug_norm` WHERE `raw` = h.`key`), 'default')
256+ )
257+ WHEN h.`type` IN ('object', 'array') THEN json(h.`value`)
258+ ELSE h.`value`
259+ END
260+ )
261+ FROM json_each(`openapi_source`.`headers`) h
262+)
263+WHERE `headers` IS NOT NULL;--> statement-breakpoint
264+
265+-- OAuth2Auth JSON becomes OAuth2SourceConfig JSON plus explicit bindings for
266+-- the client id, optional client secret, and live connection id.
267+WITH oauth_rows AS (
268+ SELECT
269+ s.`scope_id`,
270+ s.`id` AS `source_id`,
271+ json_extract(s.`oauth2`, '$.securitySchemeName') AS `security_scheme_name`,
272+ 'oauth2:' || COALESCE((SELECT `slug` FROM `__slug_norm` WHERE `raw` = json_extract(s.`oauth2`, '$.securitySchemeName')), 'default') || ':client-id' AS `client_id_slot`,
273+ json_extract(s.`oauth2`, '$.clientIdSecretId') AS `client_id_secret_id`,
274+ json_extract(s.`oauth2`, '$.connectionId') AS `connection_id`
275+ FROM `openapi_source` s
276+ WHERE s.`oauth2` IS NOT NULL
277+ AND json_extract(s.`oauth2`, '$.connectionId') IS NOT NULL
278+ AND json_extract(s.`oauth2`, '$.clientIdSecretId') IS NOT NULL
279+)
280+INSERT OR REPLACE INTO `credential_binding` (
281+ `id`, `scope_id`, `plugin_id`, `source_id`, `source_scope_id`, `slot_key`,
282+ `kind`, `text_value`, `secret_id`, `connection_id`, `created_at`, `updated_at`
283+)
284+SELECT
285+ json_array('openapi', r.`scope_id`, r.`source_id`, r.`client_id_slot`),
286+ r.`scope_id`,
287+ 'openapi',
288+ r.`source_id`,
289+ r.`scope_id`,
290+ r.`client_id_slot`,
291+ 'secret',
292+ NULL,
293+ r.`client_id_secret_id`,
294+ NULL,
295+ CAST(strftime('%s', 'now') AS INTEGER) * 1000,
296+ CAST(strftime('%s', 'now') AS INTEGER) * 1000
297+FROM oauth_rows r
298+WHERE NOT EXISTS (
299+ SELECT 1 FROM `credential_binding` b
300+ WHERE b.`scope_id` = r.`scope_id`
301+ AND b.`plugin_id` = 'openapi'
302+ AND b.`source_id` = r.`source_id`
303+ AND b.`source_scope_id` = r.`scope_id`
304+ AND b.`slot_key` = r.`client_id_slot`
305+);--> statement-breakpoint
306+
307+WITH oauth_rows AS (
308+ SELECT
309+ s.`scope_id`,
310+ s.`id` AS `source_id`,
311+ 'oauth2:' || COALESCE((SELECT `slug` FROM `__slug_norm` WHERE `raw` = json_extract(s.`oauth2`, '$.securitySchemeName')), 'default') || ':client-secret' AS `client_secret_slot`,
312+ json_extract(s.`oauth2`, '$.clientSecretSecretId') AS `client_secret_secret_id`
313+ FROM `openapi_source` s
314+ WHERE s.`oauth2` IS NOT NULL
315+ AND json_extract(s.`oauth2`, '$.connectionId') IS NOT NULL
316+ AND json_extract(s.`oauth2`, '$.clientSecretSecretId') IS NOT NULL
317+)
318+INSERT OR REPLACE INTO `credential_binding` (
319+ `id`, `scope_id`, `plugin_id`, `source_id`, `source_scope_id`, `slot_key`,
320+ `kind`, `text_value`, `secret_id`, `connection_id`, `created_at`, `updated_at`
321+)
322+SELECT
323+ json_array('openapi', r.`scope_id`, r.`source_id`, r.`client_secret_slot`),
324+ r.`scope_id`,
325+ 'openapi',
326+ r.`source_id`,
327+ r.`scope_id`,
328+ r.`client_secret_slot`,
329+ 'secret',
330+ NULL,
331+ r.`client_secret_secret_id`,
332+ NULL,
333+ CAST(strftime('%s', 'now') AS INTEGER) * 1000,
334+ CAST(strftime('%s', 'now') AS INTEGER) * 1000
335+FROM oauth_rows r
336+WHERE NOT EXISTS (
337+ SELECT 1 FROM `credential_binding` b
338+ WHERE b.`scope_id` = r.`scope_id`
339+ AND b.`plugin_id` = 'openapi'
340+ AND b.`source_id` = r.`source_id`
341+ AND b.`source_scope_id` = r.`scope_id`
342+ AND b.`slot_key` = r.`client_secret_slot`
343+);--> statement-breakpoint
344+
345+WITH oauth_rows AS (
346+ SELECT
347+ s.`scope_id`,
348+ s.`id` AS `source_id`,
349+ 'oauth2:' || COALESCE((SELECT `slug` FROM `__slug_norm` WHERE `raw` = json_extract(s.`oauth2`, '$.securitySchemeName')), 'default') || ':connection' AS `connection_slot`,
350+ json_extract(s.`oauth2`, '$.connectionId') AS `connection_id`
351+ FROM `openapi_source` s
352+ WHERE s.`oauth2` IS NOT NULL
353+ AND json_extract(s.`oauth2`, '$.connectionId') IS NOT NULL
354+)
355+INSERT OR REPLACE INTO `credential_binding` (
356+ `id`, `scope_id`, `plugin_id`, `source_id`, `source_scope_id`, `slot_key`,
357+ `kind`, `text_value`, `secret_id`, `connection_id`, `created_at`, `updated_at`
358+)
359+SELECT
360+ json_array('openapi', r.`scope_id`, r.`source_id`, r.`connection_slot`),
361+ r.`scope_id`,
362+ 'openapi',
363+ r.`source_id`,
364+ r.`scope_id`,
365+ r.`connection_slot`,
366+ 'connection',
367+ NULL,
368+ NULL,
369+ r.`connection_id`,
370+ CAST(strftime('%s', 'now') AS INTEGER) * 1000,
371+ CAST(strftime('%s', 'now') AS INTEGER) * 1000
372+FROM oauth_rows r
373+WHERE NOT EXISTS (
374+ SELECT 1 FROM `credential_binding` b
375+ WHERE b.`scope_id` = r.`scope_id`
376+ AND b.`plugin_id` = 'openapi'
377+ AND b.`source_id` = r.`source_id`
378+ AND b.`source_scope_id` = r.`scope_id`
379+ AND b.`slot_key` = r.`connection_slot`
380+);--> statement-breakpoint
381+
382+UPDATE `openapi_source`
383+SET `oauth2` = json_object(
384+ 'kind', 'oauth2',
385+ 'securitySchemeName', json_extract(`oauth2`, '$.securitySchemeName'),
386+ 'flow', json_extract(`oauth2`, '$.flow'),
387+ 'tokenUrl', json_extract(`oauth2`, '$.tokenUrl'),
388+ 'authorizationUrl', json_extract(`oauth2`, '$.authorizationUrl'),
389+ 'issuerUrl', json_extract(`oauth2`, '$.issuerUrl'),
390+ 'clientIdSlot', 'oauth2:' || COALESCE((SELECT `slug` FROM `__slug_norm` WHERE `raw` = json_extract(`oauth2`, '$.securitySchemeName')), 'default') || ':client-id',
391+ 'clientSecretSlot',
392+ CASE
393+ WHEN json_extract(`oauth2`, '$.clientSecretSecretId') IS NULL THEN NULL
394+ ELSE 'oauth2:' || COALESCE((SELECT `slug` FROM `__slug_norm` WHERE `raw` = json_extract(`oauth2`, '$.securitySchemeName')), 'default') || ':client-secret'
395+ END,
396+ 'connectionSlot', 'oauth2:' || COALESCE((SELECT `slug` FROM `__slug_norm` WHERE `raw` = json_extract(`oauth2`, '$.securitySchemeName')), 'default') || ':connection',
397+ 'scopes', json(COALESCE(json_extract(`oauth2`, '$.scopes'), '[]'))
398+)
399+WHERE `oauth2` IS NOT NULL
400+ AND json_extract(`oauth2`, '$.connectionId') IS NOT NULL;--> statement-breakpoint
401+
402+-- Child credential rows become source-owned binding slots.
403+CREATE TEMP TABLE `__openapi_query_param_slot_preflight` (
404+ `scope_id` text NOT NULL,
405+ `source_id` text NOT NULL,
406+ `slot_key` text NOT NULL,
407+ PRIMARY KEY (`scope_id`, `source_id`, `slot_key`)
408+);--> statement-breakpoint
409+
410+INSERT INTO `__openapi_query_param_slot_preflight` (`scope_id`, `source_id`, `slot_key`)
411+SELECT
412+ r.`scope_id`,
413+ r.`source_id`,
414+ 'query_param:' || COALESCE((SELECT `slug` FROM `__slug_norm` WHERE `raw` = r.`name`), 'default') AS `slot_key`
415+FROM `openapi_source_query_param` r
416+WHERE r.`kind` = 'secret' AND r.`secret_id` IS NOT NULL;--> statement-breakpoint
417+
418+DROP TABLE `__openapi_query_param_slot_preflight`;--> statement-breakpoint
419+
420+WITH rows AS (
421+ SELECT
422+ r.`scope_id`,
423+ r.`source_id`,
424+ 'query_param:' || COALESCE((SELECT `slug` FROM `__slug_norm` WHERE `raw` = r.`name`), 'default') AS `slot_key`,
425+ r.`secret_id`
426+ FROM `openapi_source_query_param` r
427+ WHERE r.`kind` = 'secret' AND r.`secret_id` IS NOT NULL
428+)
429+INSERT OR REPLACE INTO `credential_binding` (
430+ `id`, `scope_id`, `plugin_id`, `source_id`, `source_scope_id`, `slot_key`,
431+ `kind`, `text_value`, `secret_id`, `connection_id`, `created_at`, `updated_at`
432+)
433+SELECT
434+ json_array('openapi', r.`scope_id`, r.`source_id`, r.`slot_key`),
435+ r.`scope_id`,
436+ 'openapi',
437+ r.`source_id`,
438+ r.`scope_id`,
439+ r.`slot_key`,
440+ 'secret',
441+ NULL,
442+ r.`secret_id`,
443+ NULL,
444+ CAST(strftime('%s', 'now') AS INTEGER) * 1000,
445+ CAST(strftime('%s', 'now') AS INTEGER) * 1000
446+FROM rows r
447+WHERE NOT EXISTS (
448+ SELECT 1 FROM `credential_binding` b
449+ WHERE b.`scope_id` = r.`scope_id`
450+ AND b.`plugin_id` = 'openapi'
451+ AND b.`source_id` = r.`source_id`
452+ AND b.`source_scope_id` = r.`scope_id`
453+ AND b.`slot_key` = r.`slot_key`
454+);--> statement-breakpoint
455+
456+ALTER TABLE `openapi_source_query_param` ADD `slot_key` text;--> statement-breakpoint
457+ALTER TABLE `openapi_source_query_param` ADD `prefix` text;--> statement-breakpoint
458+UPDATE `openapi_source_query_param`
459+SET
460+ `slot_key` = 'query_param:' || COALESCE((SELECT `slug` FROM `__slug_norm` WHERE `raw` = `name`), 'default'),
461+ `prefix` = `secret_prefix`,
462+ `kind` = 'binding'
463+WHERE `kind` = 'secret';--> statement-breakpoint
464+DROP INDEX `openapi_source_query_param_secret_id_idx`;--> statement-breakpoint
465+ALTER TABLE `openapi_source_query_param` DROP COLUMN `secret_id`;--> statement-breakpoint
466+ALTER TABLE `openapi_source_query_param` DROP COLUMN `secret_prefix`;--> statement-breakpoint
467+
468+CREATE TEMP TABLE `__openapi_spec_fetch_header_slot_preflight` (
469+ `scope_id` text NOT NULL,
470+ `source_id` text NOT NULL,
471+ `slot_key` text NOT NULL,
472+ PRIMARY KEY (`scope_id`, `source_id`, `slot_key`)
473+);--> statement-breakpoint
474+
475+INSERT INTO `__openapi_spec_fetch_header_slot_preflight` (`scope_id`, `source_id`, `slot_key`)
476+SELECT
477+ r.`scope_id`,
478+ r.`source_id`,
479+ 'spec_fetch_header:' || COALESCE((SELECT `slug` FROM `__slug_norm` WHERE `raw` = r.`name`), 'default') AS `slot_key`
480+FROM `openapi_source_spec_fetch_header` r
481+WHERE r.`kind` = 'secret' AND r.`secret_id` IS NOT NULL;--> statement-breakpoint
482+
483+DROP TABLE `__openapi_spec_fetch_header_slot_preflight`;--> statement-breakpoint
484+
485+WITH rows AS (
486+ SELECT
487+ r.`scope_id`,
488+ r.`source_id`,
489+ 'spec_fetch_header:' || COALESCE((SELECT `slug` FROM `__slug_norm` WHERE `raw` = r.`name`), 'default') AS `slot_key`,
490+ r.`secret_id`
491+ FROM `openapi_source_spec_fetch_header` r
492+ WHERE r.`kind` = 'secret' AND r.`secret_id` IS NOT NULL
493+)
494+INSERT OR REPLACE INTO `credential_binding` (
495+ `id`, `scope_id`, `plugin_id`, `source_id`, `source_scope_id`, `slot_key`,
496+ `kind`, `text_value`, `secret_id`, `connection_id`, `created_at`, `updated_at`
497+)
498+SELECT
499+ json_array('openapi', r.`scope_id`, r.`source_id`, r.`slot_key`),
500+ r.`scope_id`,
501+ 'openapi',
502+ r.`source_id`,
503+ r.`scope_id`,
504+ r.`slot_key`,
505+ 'secret',
506+ NULL,
507+ r.`secret_id`,
508+ NULL,
509+ CAST(strftime('%s', 'now') AS INTEGER) * 1000,
510+ CAST(strftime('%s', 'now') AS INTEGER) * 1000
511+FROM rows r
512+WHERE NOT EXISTS (
513+ SELECT 1 FROM `credential_binding` b
514+ WHERE b.`scope_id` = r.`scope_id`
515+ AND b.`plugin_id` = 'openapi'
516+ AND b.`source_id` = r.`source_id`
517+ AND b.`source_scope_id` = r.`scope_id`
518+ AND b.`slot_key` = r.`slot_key`
519+);--> statement-breakpoint
520+
521+ALTER TABLE `openapi_source_spec_fetch_header` ADD `slot_key` text;--> statement-breakpoint
522+ALTER TABLE `openapi_source_spec_fetch_header` ADD `prefix` text;--> statement-breakpoint
523+UPDATE `openapi_source_spec_fetch_header`
524+SET
525+ `slot_key` = 'spec_fetch_header:' || COALESCE((SELECT `slug` FROM `__slug_norm` WHERE `raw` = `name`), 'default'),
526+ `prefix` = `secret_prefix`,
527+ `kind` = 'binding'
528+WHERE `kind` = 'secret';--> statement-breakpoint
529+DROP INDEX `openapi_source_spec_fetch_header_secret_id_idx`;--> statement-breakpoint
530+ALTER TABLE `openapi_source_spec_fetch_header` DROP COLUMN `secret_id`;--> statement-breakpoint
531+ALTER TABLE `openapi_source_spec_fetch_header` DROP COLUMN `secret_prefix`;--> statement-breakpoint
532+
533+CREATE TEMP TABLE `__openapi_spec_fetch_query_param_slot_preflight` (
534+ `scope_id` text NOT NULL,
535+ `source_id` text NOT NULL,
536+ `slot_key` text NOT NULL,
537+ PRIMARY KEY (`scope_id`, `source_id`, `slot_key`)
538+);--> statement-breakpoint
539+
540+INSERT INTO `__openapi_spec_fetch_query_param_slot_preflight` (`scope_id`, `source_id`, `slot_key`)
541+SELECT
542+ r.`scope_id`,
543+ r.`source_id`,
544+ 'spec_fetch_query_param:' || COALESCE((SELECT `slug` FROM `__slug_norm` WHERE `raw` = r.`name`), 'default') AS `slot_key`
545+FROM `openapi_source_spec_fetch_query_param` r
546+WHERE r.`kind` = 'secret' AND r.`secret_id` IS NOT NULL;--> statement-breakpoint
547+
548+DROP TABLE `__openapi_spec_fetch_query_param_slot_preflight`;--> statement-breakpoint
549+
550+WITH rows AS (
551+ SELECT
552+ r.`scope_id`,
553+ r.`source_id`,
554+ 'spec_fetch_query_param:' || COALESCE((SELECT `slug` FROM `__slug_norm` WHERE `raw` = r.`name`), 'default') AS `slot_key`,
555+ r.`secret_id`
556+ FROM `openapi_source_spec_fetch_query_param` r
557+ WHERE r.`kind` = 'secret' AND r.`secret_id` IS NOT NULL
558+)
559+INSERT OR REPLACE INTO `credential_binding` (
560+ `id`, `scope_id`, `plugin_id`, `source_id`, `source_scope_id`, `slot_key`,
561+ `kind`, `text_value`, `secret_id`, `connection_id`, `created_at`, `updated_at`
562+)
563+SELECT
564+ json_array('openapi', r.`scope_id`, r.`source_id`, r.`slot_key`),
565+ r.`scope_id`,
566+ 'openapi',
567+ r.`source_id`,
568+ r.`scope_id`,
569+ r.`slot_key`,
570+ 'secret',
571+ NULL,
572+ r.`secret_id`,
573+ NULL,
574+ CAST(strftime('%s', 'now') AS INTEGER) * 1000,
575+ CAST(strftime('%s', 'now') AS INTEGER) * 1000
576+FROM rows r
577+WHERE NOT EXISTS (
578+ SELECT 1 FROM `credential_binding` b
579+ WHERE b.`scope_id` = r.`scope_id`
580+ AND b.`plugin_id` = 'openapi'
581+ AND b.`source_id` = r.`source_id`
582+ AND b.`source_scope_id` = r.`scope_id`
583+ AND b.`slot_key` = r.`slot_key`
584+);--> statement-breakpoint
585+
586+ALTER TABLE `openapi_source_spec_fetch_query_param` ADD `slot_key` text;--> statement-breakpoint
587+ALTER TABLE `openapi_source_spec_fetch_query_param` ADD `prefix` text;--> statement-breakpoint
588+UPDATE `openapi_source_spec_fetch_query_param`
589+SET
590+ `slot_key` = 'spec_fetch_query_param:' || COALESCE((SELECT `slug` FROM `__slug_norm` WHERE `raw` = `name`), 'default'),
591+ `prefix` = `secret_prefix`,
592+ `kind` = 'binding'
593+WHERE `kind` = 'secret';--> statement-breakpoint
594+DROP INDEX `openapi_source_spec_fetch_query_param_secret_id_idx`;--> statement-breakpoint
595+ALTER TABLE `openapi_source_spec_fetch_query_param` DROP COLUMN `secret_id`;--> statement-breakpoint
596+ALTER TABLE `openapi_source_spec_fetch_query_param` DROP COLUMN `secret_prefix`;--> statement-breakpoint
597+
598+-- 0011_graphql_credential_slots.sql
599+-- Convert GraphQL's direct credential references to source-owned slot
600+-- structure plus shared core credential_binding rows. Runtime code only
601+-- reads the final slot model; this migration is the one-shot bridge.
602+
603+ALTER TABLE `graphql_source` ADD `auth_connection_slot` text;--> statement-breakpoint
604+
605+UPDATE `graphql_source`
606+SET `auth_connection_slot` = 'auth:oauth2:connection'
607+WHERE `auth_kind` = 'oauth2'
608+ AND `auth_connection_id` IS NOT NULL;--> statement-breakpoint
609+
610+INSERT OR REPLACE INTO `credential_binding` (
611+ `id`, `scope_id`, `plugin_id`, `source_id`, `source_scope_id`, `slot_key`,
612+ `kind`, `text_value`, `secret_id`, `connection_id`, `created_at`, `updated_at`
613+)
614+SELECT
615+ json_array('graphql', s.`scope_id`, s.`id`, 'auth:oauth2:connection'),
616+ s.`scope_id`,
617+ 'graphql',
618+ s.`id`,
619+ s.`scope_id`,
620+ 'auth:oauth2:connection',
621+ 'connection',
622+ NULL,
623+ NULL,
624+ s.`auth_connection_id`,
625+ CAST(strftime('%s', 'now') AS INTEGER) * 1000,
626+ CAST(strftime('%s', 'now') AS INTEGER) * 1000
627+FROM `graphql_source` s
628+WHERE s.`auth_kind` = 'oauth2'
629+ AND s.`auth_connection_id` IS NOT NULL;--> statement-breakpoint
630+
631+DROP INDEX `graphql_source_auth_connection_id_idx`;--> statement-breakpoint
632+ALTER TABLE `graphql_source` DROP COLUMN `auth_connection_id`;--> statement-breakpoint
633+
634+ALTER TABLE `graphql_source_header` ADD `slot_key` text;--> statement-breakpoint
635+ALTER TABLE `graphql_source_header` ADD `prefix` text;--> statement-breakpoint
636+
637+CREATE TEMP TABLE `__graphql_header_slot_preflight` (
638+ `scope_id` text NOT NULL,
639+ `source_id` text NOT NULL,
640+ `slot_key` text NOT NULL,
641+ PRIMARY KEY (`scope_id`, `source_id`, `slot_key`)
642+);--> statement-breakpoint
643+
644+INSERT INTO `__graphql_header_slot_preflight` (`scope_id`, `source_id`, `slot_key`)
645+SELECT
646+ h.`scope_id`,
647+ h.`source_id`,
648+ 'header:' || COALESCE((SELECT `slug` FROM `__slug_norm` WHERE `raw` = h.`name`), 'default') AS `slot_key`
649+FROM `graphql_source_header` h
650+WHERE h.`kind` = 'secret'
651+ AND h.`secret_id` IS NOT NULL;--> statement-breakpoint
652+
653+DROP TABLE `__graphql_header_slot_preflight`;--> statement-breakpoint
654+
655+WITH header_rows AS (
656+ SELECT
657+ h.`scope_id`,
658+ h.`source_id`,
659+ h.`name`,
660+ 'header:' || COALESCE((SELECT `slug` FROM `__slug_norm` WHERE `raw` = h.`name`), 'default') AS `slot_key`,
661+ h.`secret_id`,
662+ h.`secret_prefix`
663+ FROM `graphql_source_header` h
664+ WHERE h.`kind` = 'secret'
665+ AND h.`secret_id` IS NOT NULL
666+)
667+INSERT OR REPLACE INTO `credential_binding` (
668+ `id`, `scope_id`, `plugin_id`, `source_id`, `source_scope_id`, `slot_key`,
669+ `kind`, `text_value`, `secret_id`, `connection_id`, `created_at`, `updated_at`
670+)
671+SELECT
672+ json_array('graphql', r.`scope_id`, r.`source_id`, r.`slot_key`),
673+ r.`scope_id`,
674+ 'graphql',
675+ r.`source_id`,
676+ r.`scope_id`,
677+ r.`slot_key`,
678+ 'secret',
679+ NULL,
680+ r.`secret_id`,
681+ NULL,
682+ CAST(strftime('%s', 'now') AS INTEGER) * 1000,
683+ CAST(strftime('%s', 'now') AS INTEGER) * 1000
684+FROM header_rows r;--> statement-breakpoint
685+
686+UPDATE `graphql_source_header`
687+SET
688+ `kind` = 'binding',
689+ `slot_key` = 'header:' || COALESCE((SELECT `slug` FROM `__slug_norm` WHERE `raw` = `name`), 'default'),
690+ `prefix` = `secret_prefix`,
691+ `text_value` = NULL
692+WHERE `kind` = 'secret'
693+ AND `secret_id` IS NOT NULL;--> statement-breakpoint
694+
695+DROP INDEX `graphql_source_header_secret_id_idx`;--> statement-breakpoint
696+ALTER TABLE `graphql_source_header` DROP COLUMN `secret_id`;--> statement-breakpoint
697+ALTER TABLE `graphql_source_header` DROP COLUMN `secret_prefix`;--> statement-breakpoint
698+
699+ALTER TABLE `graphql_source_query_param` ADD `slot_key` text;--> statement-breakpoint
700+ALTER TABLE `graphql_source_query_param` ADD `prefix` text;--> statement-breakpoint
701+
702+CREATE TEMP TABLE `__graphql_query_param_slot_preflight` (
703+ `scope_id` text NOT NULL,
704+ `source_id` text NOT NULL,
705+ `slot_key` text NOT NULL,
706+ PRIMARY KEY (`scope_id`, `source_id`, `slot_key`)
707+);--> statement-breakpoint
708+
709+INSERT INTO `__graphql_query_param_slot_preflight` (`scope_id`, `source_id`, `slot_key`)
710+SELECT
711+ q.`scope_id`,
712+ q.`source_id`,
713+ 'query_param:' || COALESCE((SELECT `slug` FROM `__slug_norm` WHERE `raw` = q.`name`), 'default') AS `slot_key`
714+FROM `graphql_source_query_param` q
715+WHERE q.`kind` = 'secret'
716+ AND q.`secret_id` IS NOT NULL;--> statement-breakpoint
717+
718+DROP TABLE `__graphql_query_param_slot_preflight`;--> statement-breakpoint
719+
720+WITH query_param_rows AS (
721+ SELECT
722+ q.`scope_id`,
723+ q.`source_id`,
724+ q.`name`,
725+ 'query_param:' || COALESCE((SELECT `slug` FROM `__slug_norm` WHERE `raw` = q.`name`), 'default') AS `slot_key`,
726+ q.`secret_id`,
727+ q.`secret_prefix`
728+ FROM `graphql_source_query_param` q
729+ WHERE q.`kind` = 'secret'
730+ AND q.`secret_id` IS NOT NULL
731+)
732+INSERT OR REPLACE INTO `credential_binding` (
733+ `id`, `scope_id`, `plugin_id`, `source_id`, `source_scope_id`, `slot_key`,
734+ `kind`, `text_value`, `secret_id`, `connection_id`, `created_at`, `updated_at`
735+)
736+SELECT
737+ json_array('graphql', r.`scope_id`, r.`source_id`, r.`slot_key`),
738+ r.`scope_id`,
739+ 'graphql',
740+ r.`source_id`,
741+ r.`scope_id`,
742+ r.`slot_key`,
743+ 'secret',
744+ NULL,
745+ r.`secret_id`,
746+ NULL,
747+ CAST(strftime('%s', 'now') AS INTEGER) * 1000,
748+ CAST(strftime('%s', 'now') AS INTEGER) * 1000
749+FROM query_param_rows r;--> statement-breakpoint
750+
751+UPDATE `graphql_source_query_param`
752+SET
753+ `kind` = 'binding',
754+ `slot_key` = 'query_param:' || COALESCE((SELECT `slug` FROM `__slug_norm` WHERE `raw` = `name`), 'default'),
755+ `prefix` = `secret_prefix`,
756+ `text_value` = NULL
757+WHERE `kind` = 'secret'
758+ AND `secret_id` IS NOT NULL;--> statement-breakpoint
759+
760+DROP INDEX `graphql_source_query_param_secret_id_idx`;--> statement-breakpoint
761+ALTER TABLE `graphql_source_query_param` DROP COLUMN `secret_id`;--> statement-breakpoint
762+ALTER TABLE `graphql_source_query_param` DROP COLUMN `secret_prefix`;--> statement-breakpoint
763+
764+-- 0012_mcp_credential_slots.sql
765+-- Convert MCP direct credential references to source-owned slot structure
766+-- plus shared core credential_binding rows. Runtime code only reads the
767+-- final slot model; this migration is the one-shot bridge from old data.
768+
769+ALTER TABLE `mcp_source` ADD `auth_header_slot` text;--> statement-breakpoint
770+ALTER TABLE `mcp_source` ADD `auth_header_prefix` text;--> statement-breakpoint
771+ALTER TABLE `mcp_source` ADD `auth_connection_slot` text;--> statement-breakpoint
772+ALTER TABLE `mcp_source` ADD `auth_client_id_slot` text;--> statement-breakpoint
773+ALTER TABLE `mcp_source` ADD `auth_client_secret_slot` text;--> statement-breakpoint
774+
775+UPDATE `mcp_source`
776+SET
777+ `auth_header_slot` = 'auth:header',
778+ `auth_header_prefix` = `auth_secret_prefix`
779+WHERE `auth_kind` = 'header'
780+ AND `auth_secret_id` IS NOT NULL;--> statement-breakpoint
781+
782+INSERT OR REPLACE INTO `credential_binding` (
783+ `id`, `scope_id`, `plugin_id`, `source_id`, `source_scope_id`, `slot_key`,
784+ `kind`, `text_value`, `secret_id`, `connection_id`, `created_at`, `updated_at`
785+)
786+SELECT
787+ json_array('mcp', s.`scope_id`, s.`id`, 'auth:header'),
788+ s.`scope_id`,
789+ 'mcp',
790+ s.`id`,
791+ s.`scope_id`,
792+ 'auth:header',
793+ 'secret',
794+ NULL,
795+ s.`auth_secret_id`,
796+ NULL,
797+ CAST(strftime('%s', 'now') AS INTEGER) * 1000,
798+ CAST(strftime('%s', 'now') AS INTEGER) * 1000
799+FROM `mcp_source` s
800+WHERE s.`auth_kind` = 'header'
801+ AND s.`auth_secret_id` IS NOT NULL;--> statement-breakpoint
802+
803+UPDATE `mcp_source`
804+SET
805+ `auth_connection_slot` = 'auth:oauth2:connection',
806+ `auth_client_id_slot` = CASE
807+ WHEN `auth_client_id_secret_id` IS NOT NULL THEN 'auth:oauth2:client-id'
808+ ELSE NULL
809+ END,
810+ `auth_client_secret_slot` = CASE
811+ WHEN `auth_client_secret_secret_id` IS NOT NULL THEN 'auth:oauth2:client-secret'
812+ ELSE NULL
813+ END
814+WHERE `auth_kind` = 'oauth2'
815+ AND `auth_connection_id` IS NOT NULL;--> statement-breakpoint
816+
817+INSERT OR REPLACE INTO `credential_binding` (
818+ `id`, `scope_id`, `plugin_id`, `source_id`, `source_scope_id`, `slot_key`,
819+ `kind`, `text_value`, `secret_id`, `connection_id`, `created_at`, `updated_at`
820+)
821+SELECT
822+ json_array('mcp', s.`scope_id`, s.`id`, 'auth:oauth2:connection'),
823+ s.`scope_id`,
824+ 'mcp',
825+ s.`id`,
826+ s.`scope_id`,
827+ 'auth:oauth2:connection',
828+ 'connection',
829+ NULL,
830+ NULL,
831+ s.`auth_connection_id`,
832+ CAST(strftime('%s', 'now') AS INTEGER) * 1000,
833+ CAST(strftime('%s', 'now') AS INTEGER) * 1000
834+FROM `mcp_source` s
835+WHERE s.`auth_kind` = 'oauth2'
836+ AND s.`auth_connection_id` IS NOT NULL;--> statement-breakpoint
837+
838+INSERT OR REPLACE INTO `credential_binding` (
839+ `id`, `scope_id`, `plugin_id`, `source_id`, `source_scope_id`, `slot_key`,
840+ `kind`, `text_value`, `secret_id`, `connection_id`, `created_at`, `updated_at`
841+)
842+SELECT
843+ json_array('mcp', s.`scope_id`, s.`id`, 'auth:oauth2:client-id'),
844+ s.`scope_id`,
845+ 'mcp',
846+ s.`id`,
847+ s.`scope_id`,
848+ 'auth:oauth2:client-id',
849+ 'secret',
850+ NULL,
851+ s.`auth_client_id_secret_id`,
852+ NULL,
853+ CAST(strftime('%s', 'now') AS INTEGER) * 1000,
854+ CAST(strftime('%s', 'now') AS INTEGER) * 1000
855+FROM `mcp_source` s
856+WHERE s.`auth_kind` = 'oauth2'
857+ AND s.`auth_client_id_secret_id` IS NOT NULL;--> statement-breakpoint
858+
859+INSERT OR REPLACE INTO `credential_binding` (
860+ `id`, `scope_id`, `plugin_id`, `source_id`, `source_scope_id`, `slot_key`,
861+ `kind`, `text_value`, `secret_id`, `connection_id`, `created_at`, `updated_at`
862+)
863+SELECT
864+ json_array('mcp', s.`scope_id`, s.`id`, 'auth:oauth2:client-secret'),
865+ s.`scope_id`,
866+ 'mcp',
867+ s.`id`,
868+ s.`scope_id`,
869+ 'auth:oauth2:client-secret',
870+ 'secret',
871+ NULL,
872+ s.`auth_client_secret_secret_id`,
873+ NULL,
874+ CAST(strftime('%s', 'now') AS INTEGER) * 1000,
875+ CAST(strftime('%s', 'now') AS INTEGER) * 1000
876+FROM `mcp_source` s
877+WHERE s.`auth_kind` = 'oauth2'
878+ AND s.`auth_client_secret_secret_id` IS NOT NULL;--> statement-breakpoint
879+
880+DROP INDEX `mcp_source_auth_secret_id_idx`;--> statement-breakpoint
881+DROP INDEX `mcp_source_auth_connection_id_idx`;--> statement-breakpoint
882+DROP INDEX `mcp_source_auth_client_id_secret_id_idx`;--> statement-breakpoint
883+DROP INDEX `mcp_source_auth_client_secret_secret_id_idx`;--> statement-breakpoint
884+ALTER TABLE `mcp_source` DROP COLUMN `auth_secret_id`;--> statement-breakpoint
885+ALTER TABLE `mcp_source` DROP COLUMN `auth_secret_prefix`;--> statement-breakpoint
886+ALTER TABLE `mcp_source` DROP COLUMN `auth_connection_id`;--> statement-breakpoint
887+ALTER TABLE `mcp_source` DROP COLUMN `auth_client_id_secret_id`;--> statement-breakpoint
888+ALTER TABLE `mcp_source` DROP COLUMN `auth_client_secret_secret_id`;--> statement-breakpoint
889+
890+ALTER TABLE `mcp_source_header` ADD `slot_key` text;--> statement-breakpoint
891+ALTER TABLE `mcp_source_header` ADD `prefix` text;--> statement-breakpoint
892+
893+CREATE TEMP TABLE `__mcp_header_slot_preflight` (
894+ `scope_id` text NOT NULL,
895+ `source_id` text NOT NULL,
896+ `slot_key` text NOT NULL,
897+ PRIMARY KEY (`scope_id`, `source_id`, `slot_key`)
898+);--> statement-breakpoint
899+
900+INSERT INTO `__mcp_header_slot_preflight` (`scope_id`, `source_id`, `slot_key`)
901+SELECT
902+ h.`scope_id`,
903+ h.`source_id`,
904+ 'header:' || COALESCE((SELECT `slug` FROM `__slug_norm` WHERE `raw` = h.`name`), 'default') AS `slot_key`
905+FROM `mcp_source_header` h
906+WHERE h.`kind` = 'secret'
907+ AND h.`secret_id` IS NOT NULL;--> statement-breakpoint
908+
909+DROP TABLE `__mcp_header_slot_preflight`;--> statement-breakpoint
910+
911+WITH header_rows AS (
912+ SELECT
913+ h.`scope_id`,
914+ h.`source_id`,
915+ h.`name`,
916+ 'header:' || COALESCE((SELECT `slug` FROM `__slug_norm` WHERE `raw` = h.`name`), 'default') AS `slot_key`,
917+ h.`secret_id`,
918+ h.`secret_prefix`
919+ FROM `mcp_source_header` h
920+ WHERE h.`kind` = 'secret'
921+ AND h.`secret_id` IS NOT NULL
922+)
923+INSERT OR REPLACE INTO `credential_binding` (
924+ `id`, `scope_id`, `plugin_id`, `source_id`, `source_scope_id`, `slot_key`,
925+ `kind`, `text_value`, `secret_id`, `connection_id`, `created_at`, `updated_at`
926+)
927+SELECT
928+ json_array('mcp', r.`scope_id`, r.`source_id`, r.`slot_key`),
929+ r.`scope_id`,
930+ 'mcp',
931+ r.`source_id`,
932+ r.`scope_id`,
933+ r.`slot_key`,
934+ 'secret',
935+ NULL,
936+ r.`secret_id`,
937+ NULL,
938+ CAST(strftime('%s', 'now') AS INTEGER) * 1000,
939+ CAST(strftime('%s', 'now') AS INTEGER) * 1000
940+FROM header_rows r;--> statement-breakpoint
941+
942+UPDATE `mcp_source_header`
943+SET
944+ `kind` = 'binding',
945+ `slot_key` = 'header:' || COALESCE((SELECT `slug` FROM `__slug_norm` WHERE `raw` = `name`), 'default'),
946+ `prefix` = `secret_prefix`,
947+ `text_value` = NULL
948+WHERE `kind` = 'secret'
949+ AND `secret_id` IS NOT NULL;--> statement-breakpoint
950+
951+DROP INDEX `mcp_source_header_secret_id_idx`;--> statement-breakpoint
952+ALTER TABLE `mcp_source_header` DROP COLUMN `secret_id`;--> statement-breakpoint
953+ALTER TABLE `mcp_source_header` DROP COLUMN `secret_prefix`;--> statement-breakpoint
954+
955+ALTER TABLE `mcp_source_query_param` ADD `slot_key` text;--> statement-breakpoint
956+ALTER TABLE `mcp_source_query_param` ADD `prefix` text;--> statement-breakpoint
957+
958+CREATE TEMP TABLE `__mcp_query_param_slot_preflight` (
959+ `scope_id` text NOT NULL,
960+ `source_id` text NOT NULL,
961+ `slot_key` text NOT NULL,
962+ PRIMARY KEY (`scope_id`, `source_id`, `slot_key`)
963+);--> statement-breakpoint
964+
965+INSERT INTO `__mcp_query_param_slot_preflight` (`scope_id`, `source_id`, `slot_key`)
966+SELECT
967+ q.`scope_id`,
968+ q.`source_id`,
969+ 'query_param:' || COALESCE((SELECT `slug` FROM `__slug_norm` WHERE `raw` = q.`name`), 'default') AS `slot_key`
970+FROM `mcp_source_query_param` q
971+WHERE q.`kind` = 'secret'
972+ AND q.`secret_id` IS NOT NULL;--> statement-breakpoint
973+
974+DROP TABLE `__mcp_query_param_slot_preflight`;--> statement-breakpoint
975+
976+WITH query_param_rows AS (
977+ SELECT
978+ q.`scope_id`,
979+ q.`source_id`,
980+ q.`name`,
981+ 'query_param:' || COALESCE((SELECT `slug` FROM `__slug_norm` WHERE `raw` = q.`name`), 'default') AS `slot_key`,
982+ q.`secret_id`,
983+ q.`secret_prefix`
984+ FROM `mcp_source_query_param` q
985+ WHERE q.`kind` = 'secret'
986+ AND q.`secret_id` IS NOT NULL
987+)
988+INSERT OR REPLACE INTO `credential_binding` (
989+ `id`, `scope_id`, `plugin_id`, `source_id`, `source_scope_id`, `slot_key`,
990+ `kind`, `text_value`, `secret_id`, `connection_id`, `created_at`, `updated_at`
991+)
992+SELECT
993+ json_array('mcp', r.`scope_id`, r.`source_id`, r.`slot_key`),
994+ r.`scope_id`,
995+ 'mcp',
996+ r.`source_id`,
997+ r.`scope_id`,
998+ r.`slot_key`,
999+ 'secret',
1000+ NULL,
1001+ r.`secret_id`,
1002+ NULL,
1003+ CAST(strftime('%s', 'now') AS INTEGER) * 1000,
1004+ CAST(strftime('%s', 'now') AS INTEGER) * 1000
1005+FROM query_param_rows r;--> statement-breakpoint
1006+
1007+UPDATE `mcp_source_query_param`
1008+SET
1009+ `kind` = 'binding',
1010+ `slot_key` = 'query_param:' || COALESCE((SELECT `slug` FROM `__slug_norm` WHERE `raw` = `name`), 'default'),
1011+ `prefix` = `secret_prefix`,
1012+ `text_value` = NULL
1013+WHERE `kind` = 'secret'
1014+ AND `secret_id` IS NOT NULL;--> statement-breakpoint
1015+
1016+DROP INDEX `mcp_source_query_param_secret_id_idx`;--> statement-breakpoint
1017+ALTER TABLE `mcp_source_query_param` DROP COLUMN `secret_id`;--> statement-breakpoint
1018+ALTER TABLE `mcp_source_query_param` DROP COLUMN `secret_prefix`;--> statement-breakpoint
1019+
1020+-- 0013_normalize_oauth_connections.sql
1021+-- Normalize pre-unified OAuth connection rows to the canonical core
1022+-- provider/provider_state shape. Runtime refresh only reads provider='oauth2'
1023+-- and provider_state.kind after this one-shot data migration.
1024+
1025+UPDATE `connection`
1026+SET
1027+ `provider` = 'oauth2',
1028+ `provider_state` = json_object(
1029+ 'kind', CASE json_extract(`provider_state`, '$.flow')
1030+ WHEN 'authorizationCode' THEN 'authorization-code'
1031+ ELSE 'client-credentials'
1032+ END,
1033+ 'tokenEndpoint', json_extract(`provider_state`, '$.tokenUrl'),
1034+ 'issuerUrl', NULL,
1035+ 'clientIdSecretId', json_extract(`provider_state`, '$.clientIdSecretId'),
1036+ 'clientSecretSecretId', CASE json_extract(`provider_state`, '$.flow')
1037+ WHEN 'clientCredentials' THEN coalesce(json_extract(`provider_state`, '$.clientSecretSecretId'), '')
1038+ ELSE json_extract(`provider_state`, '$.clientSecretSecretId')
1039+ END,
1040+ 'clientAuth', 'body',
1041+ 'scopes', coalesce(json_extract(`provider_state`, '$.scopes'), json('[]')),
1042+ 'scope', `scope`
1043+ ),
1044+ `updated_at` = CAST(strftime('%s', 'now') AS INTEGER) * 1000
1045+WHERE `provider` = 'openapi:oauth2'
1046+ AND json_extract(`provider_state`, '$.flow') IN ('authorizationCode', 'clientCredentials');--> statement-breakpoint
1047+
1048+UPDATE `connection`
1049+SET
1050+ `provider` = 'oauth2',
1051+ `provider_state` = json_object(
1052+ 'kind', 'dynamic-dcr',
1053+ 'tokenEndpoint', coalesce(
1054+ json_extract(`provider_state`, '$.tokenEndpoint'),
1055+ json_extract(`provider_state`, '$.authorizationServerMetadata.token_endpoint'),
1056+ ''
1057+ ),
1058+ 'issuerUrl', json_extract(`provider_state`, '$.authorizationServerMetadata.issuer'),
1059+ 'authorizationServerUrl', json_extract(`provider_state`, '$.authorizationServerUrl'),
1060+ 'authorizationServerMetadataUrl', json_extract(`provider_state`, '$.authorizationServerMetadataUrl'),
1061+ 'idTokenSigningAlgValuesSupported', coalesce(
1062+ json_extract(`provider_state`, '$.authorizationServerMetadata.id_token_signing_alg_values_supported'),
1063+ json('[]')
1064+ ),
1065+ 'clientId', coalesce(json_extract(`provider_state`, '$.clientInformation.client_id'), ''),
1066+ 'clientSecretSecretId', NULL,
1067+ 'clientAuth', CASE json_extract(`provider_state`, '$.clientInformation.token_endpoint_auth_method')
1068+ WHEN 'client_secret_basic' THEN 'basic'
1069+ ELSE 'body'
1070+ END,
1071+ 'scopes', json('[]'),
1072+ 'scope', `scope`,
1073+ 'resource', json_extract(`provider_state`, '$.endpoint')
1074+ ),
1075+ `updated_at` = CAST(strftime('%s', 'now') AS INTEGER) * 1000
1076+WHERE `provider` = 'mcp:oauth2';--> statement-breakpoint
1077+
1078+UPDATE `connection`
1079+SET
1080+ `provider` = 'oauth2',
1081+ `provider_state` = json_object(
1082+ 'kind', 'authorization-code',
1083+ 'tokenEndpoint', 'https://oauth2.googleapis.com/token',
1084+ 'issuerUrl', 'https://accounts.google.com',
1085+ 'clientIdSecretId', json_extract(`provider_state`, '$.clientIdSecretId'),
1086+ 'clientSecretSecretId', json_extract(`provider_state`, '$.clientSecretSecretId'),
1087+ 'clientAuth', 'body',
1088+ 'scopes', coalesce(json_extract(`provider_state`, '$.scopes'), json('[]')),
1089+ 'scope', `scope`
1090+ ),
1091+ `updated_at` = CAST(strftime('%s', 'now') AS INTEGER) * 1000
1092+WHERE `provider` IN ('google-discovery:google', 'google-discovery:oauth2');--> statement-breakpoint
1093+
1094+-- 0014_openapi_header_rows.sql
1095+-- Move OpenAPI request headers out of openapi_source.headers JSON and into
1096+-- the same child-row slot model used by query params and spec-fetch
1097+-- credentials. Runtime code reads only openapi_source_header after this.
1098+
1099+CREATE TABLE `openapi_source_header` (
1100+ `id` text NOT NULL,
1101+ `scope_id` text NOT NULL,
1102+ `source_id` text NOT NULL,
1103+ `name` text NOT NULL,
1104+ `kind` text NOT NULL,
1105+ `text_value` text,
1106+ `slot_key` text,
1107+ `prefix` text,
1108+ PRIMARY KEY(`scope_id`, `id`)
1109+);--> statement-breakpoint
1110+CREATE INDEX `openapi_source_header_scope_id_idx` ON `openapi_source_header` (`scope_id`);--> statement-breakpoint
1111+CREATE INDEX `openapi_source_header_source_id_idx` ON `openapi_source_header` (`source_id`);--> statement-breakpoint
1112+
1113+CREATE TEMP TABLE `__openapi_header_row_slot_preflight` (
1114+ `scope_id` text NOT NULL,
1115+ `source_id` text NOT NULL,
1116+ `slot_key` text NOT NULL,
1117+ PRIMARY KEY (`scope_id`, `source_id`, `slot_key`)
1118+);--> statement-breakpoint
1119+
1120+INSERT INTO `__openapi_header_row_slot_preflight` (`scope_id`, `source_id`, `slot_key`)
1121+SELECT
1122+ s.`scope_id`,
1123+ s.`id`,
1124+ 'header:' || COALESCE((SELECT `slug` FROM `__slug_norm` WHERE `raw` = h.`key`), 'default') AS `slot_key`
1125+FROM `openapi_source` s, json_each(s.`headers`) h
1126+WHERE s.`headers` IS NOT NULL
1127+ AND h.`type` = 'object'
1128+ AND json_extract(h.`value`, '$.secretId') IS NOT NULL
1129+ AND COALESCE(json_extract(h.`value`, '$.kind'), 'secret') = 'secret';--> statement-breakpoint
1130+
1131+DROP TABLE `__openapi_header_row_slot_preflight`;--> statement-breakpoint
1132+
1133+WITH header_rows AS (
1134+ SELECT
1135+ s.`scope_id`,
1136+ s.`id` AS `source_id`,
1137+ h.`key` AS `name`,
1138+ 'header:' || COALESCE((SELECT `slug` FROM `__slug_norm` WHERE `raw` = h.`key`), 'default') AS `slot_key`,
1139+ json_extract(h.`value`, '$.secretId') AS `secret_id`
1140+ FROM `openapi_source` s, json_each(s.`headers`) h
1141+ WHERE s.`headers` IS NOT NULL
1142+ AND h.`type` = 'object'
1143+ AND json_extract(h.`value`, '$.secretId') IS NOT NULL
1144+ AND COALESCE(json_extract(h.`value`, '$.kind'), 'secret') = 'secret'
1145+)
1146+INSERT OR REPLACE INTO `credential_binding` (
1147+ `id`, `scope_id`, `plugin_id`, `source_id`, `source_scope_id`, `slot_key`,
1148+ `kind`, `text_value`, `secret_id`, `connection_id`, `created_at`, `updated_at`
1149+)
1150+SELECT
1151+ json_array('openapi', r.`scope_id`, r.`source_id`, r.`slot_key`),
1152+ r.`scope_id`,
1153+ 'openapi',
1154+ r.`source_id`,
1155+ r.`scope_id`,
1156+ r.`slot_key`,
1157+ 'secret',
1158+ NULL,
1159+ r.`secret_id`,
1160+ NULL,
1161+ CAST(strftime('%s', 'now') AS INTEGER) * 1000,
1162+ CAST(strftime('%s', 'now') AS INTEGER) * 1000
1163+FROM header_rows r
1164+WHERE NOT EXISTS (
1165+ SELECT 1 FROM `credential_binding` b
1166+ WHERE b.`scope_id` = r.`scope_id`
1167+ AND b.`plugin_id` = 'openapi'
1168+ AND b.`source_id` = r.`source_id`
1169+ AND b.`source_scope_id` = r.`scope_id`
1170+ AND b.`slot_key` = r.`slot_key`
1171+);--> statement-breakpoint
1172+
1173+INSERT OR REPLACE INTO `openapi_source_header` (
1174+ `id`, `scope_id`, `source_id`, `name`, `kind`, `text_value`, `slot_key`, `prefix`
1175+)
1176+SELECT
1177+ json_array(s.`id`, h.`key`),
1178+ s.`scope_id`,
1179+ s.`id`,
1180+ h.`key`,
1181+ CASE
1182+ WHEN h.`type` = 'text' THEN 'text'
1183+ WHEN h.`type` = 'object' AND json_extract(h.`value`, '$.kind') = 'text' THEN 'text'
1184+ ELSE 'binding'
1185+ END,
1186+ CASE
1187+ WHEN h.`type` = 'text' THEN h.`value`
1188+ WHEN h.`type` = 'object' AND json_extract(h.`value`, '$.kind') = 'text'
1189+ THEN json_extract(h.`value`, '$.text')
1190+ ELSE NULL
1191+ END,
1192+ CASE
1193+ WHEN h.`type` = 'object' AND json_extract(h.`value`, '$.kind') = 'binding'
1194+ THEN json_extract(h.`value`, '$.slot')
1195+ WHEN h.`type` = 'object' AND json_extract(h.`value`, '$.secretId') IS NOT NULL
1196+ THEN 'header:' || COALESCE((SELECT `slug` FROM `__slug_norm` WHERE `raw` = h.`key`), 'default')
1197+ ELSE NULL
1198+ END,
1199+ CASE
1200+ WHEN h.`type` = 'object'
1201+ THEN COALESCE(json_extract(h.`value`, '$.prefix'), json_extract(h.`value`, '$.secretPrefix'))
1202+ ELSE NULL
1203+ END
1204+FROM `openapi_source` s, json_each(s.`headers`) h
1205+WHERE s.`headers` IS NOT NULL
1206+ AND (
1207+ h.`type` = 'text'
1208+ OR (
1209+ h.`type` = 'object'
1210+ AND (
1211+ json_extract(h.`value`, '$.kind') IN ('binding', 'text')
1212+ OR json_extract(h.`value`, '$.secretId') IS NOT NULL
1213+ )
1214+ )
1215+);--> statement-breakpoint
1216+
1217+ALTER TABLE `openapi_source` DROP COLUMN `headers`;
1218+
1219+--> statement-breakpoint
1220+DROP TABLE `__slug_norm`;
apps/local/drizzle-legacy-v1/0009_repair_openapi_oauth_cutover_residue.sql+253−0
@@ -0,0 +1,253 @@
1+-- Repair OpenAPI OAuth residue from the scoped-credential cutover.
2+
3+UPDATE `connection`
4+SET `provider` = 'oauth2',
5+ `updated_at` = CAST(strftime('%s', 'now') AS INTEGER) * 1000
6+WHERE `provider` = 'openapi:oauth2'
7+ AND json_extract(`provider_state`, '$.kind') IS NOT NULL;--> statement-breakpoint
8+
9+WITH oauth_connections AS (
10+ SELECT
11+ b.`scope_id`,
12+ b.`id` AS `binding_id`,
13+ b.`plugin_id`,
14+ b.`source_id`,
15+ b.`source_scope_id`,
16+ json_extract(s.`oauth2`, '$.clientIdSlot') AS `slot_key`,
17+ json_extract(c.`provider_state`, '$.clientIdSecretId') AS `secret_id`,
18+ b.`created_at`
19+ FROM `credential_binding` b
20+ JOIN `openapi_source` s
21+ ON s.`id` = b.`source_id`
22+ AND s.`scope_id` = b.`source_scope_id`
23+ JOIN `connection` c
24+ ON c.`id` = b.`connection_id`
25+ AND c.`scope_id` = b.`scope_id`
26+ WHERE b.`plugin_id` = 'openapi'
27+ AND b.`kind` = 'connection'
28+ AND s.`oauth2` IS NOT NULL
29+ AND c.`provider` = 'oauth2'
30+ AND json_extract(c.`provider_state`, '$.clientIdSecretId') IS NOT NULL
31+ AND coalesce(json_extract(c.`provider_state`, '$.clientIdSecretId'), '') <> ''
32+)
33+UPDATE `credential_binding`
34+SET
35+ `secret_id` = (
36+ SELECT r.`secret_id`
37+ FROM oauth_connections r
38+ WHERE r.`scope_id` = `credential_binding`.`scope_id`
39+ AND r.`plugin_id` = `credential_binding`.`plugin_id`
40+ AND r.`source_id` = `credential_binding`.`source_id`
41+ AND r.`source_scope_id` = `credential_binding`.`source_scope_id`
42+ AND r.`slot_key` = `credential_binding`.`slot_key`
43+ LIMIT 1
44+ ),
45+ `text_value` = NULL,
46+ `connection_id` = NULL,
47+ `updated_at` = CAST(strftime('%s', 'now') AS INTEGER) * 1000
48+WHERE `kind` = 'secret'
49+ AND EXISTS (
50+ SELECT 1
51+ FROM oauth_connections r
52+ WHERE r.`scope_id` = `credential_binding`.`scope_id`
53+ AND r.`plugin_id` = `credential_binding`.`plugin_id`
54+ AND r.`source_id` = `credential_binding`.`source_id`
55+ AND r.`source_scope_id` = `credential_binding`.`source_scope_id`
56+ AND r.`slot_key` = `credential_binding`.`slot_key`
57+ );--> statement-breakpoint
58+
59+WITH oauth_connections AS (
60+ SELECT
61+ b.`scope_id`,
62+ b.`id` AS `binding_id`,
63+ b.`plugin_id`,
64+ b.`source_id`,
65+ b.`source_scope_id`,
66+ json_extract(s.`oauth2`, '$.clientIdSlot') AS `slot_key`,
67+ json_extract(c.`provider_state`, '$.clientIdSecretId') AS `secret_id`,
68+ b.`created_at`
69+ FROM `credential_binding` b
70+ JOIN `openapi_source` s
71+ ON s.`id` = b.`source_id`
72+ AND s.`scope_id` = b.`source_scope_id`
73+ JOIN `connection` c
74+ ON c.`id` = b.`connection_id`
75+ AND c.`scope_id` = b.`scope_id`
76+ WHERE b.`plugin_id` = 'openapi'
77+ AND b.`kind` = 'connection'
78+ AND s.`oauth2` IS NOT NULL
79+ AND c.`provider` = 'oauth2'
80+ AND json_extract(c.`provider_state`, '$.clientIdSecretId') IS NOT NULL
81+ AND coalesce(json_extract(c.`provider_state`, '$.clientIdSecretId'), '') <> ''
82+)
83+INSERT INTO `credential_binding` (
84+ `id`,
85+ `scope_id`,
86+ `plugin_id`,
87+ `source_id`,
88+ `source_scope_id`,
89+ `slot_key`,
90+ `kind`,
91+ `text_value`,
92+ `secret_id`,
93+ `connection_id`,
94+ `created_at`,
95+ `updated_at`
96+)
97+SELECT
98+ json_array('oconn-openapi-oauth-client', r.`binding_id`, r.`scope_id`, r.`slot_key`),
99+ r.`scope_id`,
100+ r.`plugin_id`,
101+ r.`source_id`,
102+ r.`source_scope_id`,
103+ r.`slot_key`,
104+ 'secret',
105+ NULL,
106+ r.`secret_id`,
107+ NULL,
108+ r.`created_at`,
109+ CAST(strftime('%s', 'now') AS INTEGER) * 1000
110+FROM oauth_connections r
111+WHERE r.`slot_key` IS NOT NULL
112+ AND NOT EXISTS (
113+ SELECT 1
114+ FROM `credential_binding` existing
115+ WHERE existing.`scope_id` = r.`scope_id`
116+ AND existing.`plugin_id` = r.`plugin_id`
117+ AND existing.`source_id` = r.`source_id`
118+ AND existing.`source_scope_id` = r.`source_scope_id`
119+ AND existing.`slot_key` = r.`slot_key`
120+ )
121+ON CONFLICT(`scope_id`, `id`) DO UPDATE SET
122+ `plugin_id` = excluded.`plugin_id`,
123+ `source_id` = excluded.`source_id`,
124+ `source_scope_id` = excluded.`source_scope_id`,
125+ `slot_key` = excluded.`slot_key`,
126+ `kind` = excluded.`kind`,
127+ `text_value` = excluded.`text_value`,
128+ `secret_id` = excluded.`secret_id`,
129+ `connection_id` = excluded.`connection_id`,
130+ `updated_at` = excluded.`updated_at`;--> statement-breakpoint
131+
132+WITH oauth_connections AS (
133+ SELECT
134+ b.`scope_id`,
135+ b.`id` AS `binding_id`,
136+ b.`plugin_id`,
137+ b.`source_id`,
138+ b.`source_scope_id`,
139+ json_extract(s.`oauth2`, '$.clientSecretSlot') AS `slot_key`,
140+ json_extract(c.`provider_state`, '$.clientSecretSecretId') AS `secret_id`,
141+ b.`created_at`
142+ FROM `credential_binding` b
143+ JOIN `openapi_source` s
144+ ON s.`id` = b.`source_id`
145+ AND s.`scope_id` = b.`source_scope_id`
146+ JOIN `connection` c
147+ ON c.`id` = b.`connection_id`
148+ AND c.`scope_id` = b.`scope_id`
149+ WHERE b.`plugin_id` = 'openapi'
150+ AND b.`kind` = 'connection'
151+ AND s.`oauth2` IS NOT NULL
152+ AND c.`provider` = 'oauth2'
153+ AND json_extract(c.`provider_state`, '$.clientSecretSecretId') IS NOT NULL
154+ AND coalesce(json_extract(c.`provider_state`, '$.clientSecretSecretId'), '') <> ''
155+)
156+UPDATE `credential_binding`
157+SET
158+ `secret_id` = (
159+ SELECT r.`secret_id`
160+ FROM oauth_connections r
161+ WHERE r.`scope_id` = `credential_binding`.`scope_id`
162+ AND r.`plugin_id` = `credential_binding`.`plugin_id`
163+ AND r.`source_id` = `credential_binding`.`source_id`
164+ AND r.`source_scope_id` = `credential_binding`.`source_scope_id`
165+ AND r.`slot_key` = `credential_binding`.`slot_key`
166+ LIMIT 1
167+ ),
168+ `text_value` = NULL,
169+ `connection_id` = NULL,
170+ `updated_at` = CAST(strftime('%s', 'now') AS INTEGER) * 1000
171+WHERE `kind` = 'secret'
172+ AND EXISTS (
173+ SELECT 1
174+ FROM oauth_connections r
175+ WHERE r.`scope_id` = `credential_binding`.`scope_id`
176+ AND r.`plugin_id` = `credential_binding`.`plugin_id`
177+ AND r.`source_id` = `credential_binding`.`source_id`
178+ AND r.`source_scope_id` = `credential_binding`.`source_scope_id`
179+ AND r.`slot_key` = `credential_binding`.`slot_key`
180+ );--> statement-breakpoint
181+
182+WITH oauth_connections AS (
183+ SELECT
184+ b.`scope_id`,
185+ b.`id` AS `binding_id`,
186+ b.`plugin_id`,
187+ b.`source_id`,
188+ b.`source_scope_id`,
189+ json_extract(s.`oauth2`, '$.clientSecretSlot') AS `slot_key`,
190+ json_extract(c.`provider_state`, '$.clientSecretSecretId') AS `secret_id`,
191+ b.`created_at`
192+ FROM `credential_binding` b
193+ JOIN `openapi_source` s
194+ ON s.`id` = b.`source_id`
195+ AND s.`scope_id` = b.`source_scope_id`
196+ JOIN `connection` c
197+ ON c.`id` = b.`connection_id`
198+ AND c.`scope_id` = b.`scope_id`
199+ WHERE b.`plugin_id` = 'openapi'
200+ AND b.`kind` = 'connection'
201+ AND s.`oauth2` IS NOT NULL
202+ AND c.`provider` = 'oauth2'
203+ AND json_extract(c.`provider_state`, '$.clientSecretSecretId') IS NOT NULL
204+ AND coalesce(json_extract(c.`provider_state`, '$.clientSecretSecretId'), '') <> ''
205+)
206+INSERT INTO `credential_binding` (
207+ `id`,
208+ `scope_id`,
209+ `plugin_id`,
210+ `source_id`,
211+ `source_scope_id`,
212+ `slot_key`,
213+ `kind`,
214+ `text_value`,
215+ `secret_id`,
216+ `connection_id`,
217+ `created_at`,
218+ `updated_at`
219+)
220+SELECT
221+ json_array('oconn-openapi-oauth-secret', r.`binding_id`, r.`scope_id`, r.`slot_key`),
222+ r.`scope_id`,
223+ r.`plugin_id`,
224+ r.`source_id`,
225+ r.`source_scope_id`,
226+ r.`slot_key`,
227+ 'secret',
228+ NULL,
229+ r.`secret_id`,
230+ NULL,
231+ r.`created_at`,
232+ CAST(strftime('%s', 'now') AS INTEGER) * 1000
233+FROM oauth_connections r
234+WHERE r.`slot_key` IS NOT NULL
235+ AND NOT EXISTS (
236+ SELECT 1
237+ FROM `credential_binding` existing
238+ WHERE existing.`scope_id` = r.`scope_id`
239+ AND existing.`plugin_id` = r.`plugin_id`
240+ AND existing.`source_id` = r.`source_id`
241+ AND existing.`source_scope_id` = r.`source_scope_id`
242+ AND existing.`slot_key` = r.`slot_key`
243+ )
244+ON CONFLICT(`scope_id`, `id`) DO UPDATE SET
245+ `plugin_id` = excluded.`plugin_id`,
246+ `source_id` = excluded.`source_id`,
247+ `source_scope_id` = excluded.`source_scope_id`,
248+ `slot_key` = excluded.`slot_key`,
249+ `kind` = excluded.`kind`,
250+ `text_value` = excluded.`text_value`,
251+ `secret_id` = excluded.`secret_id`,
252+ `connection_id` = excluded.`connection_id`,
253+ `updated_at` = excluded.`updated_at`;
apps/local/drizzle-legacy-v1/0010_add_credential_binding_secret_scope.sql+2−0
@@ -0,0 +1,2 @@
1+ALTER TABLE `credential_binding` ADD COLUMN `secret_scope_id` text;--> statement-breakpoint
2+CREATE INDEX `credential_binding_secret_scope_id_idx` ON `credential_binding` (`secret_scope_id`);
apps/local/drizzle-legacy-v1/0011_plugin_storage_sources.sql+215−0
@@ -0,0 +1,215 @@
1+CREATE TABLE IF NOT EXISTS `openapi_source` (`id` text NOT NULL, `scope_id` text NOT NULL, `name` text NOT NULL, `spec` text NOT NULL, `source_url` text, `base_url` text, `oauth2` text);
2+--> statement-breakpoint
3+CREATE TABLE IF NOT EXISTS `openapi_operation` (`id` text NOT NULL, `scope_id` text NOT NULL, `source_id` text NOT NULL, `binding` text NOT NULL);
4+--> statement-breakpoint
5+CREATE TABLE IF NOT EXISTS `openapi_source_header` (`id` text, `scope_id` text, `source_id` text, `name` text, `kind` text, `text_value` text, `slot_key` text, `prefix` text);
6+--> statement-breakpoint
7+CREATE TABLE IF NOT EXISTS `openapi_source_query_param` (`id` text, `scope_id` text, `source_id` text, `name` text, `kind` text, `text_value` text, `slot_key` text, `prefix` text);
8+--> statement-breakpoint
9+CREATE TABLE IF NOT EXISTS `openapi_source_spec_fetch_header` (`id` text, `scope_id` text, `source_id` text, `name` text, `kind` text, `text_value` text, `slot_key` text, `prefix` text);
10+--> statement-breakpoint
11+CREATE TABLE IF NOT EXISTS `openapi_source_spec_fetch_query_param` (`id` text, `scope_id` text, `source_id` text, `name` text, `kind` text, `text_value` text, `slot_key` text, `prefix` text);
12+--> statement-breakpoint
13+CREATE TABLE IF NOT EXISTS `graphql_source` (`id` text NOT NULL, `scope_id` text NOT NULL, `name` text NOT NULL, `endpoint` text NOT NULL, `auth_kind` text NOT NULL DEFAULT 'none', `auth_connection_slot` text);
14+--> statement-breakpoint
15+CREATE TABLE IF NOT EXISTS `graphql_source_header` (`id` text, `scope_id` text, `source_id` text, `name` text, `kind` text, `text_value` text, `slot_key` text, `prefix` text);
16+--> statement-breakpoint
17+CREATE TABLE IF NOT EXISTS `graphql_source_query_param` (`id` text, `scope_id` text, `source_id` text, `name` text, `kind` text, `text_value` text, `slot_key` text, `prefix` text);
18+--> statement-breakpoint
19+CREATE TABLE IF NOT EXISTS `graphql_operation` (`id` text NOT NULL, `scope_id` text NOT NULL, `source_id` text NOT NULL, `binding` text NOT NULL);
20+--> statement-breakpoint
21+CREATE TABLE IF NOT EXISTS `mcp_source` (`id` text NOT NULL, `scope_id` text NOT NULL, `name` text NOT NULL, `config` text NOT NULL, `auth_kind` text NOT NULL DEFAULT 'none', `auth_header_name` text, `auth_header_slot` text, `auth_header_prefix` text, `auth_connection_slot` text, `auth_client_id_slot` text, `auth_client_secret_slot` text, `created_at` integer);
22+--> statement-breakpoint
23+CREATE TABLE IF NOT EXISTS `mcp_source_header` (`id` text, `scope_id` text, `source_id` text, `name` text, `kind` text, `text_value` text, `slot_key` text, `prefix` text);
24+--> statement-breakpoint
25+CREATE TABLE IF NOT EXISTS `mcp_source_query_param` (`id` text, `scope_id` text, `source_id` text, `name` text, `kind` text, `text_value` text, `slot_key` text, `prefix` text);
26+--> statement-breakpoint
27+CREATE TABLE IF NOT EXISTS `mcp_binding` (`id` text NOT NULL, `scope_id` text NOT NULL, `source_id` text NOT NULL, `binding` text NOT NULL, `created_at` integer);
28+--> statement-breakpoint
29+CREATE TABLE IF NOT EXISTS `plugin_storage` (
30+ `id` text NOT NULL,
31+ `scope_id` text NOT NULL,
32+ `plugin_id` text NOT NULL,
33+ `collection` text NOT NULL,
34+ `key` text NOT NULL,
35+ `data` text NOT NULL,
36+ `created_at` integer NOT NULL,
37+ `updated_at` integer NOT NULL,
38+ PRIMARY KEY(`scope_id`, `id`)
39+);
40+--> statement-breakpoint
41+CREATE INDEX IF NOT EXISTS `plugin_storage_scope_id_idx` ON `plugin_storage` (`scope_id`);
42+--> statement-breakpoint
43+CREATE INDEX IF NOT EXISTS `plugin_storage_plugin_id_collection_idx` ON `plugin_storage` (`plugin_id`, `collection`);
44+--> statement-breakpoint
45+CREATE INDEX IF NOT EXISTS `plugin_storage_key_idx` ON `plugin_storage` (`key`);
46+--> statement-breakpoint
47+INSERT OR REPLACE INTO `plugin_storage` (`id`, `scope_id`, `plugin_id`, `collection`, `key`, `data`, `created_at`, `updated_at`)
48+SELECT
49+ json_array('openapi', 'source', s.`id`),
50+ s.`scope_id`,
51+ 'openapi',
52+ 'source',
53+ s.`id`,
54+ json_object(
55+ 'namespace', s.`id`,
56+ 'scope', s.`scope_id`,
57+ 'name', s.`name`,
58+ 'config', json_patch(
59+ json_patch(
60+ json_patch(
61+ json_patch(
62+ json_patch(
63+ json_patch(
64+ json_object('spec', s.`spec`),
65+ CASE WHEN s.`source_url` IS NULL THEN json_object() ELSE json_object('sourceUrl', s.`source_url`) END
66+ ),
67+ CASE WHEN s.`base_url` IS NULL THEN json_object() ELSE json_object('baseUrl', s.`base_url`) END
68+ ),
69+ CASE WHEN h.`headers` IS NULL THEN json_object() ELSE json_object('headers', json(h.`headers`)) END
70+ ),
71+ CASE WHEN q.`queryParams` IS NULL THEN json_object() ELSE json_object('queryParams', json(q.`queryParams`)) END
72+ ),
73+ CASE WHEN sfh.`headers` IS NULL AND sfq.`queryParams` IS NULL THEN json_object() ELSE json_object('specFetchCredentials', json_patch(
74+ CASE WHEN sfh.`headers` IS NULL THEN json_object() ELSE json_object('headers', json(sfh.`headers`)) END,
75+ CASE WHEN sfq.`queryParams` IS NULL THEN json_object() ELSE json_object('queryParams', json(sfq.`queryParams`)) END
76+ )) END
77+ ),
78+ CASE WHEN s.`oauth2` IS NULL THEN json_object() ELSE json_object('oauth2', json(s.`oauth2`)) END
79+ )
80+ ),
81+ unixepoch('now') * 1000,
82+ unixepoch('now') * 1000
83+FROM `openapi_source` s
84+LEFT JOIN (
85+ SELECT `scope_id`, `source_id`, json_group_object(`name`, CASE WHEN `kind` = 'text' THEN json_quote(`text_value`) ELSE json_object('kind', 'binding', 'slot', `slot_key`, 'prefix', `prefix`) END) AS `headers`
86+ FROM `openapi_source_header`
87+ GROUP BY `scope_id`, `source_id`
88+) h ON h.`scope_id` = s.`scope_id` AND h.`source_id` = s.`id`
89+LEFT JOIN (
90+ SELECT `scope_id`, `source_id`, json_group_object(`name`, CASE WHEN `kind` = 'text' THEN json_quote(`text_value`) ELSE json_object('kind', 'binding', 'slot', `slot_key`, 'prefix', `prefix`) END) AS `queryParams`
91+ FROM `openapi_source_query_param`
92+ GROUP BY `scope_id`, `source_id`
93+) q ON q.`scope_id` = s.`scope_id` AND q.`source_id` = s.`id`
94+LEFT JOIN (
95+ SELECT `scope_id`, `source_id`, json_group_object(`name`, CASE WHEN `kind` = 'text' THEN json_quote(`text_value`) ELSE json_object('kind', 'binding', 'slot', `slot_key`, 'prefix', `prefix`) END) AS `headers`
96+ FROM `openapi_source_spec_fetch_header`
97+ GROUP BY `scope_id`, `source_id`
98+) sfh ON sfh.`scope_id` = s.`scope_id` AND sfh.`source_id` = s.`id`
99+LEFT JOIN (
100+ SELECT `scope_id`, `source_id`, json_group_object(`name`, CASE WHEN `kind` = 'text' THEN json_quote(`text_value`) ELSE json_object('kind', 'binding', 'slot', `slot_key`, 'prefix', `prefix`) END) AS `queryParams`
101+ FROM `openapi_source_spec_fetch_query_param`
102+ GROUP BY `scope_id`, `source_id`
103+) sfq ON sfq.`scope_id` = s.`scope_id` AND sfq.`source_id` = s.`id`;
104+--> statement-breakpoint
105+INSERT OR REPLACE INTO `plugin_storage` (`id`, `scope_id`, `plugin_id`, `collection`, `key`, `data`, `created_at`, `updated_at`)
106+SELECT json_array('openapi', 'operation', o.`id`), o.`scope_id`, 'openapi', 'operation', o.`id`, json_object('toolId', o.`id`, 'sourceId', o.`source_id`, 'binding', json(o.`binding`)), unixepoch('now') * 1000, unixepoch('now') * 1000
107+FROM `openapi_operation` o;
108+--> statement-breakpoint
109+INSERT OR REPLACE INTO `plugin_storage` (`id`, `scope_id`, `plugin_id`, `collection`, `key`, `data`, `created_at`, `updated_at`)
110+SELECT
111+ json_array('graphql', 'source', s.`id`),
112+ s.`scope_id`,
113+ 'graphql',
114+ 'source',
115+ s.`id`,
116+ json_object(
117+ 'namespace', s.`id`,
118+ 'scope', s.`scope_id`,
119+ 'name', s.`name`,
120+ 'endpoint', s.`endpoint`,
121+ 'headers', COALESCE(json(h.`headers`), json_object()),
122+ 'queryParams', COALESCE(json(q.`queryParams`), json_object()),
123+ 'auth', CASE WHEN s.`auth_kind` = 'oauth2' AND s.`auth_connection_slot` IS NOT NULL THEN json_object('kind', 'oauth2', 'connectionSlot', s.`auth_connection_slot`) ELSE json_object('kind', 'none') END
124+ ),
125+ unixepoch('now') * 1000,
126+ unixepoch('now') * 1000
127+FROM `graphql_source` s
128+LEFT JOIN (
129+ SELECT `scope_id`, `source_id`, json_group_object(`name`, CASE WHEN `kind` = 'text' THEN json_quote(`text_value`) ELSE json_object('kind', 'binding', 'slot', `slot_key`, 'prefix', `prefix`) END) AS `headers`
130+ FROM `graphql_source_header`
131+ GROUP BY `scope_id`, `source_id`
132+) h ON h.`scope_id` = s.`scope_id` AND h.`source_id` = s.`id`
133+LEFT JOIN (
134+ SELECT `scope_id`, `source_id`, json_group_object(`name`, CASE WHEN `kind` = 'text' THEN json_quote(`text_value`) ELSE json_object('kind', 'binding', 'slot', `slot_key`, 'prefix', `prefix`) END) AS `queryParams`
135+ FROM `graphql_source_query_param`
136+ GROUP BY `scope_id`, `source_id`
137+) q ON q.`scope_id` = s.`scope_id` AND q.`source_id` = s.`id`;
138+--> statement-breakpoint
139+INSERT OR REPLACE INTO `plugin_storage` (`id`, `scope_id`, `plugin_id`, `collection`, `key`, `data`, `created_at`, `updated_at`)
140+SELECT json_array('graphql', 'operation', o.`id`), o.`scope_id`, 'graphql', 'operation', o.`id`, json_object('toolId', o.`id`, 'sourceId', o.`source_id`, 'binding', json(o.`binding`)), unixepoch('now') * 1000, unixepoch('now') * 1000
141+FROM `graphql_operation` o;
142+--> statement-breakpoint
143+INSERT OR REPLACE INTO `plugin_storage` (`id`, `scope_id`, `plugin_id`, `collection`, `key`, `data`, `created_at`, `updated_at`)
144+SELECT
145+ json_array('mcp', 'source', s.`id`),
146+ s.`scope_id`,
147+ 'mcp',
148+ 'source',
149+ s.`id`,
150+ json_object(
151+ 'namespace', s.`id`,
152+ 'scope', s.`scope_id`,
153+ 'name', s.`name`,
154+ 'config', CASE WHEN json_extract(s.`config`, '$.transport') = 'remote' THEN json_patch(
155+ json_patch(
156+ json_patch(
157+ json(s.`config`),
158+ CASE WHEN h.`headers` IS NULL THEN json_object() ELSE json_object('headers', json(h.`headers`)) END
159+ ),
160+ CASE WHEN q.`queryParams` IS NULL THEN json_object() ELSE json_object('queryParams', json(q.`queryParams`)) END
161+ ),
162+ json_object('auth',
163+ CASE
164+ WHEN s.`auth_kind` = 'header' THEN json_object('kind', 'header', 'headerName', COALESCE(s.`auth_header_name`, ''), 'secretSlot', s.`auth_header_slot`, 'prefix', s.`auth_header_prefix`)
165+ WHEN s.`auth_kind` = 'oauth2' THEN json_object('kind', 'oauth2', 'connectionSlot', s.`auth_connection_slot`, 'clientIdSlot', s.`auth_client_id_slot`, 'clientSecretSlot', s.`auth_client_secret_slot`)
166+ ELSE json_object('kind', 'none')
167+ END
168+ )
169+ ) ELSE json(s.`config`) END
170+ ),
171+ unixepoch('now') * 1000,
172+ unixepoch('now') * 1000
173+FROM `mcp_source` s
174+LEFT JOIN (
175+ SELECT `scope_id`, `source_id`, json_group_object(`name`, CASE WHEN `kind` = 'text' THEN json_quote(`text_value`) ELSE json_object('kind', 'binding', 'slot', `slot_key`, 'prefix', `prefix`) END) AS `headers`
176+ FROM `mcp_source_header`
177+ GROUP BY `scope_id`, `source_id`
178+) h ON h.`scope_id` = s.`scope_id` AND h.`source_id` = s.`id`
179+LEFT JOIN (
180+ SELECT `scope_id`, `source_id`, json_group_object(`name`, CASE WHEN `kind` = 'text' THEN json_quote(`text_value`) ELSE json_object('kind', 'binding', 'slot', `slot_key`, 'prefix', `prefix`) END) AS `queryParams`
181+ FROM `mcp_source_query_param`
182+ GROUP BY `scope_id`, `source_id`
183+) q ON q.`scope_id` = s.`scope_id` AND q.`source_id` = s.`id`;
184+--> statement-breakpoint
185+INSERT OR REPLACE INTO `plugin_storage` (`id`, `scope_id`, `plugin_id`, `collection`, `key`, `data`, `created_at`, `updated_at`)
186+SELECT json_array('mcp', 'binding', b.`id`), b.`scope_id`, 'mcp', 'binding', b.`id`, json_object('namespace', b.`source_id`, 'toolId', b.`id`, 'binding', json(b.`binding`)), COALESCE(b.`created_at`, unixepoch('now') * 1000), unixepoch('now') * 1000
187+FROM `mcp_binding` b;
188+--> statement-breakpoint
189+DROP TABLE IF EXISTS `openapi_source`;
190+--> statement-breakpoint
191+DROP TABLE IF EXISTS `openapi_operation`;
192+--> statement-breakpoint
193+DROP TABLE IF EXISTS `openapi_source_header`;
194+--> statement-breakpoint
195+DROP TABLE IF EXISTS `openapi_source_query_param`;
196+--> statement-breakpoint
197+DROP TABLE IF EXISTS `openapi_source_spec_fetch_header`;
198+--> statement-breakpoint
199+DROP TABLE IF EXISTS `openapi_source_spec_fetch_query_param`;
200+--> statement-breakpoint
201+DROP TABLE IF EXISTS `graphql_source`;
202+--> statement-breakpoint
203+DROP TABLE IF EXISTS `graphql_source_header`;
204+--> statement-breakpoint
205+DROP TABLE IF EXISTS `graphql_source_query_param`;
206+--> statement-breakpoint
207+DROP TABLE IF EXISTS `graphql_operation`;
208+--> statement-breakpoint
209+DROP TABLE IF EXISTS `mcp_source`;
210+--> statement-breakpoint
211+DROP TABLE IF EXISTS `mcp_source_header`;
212+--> statement-breakpoint
213+DROP TABLE IF EXISTS `mcp_source_query_param`;
214+--> statement-breakpoint
215+DROP TABLE IF EXISTS `mcp_binding`;
apps/local/drizzle-legacy-v1/0012_connection_identity_override.sql+1−0
@@ -0,0 +1 @@
1+ALTER TABLE `connection` ADD `identity_override` text;
apps/local/drizzle-legacy-v1/meta/_journal.json+97−0
@@ -0,0 +1,97 @@
1+{
2+ "version": "7",
3+ "dialect": "sqlite",
4+ "entries": [
5+ {
6+ "idx": 0,
7+ "version": "6",
8+ "when": 1776367901699,
9+ "tag": "0000_overconfident_sharon_carter",
10+ "breakpoints": true
11+ },
12+ {
13+ "idx": 1,
14+ "version": "6",
15+ "when": 1776680432025,
16+ "tag": "0001_sour_catseye",
17+ "breakpoints": true
18+ },
19+ {
20+ "idx": 2,
21+ "version": "6",
22+ "when": 1776732062873,
23+ "tag": "0002_lively_sue_storm",
24+ "breakpoints": true
25+ },
26+ {
27+ "idx": 3,
28+ "version": "6",
29+ "when": 1776976132767,
30+ "tag": "0003_little_silk_fever",
31+ "breakpoints": true
32+ },
33+ {
34+ "idx": 4,
35+ "version": "6",
36+ "when": 1777800000000,
37+ "tag": "0004_add_tool_policy",
38+ "breakpoints": true
39+ },
40+ {
41+ "idx": 5,
42+ "version": "6",
43+ "when": 1777850000000,
44+ "tag": "0005_repair_mcp_oauth_session",
45+ "breakpoints": true
46+ },
47+ {
48+ "idx": 6,
49+ "version": "6",
50+ "when": 1777850000001,
51+ "tag": "0006_neat_terror",
52+ "breakpoints": true
53+ },
54+ {
55+ "idx": 7,
56+ "version": "6",
57+ "when": 1778100000000,
58+ "tag": "0007_normalize_plugin_secret_refs",
59+ "breakpoints": true
60+ },
61+ {
62+ "idx": 8,
63+ "version": "6",
64+ "when": 1778128200000,
65+ "tag": "0008_scoped_credentials_cutover",
66+ "breakpoints": true
67+ },
68+ {
69+ "idx": 9,
70+ "version": "6",
71+ "when": 1778192434062,
72+ "tag": "0009_repair_openapi_oauth_cutover_residue",
73+ "breakpoints": true
74+ },
75+ {
76+ "idx": 10,
77+ "version": "6",
78+ "when": 1778192434063,
79+ "tag": "0010_add_credential_binding_secret_scope",
80+ "breakpoints": true
81+ },
82+ {
83+ "idx": 11,
84+ "version": "6",
85+ "when": 1779087600000,
86+ "tag": "0011_plugin_storage_sources",
87+ "breakpoints": true
88+ },
89+ {
90+ "idx": 12,
91+ "version": "6",
92+ "when": 1779998400000,
93+ "tag": "0012_connection_identity_override",
94+ "breakpoints": true
95+ }
96+ ]
97+}
apps/local/src/db/v1-v2-migration.test.ts+79−0
@@ -1,4 +1,6 @@
11 import { afterEach, beforeEach, describe, expect, it } from "@effect/vitest";
2+import { drizzle } from "drizzle-orm/libsql";
3+import { migrate } from "drizzle-orm/libsql/migrator";
24 import { Buffer } from "node:buffer";
35 import { mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";
46 import { tmpdir } from "node:os";
@@ -14,6 +16,12 @@ import { migrateLocalV1ToV2IfNeeded } from "./v1-v2-migration"; · 9 unmodified lines
1416 const AuthFile = Schema.Record(Schema.String, Schema.String);
1517 const decodeAuthFile = Schema.decodeUnknownSync(Schema.fromJsonString(AuthFile));
1618 const decodeUnknownJson = Schema.decodeUnknownSync(Schema.fromJsonString(Schema.Unknown));
19+// Preserves every journal field (`when`, `breakpoints`, …) — drizzle's
20+// migrator needs them, so only `entries` is typed for the truncation filter.
21+const decodeJournal = (text: string) =>
22+ decodeUnknownJson(text) as {
23+ readonly entries: ReadonlyArray<{ readonly idx: number; readonly tag: string }>;
24+ };
1725
1826 let workDir: string;
1927 let previousXdgDataHome: string | undefined;
@@ -1107,4 +1115,75 @@ describe("local v1 -> v2 migration", () => { · 1087 unmodified lines
11071115 ]);
11081116 client.close();
11091117 });
1118+
1119+ // Regression: a database last touched by a release OLDER than v1-final
1120+ // (pre-0011 — no `plugin_storage` table) must be replayed through the
1121+ // bundled legacy drizzle chain before the v1→v2 data migration reads it.
1122+ // Without the replay, migration crashed with "no such table: plugin_storage"
1123+ // on every fresh 1.5.0 install over old data.
1124+ it("replays the legacy drizzle chain for a v1 database that predates v1-final", async () => {
1125+ const scopeId = "executor-workspace-abcd1234";
1126+ const dataDir = join(workDir, "data");
1127+ const dbPath = join(dataDir, "data.db");
1128+ mkdirSync(dataDir, { recursive: true });
1129+
1130+ // Build a REAL pre-0011 v1 database: apply the vendored legacy chain
1131+ // truncated after 0010, so drizzle records the genuine hashes and the
1132+ // schema has per-plugin source tables but no `plugin_storage`.
1133+ const legacyDir = join(import.meta.dirname, "../../drizzle-legacy-v1");
1134+ const truncatedDir = join(workDir, "legacy-truncated");
1135+ mkdirSync(join(truncatedDir, "meta"), { recursive: true });
1136+ const journal = decodeJournal(
1137+ readFileSync(join(legacyDir, "meta", "_journal.json")).toString(),
1138+ );
1139+ const kept = journal.entries.filter((entry) => entry.idx <= 10);
1140+ for (const entry of kept) {
1141+ writeFileSync(
1142+ join(truncatedDir, `${entry.tag}.sql`),
1143+ readFileSync(join(legacyDir, `${entry.tag}.sql`)),
1144+ );
1145+ }
1146+ writeFileSync(
1147+ join(truncatedDir, "meta", "_journal.json"),
1148+ JSON.stringify({ ...journal, entries: kept }),
1149+ );
1150+
1151+ const seed = await openLocalLibsql(dbPath);
1152+ await migrate(drizzle({ client: seed }), { migrationsFolder: truncatedDir });
1153+ const missing = await seed.execute(
1154+ "SELECT name FROM sqlite_master WHERE type = 'table' AND name = 'plugin_storage'",
1155+ );
1156+ expect(missing.rows).toEqual([]); // genuinely pre-0011
1157+ const now = Date.now();
1158+ await executeSql(
1159+ seed,
1160+ "INSERT INTO source (id, scope_id, plugin_id, kind, name, url, can_remove, can_refresh, can_edit, created_at, updated_at) VALUES (?, ?, ?, ?, ?, ?, 1, 1, 1, ?, ?)",
1161+ ["context7", scopeId, "mcp", "mcp", "Context7", "https://mcp.context7.com/mcp", now, now],
1162+ );
1163+ await executeSql(
1164+ seed,
1165+ "INSERT INTO mcp_source (id, scope_id, name, config, created_at) VALUES (?, ?, ?, ?, ?)",
1166+ [
1167+ "context7",
1168+ scopeId,
1169+ "Context7",
1170+ JSON.stringify({ transport: "remote", endpoint: "https://mcp.context7.com/mcp" }),
1171+ now,
1172+ ],
1173+ );
1174+ seed.close();
1175+
1176+ const result = await migrateLocalV1ToV2IfNeeded({
1177+ sqlitePath: dbPath,
1178+ tables: collectTables(),
1179+ namespace: "executor_local",
1180+ tenantId: scopeId,
1181+ });
1182+
1183+ expect(result.migrated).toBe(true);
1184+ const client = await openLocalLibsql(dbPath);
1185+ const integrations = await client.execute("SELECT tenant, slug, plugin_id FROM integration");
1186+ expect(integrations.rows).toEqual([{ tenant: scopeId, slug: "context7", plugin_id: "mcp" }]);
1187+ client.close();
1188+ });
11101189 });
apps/local/src/db/v1-v2-migration.ts+81−3
@@ -1,11 +1,13 @@
11 /* oxlint-disable executor/no-json-parse, executor/no-raw-fetch, executor/no-try-catch-or-throw -- boundary: one-shot local SQLite/auth-file migration normalizes legacy on-disk state */
22
33 import type { Client } from "@libsql/client";
4+import { drizzle } from "drizzle-orm/libsql";
5+import { migrate } from "drizzle-orm/libsql/migrator";
46 import { Effect } from "effect";
57 import { createId } from "fumadb/cuid";
6−import { randomBytes } from "node:crypto";
8+import { createHash, randomBytes } from "node:crypto";
79 import * as fs from "node:fs";
8−import { homedir } from "node:os";
10+import { homedir, tmpdir } from "node:os";
911 import { dirname, join } from "node:path";
1012 import { TextDecoder } from "node:util";
1113
@@ -34,6 +36,7 @@ import { · 22 unmodified lines
3436 import { makeKeychainProvider } from "@executor-js/plugin-keychain";
3537
3638 import { createSqliteFumaDb } from "./sqlite-fumadb";
39+import embeddedLegacyMigrations from "./embedded-migrations.gen";
3740 import { executeSql, openLocalLibsql, queryFirst, queryRows } from "./libsql";
3841
3942 type Row = Record<string, unknown>;
@@ -131,6 +134,79 @@ const isLocalV1Database = async (client: Client): Promise<boolean> => { · 91 unmodified lines
131134 return !connectionColumns.has("tenant") || connectionColumns.has("scope_id");
132135 };
133136
137+// ---------------------------------------------------------------------------
138+// Legacy v1 schema replay.
139+//
140+// The v1→v2 data migration below reads the v1-FINAL schema (it queries
141+// `plugin_storage`, which only exists after v1 migration 0011). A database
142+// last touched by an older release is still mid-chain, so replay the bundled
143+// legacy drizzle migrations (`apps/local/drizzle-legacy-v1`, embedded into
144+// the binary by apps/cli/src/build.ts) to bring it to v1-final first — the
145+// same step every pre-v1.5 release performed at startup.
146+// ---------------------------------------------------------------------------
147+
148+const resolveLegacyMigrationsFolder = (): string => {
149+ if (!embeddedLegacyMigrations) {
150+ return join(import.meta.dirname, "../../drizzle-legacy-v1");
151+ }
152+ // drizzle's migrate() reads a folder from disk; materialize the embedded
153+ // contents into a tmpdir.
154+ const dir = fs.mkdtempSync(join(tmpdir(), "executor-legacy-migrations-"));
155+ for (const [rel, content] of Object.entries(embeddedLegacyMigrations)) {
156+ const target = join(dir, rel);
157+ fs.mkdirSync(dirname(target), { recursive: true });
158+ fs.writeFileSync(target, content);
159+ }
160+ return dir;
161+};
162+
163+const readBundledLegacyMigrationHashes = (migrationsFolder: string): readonly string[] => {
164+ const journal = JSON.parse(
165+ fs.readFileSync(join(migrationsFolder, "meta", "_journal.json")).toString(),
166+ ) as { entries: ReadonlyArray<{ idx: number; tag: string }> };
167+ return [...journal.entries]
168+ .sort((left, right) => left.idx - right.idx)
169+ .map((entry) =>
170+ createHash("sha256")
171+ .update(fs.readFileSync(join(migrationsFolder, `${entry.tag}.sql`)).toString())
172+ .digest("hex"),
173+ );
174+};
175+
176+const readAppliedLegacyMigrationHashes = async (client: Client): Promise<readonly string[]> =>
177+ (
178+ await queryRows<{ hash: string }>(
179+ client,
180+ "SELECT hash FROM __drizzle_migrations ORDER BY id ASC",
181+ )
182+ ).map((row) => row.hash);
183+
184+/** Bring a v1 database that predates v1-final up to the last v1 schema. Only
185+ * replays when the database's drizzle history is a strict prefix of the
186+ * bundled legacy chain — anything else is left as-is with a warning (the
187+ * data migration may still succeed if the schema is close enough). */
188+const replayLegacyV1Migrations = async (client: Client, warnings: string[]): Promise<void> => {
189+ if (!(await tableExists(client, "__drizzle_migrations"))) {
190+ warnings.push(
191+ "v1 database has no drizzle migration history; skipping the legacy schema replay.",
192+ );
193+ return;
194+ }
195+ const migrationsFolder = resolveLegacyMigrationsFolder();
196+ const bundled = readBundledLegacyMigrationHashes(migrationsFolder);
197+ const applied = await readAppliedLegacyMigrationHashes(client);
198+ const isPrefix =
199+ applied.length <= bundled.length && applied.every((hash, index) => hash === bundled[index]);
200+ if (!isPrefix) {
201+ warnings.push(
202+ "v1 database migration history does not match this build's bundled legacy migrations; reading the schema as-is.",
203+ );
204+ return;
205+ }
206+ if (applied.length === bundled.length) return; // already v1-final
207+ await migrate(drizzle({ client }), { migrationsFolder });
208+};
209+
134210 const textDecoder = new TextDecoder();
135211
136212 const decodeBytes = (value: ArrayBuffer | ArrayBufferView): string => {
@@ -837,6 +913,8 @@ export const migrateLocalV1ToV2IfNeeded = async ( · 700 unmodified lines
837913 const reader = await openLocalLibsql(options.sqlitePath);
838914 try {
839915 if (!(await isLocalV1Database(reader))) return { migrated: false, warnings: [] };
916+ const replayWarnings: string[] = [];
917+ await replayLegacyV1Migrations(reader, replayWarnings);
840918 const snapshot = await readV1Snapshot(reader, options.tenantId);
841919 const plan = planMigration(snapshot.input);
842920 const secretValues = await collectSecretValues(plan);
@@ -872,7 +950,7 @@ export const migrateLocalV1ToV2IfNeeded = async ( · 29 unmodified lines
872950 migrated: true,
873951 backupPath,
874952 report: plan.report,
875− warnings: [...plan.report.warnings, ...secretValues.warnings],
953+ warnings: [...replayWarnings, ...plan.report.warnings, ...secretValues.warnings],
876954 };
877955 } catch (cause) {
878956 if (target) await target.close();

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t