Skip to content

Commit 9659823

Browse files
committed
fix(db): make expand-only migration deferral honest in schema.ts
schema.ts still declared .references(() => folder.id) on workflow.folderId/workspaceFiles.folderId even though the migration defers adding that FK to a future contract migration (blue/green safety). That left the drizzle snapshot recording the FK as already present, so a future `drizzle-kit generate` would never emit the contract migration to actually add it back. Drop the .references() from both columns (plain text columns until the contract phase re-adds them) and regenerate 0258 from scratch so the snapshot matches the real post-migration schema.
1 parent 8e355c3 commit 9659823

4 files changed

Lines changed: 1297 additions & 428 deletions

File tree

packages/db/migrations/0258_lyrical_flatman.sql renamed to packages/db/migrations/0258_lumpy_terrax.sql

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,18 @@ CREATE TABLE "pinned_item" (
2222
"pinned_at" timestamp DEFAULT now() NOT NULL
2323
);
2424
--> statement-breakpoint
25+
-- Drops the FK's old target only, no replacement added here — this strictly loosens
26+
-- the column (removes a check, adds none), so it cannot reject any write that used to
27+
-- succeed. Adding a `folder`-targeted FK in this same migration would reject a
28+
-- still-running old-code pod (blue/green cutover) writing a workflow_folder-only id;
29+
-- that FK is deferred to a follow-up migration once old code is fully drained
30+
-- (contract-pending marker on workflow.folderId in schema.ts). App-layer validation
31+
-- already independently enforces the invariant on both old and new code paths.
32+
-- migration-safe: strictly loosens the column, no cross-deploy write can be rejected by removing this constraint; see contract-pending marker on workflow.folderId in schema.ts
2533
ALTER TABLE "workflow" DROP CONSTRAINT "workflow_folder_id_workflow_folder_id_fk";
2634
--> statement-breakpoint
35+
-- Same reasoning as the workflow.folder_id drop above.
36+
-- migration-safe: strictly loosens the column, no cross-deploy write can be rejected by removing this constraint; see contract-pending marker on workspaceFiles.folderId in schema.ts
2737
ALTER TABLE "workspace_files" DROP CONSTRAINT "workspace_files_folder_id_workspace_file_folders_id_fk";
2838
--> statement-breakpoint
2939
ALTER TABLE "knowledge_base" ADD COLUMN "folder_id" text;--> statement-breakpoint
@@ -117,9 +127,24 @@ FROM (
117127
FROM "workspace_file_folders"
118128
) "ranked_workspace_file_folders";
119129
--> statement-breakpoint
120-
ALTER TABLE "knowledge_base" ADD CONSTRAINT "knowledge_base_folder_id_folder_id_fk" FOREIGN KEY ("folder_id") REFERENCES "public"."folder"("id") ON DELETE set null ON UPDATE no action;--> statement-breakpoint
121-
ALTER TABLE "user_table_definitions" ADD CONSTRAINT "user_table_definitions_folder_id_folder_id_fk" FOREIGN KEY ("folder_id") REFERENCES "public"."folder"("id") ON DELETE set null ON UPDATE no action;--> statement-breakpoint
122-
ALTER TABLE "workflow" ADD CONSTRAINT "workflow_folder_id_folder_id_fk" FOREIGN KEY ("folder_id") REFERENCES "public"."folder"("id") ON DELETE set null ON UPDATE no action;--> statement-breakpoint
123-
ALTER TABLE "workspace_files" ADD CONSTRAINT "workspace_files_folder_id_folder_id_fk" FOREIGN KEY ("folder_id") REFERENCES "public"."folder"("id") ON DELETE set null ON UPDATE no action;--> statement-breakpoint
124-
CREATE INDEX "kb_folder_id_idx" ON "knowledge_base" USING btree ("folder_id");--> statement-breakpoint
125-
CREATE INDEX "user_table_def_folder_id_idx" ON "user_table_definitions" USING btree ("folder_id");
130+
-- knowledge_base.folder_id / user_table_definitions.folder_id are brand-new columns added
131+
-- earlier in this same migration (all rows NULL) -- no old or new code reads/writes them
132+
-- yet, so adding their FK here (unlike workflow/workspace_files above) has zero cross-deploy
133+
-- write-compatibility risk. NOT VALID + an immediate VALIDATE still avoids the full-table
134+
-- lock a plain ADD CONSTRAINT would take (validating an all-NULL column is instant either
135+
-- way, but this keeps the pattern uniform and matches this repo's established precedent,
136+
-- e.g. migrations/0243_kb_workspace_cascade.sql).
137+
ALTER TABLE "knowledge_base" ADD CONSTRAINT "knowledge_base_folder_id_folder_id_fk" FOREIGN KEY ("folder_id") REFERENCES "public"."folder"("id") ON DELETE set null ON UPDATE no action NOT VALID;--> statement-breakpoint
138+
ALTER TABLE "knowledge_base" VALIDATE CONSTRAINT "knowledge_base_folder_id_folder_id_fk";--> statement-breakpoint
139+
ALTER TABLE "user_table_definitions" ADD CONSTRAINT "user_table_definitions_folder_id_folder_id_fk" FOREIGN KEY ("folder_id") REFERENCES "public"."folder"("id") ON DELETE set null ON UPDATE no action NOT VALID;--> statement-breakpoint
140+
ALTER TABLE "user_table_definitions" VALIDATE CONSTRAINT "user_table_definitions_folder_id_folder_id_fk";--> statement-breakpoint
141+
-- knowledge_base/user_table_definitions are existing tables: build these indexes
142+
-- CONCURRENTLY so the build never write-locks the relation (runner convention -- plain
143+
-- CREATE INDEX takes ACCESS EXCLUSIVE; see packages/db/scripts/migrate.ts). This must be
144+
-- the last statement group in the file -- CONCURRENTLY cannot run inside the migration
145+
-- transaction, so everything after the COMMIT below runs outside it.
146+
COMMIT;--> statement-breakpoint
147+
SET lock_timeout = 0;--> statement-breakpoint
148+
CREATE INDEX CONCURRENTLY IF NOT EXISTS "kb_folder_id_idx" ON "knowledge_base" USING btree ("folder_id");--> statement-breakpoint
149+
CREATE INDEX CONCURRENTLY IF NOT EXISTS "user_table_def_folder_id_idx" ON "user_table_definitions" USING btree ("folder_id");--> statement-breakpoint
150+
SET lock_timeout = '5s';

0 commit comments

Comments
 (0)