Skip to content

Commit 9fbb66f

Browse files
Refactor SQL migration for news_articles and update repair script documentation
- Enhanced the SQL migration for the news_articles table to include conditional checks before altering columns and adding constraints, ensuring idempotency. - Updated the repair migration state script documentation to reflect the inclusion of migration 0008 in the idempotency notes.
1 parent a5163df commit 9fbb66f

2 files changed

Lines changed: 37 additions & 9 deletions

File tree

drizzle/0008_quick_menace.sql

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,36 @@
1-
ALTER TABLE "news_articles" RENAME COLUMN "author" TO "author_id";--> statement-breakpoint
2-
ALTER TABLE "news_articles" ALTER COLUMN "author_id" DROP NOT NULL;--> statement-breakpoint
3-
ALTER TABLE "news_articles" ALTER COLUMN "author_id" SET DATA TYPE text;--> statement-breakpoint
4-
ALTER TABLE "documents" ALTER COLUMN "slug" DROP DEFAULT;--> statement-breakpoint
5-
ALTER TABLE "news_articles" ALTER COLUMN "id" SET DATA TYPE text;--> statement-breakpoint
6-
ALTER TABLE "pages" ALTER COLUMN "slug_name" DROP DEFAULT;--> statement-breakpoint
7-
UPDATE "news_articles" SET "author_id" = NULL;--> statement-breakpoint
8-
ALTER TABLE "news_articles" ADD CONSTRAINT "news_articles_author_id_user_id_fk" FOREIGN KEY ("author_id") REFERENCES "public"."user"("id") ON DELETE set null ON UPDATE no action;
1+
DO $$ BEGIN
2+
IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema = 'public' AND table_name = 'news_articles' AND column_name = 'author') THEN
3+
ALTER TABLE "news_articles" RENAME COLUMN "author" TO "author_id";
4+
END IF;
5+
END $$;--> statement-breakpoint
6+
DO $$ BEGIN
7+
IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema = 'public' AND table_name = 'news_articles' AND column_name = 'author_id' AND is_nullable = 'NO') THEN
8+
ALTER TABLE "news_articles" ALTER COLUMN "author_id" DROP NOT NULL;
9+
END IF;
10+
END $$;--> statement-breakpoint
11+
DO $$ BEGIN
12+
IF EXISTS (SELECT 1 FROM information_schema.columns c WHERE c.table_schema = 'public' AND c.table_name = 'news_articles' AND c.column_name = 'author_id' AND c.data_type <> 'text') THEN
13+
ALTER TABLE "news_articles" ALTER COLUMN "author_id" SET DATA TYPE text;
14+
END IF;
15+
END $$;--> statement-breakpoint
16+
DO $$ BEGIN
17+
IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema = 'public' AND table_name = 'documents' AND column_name = 'slug' AND column_default IS NOT NULL) THEN
18+
ALTER TABLE "documents" ALTER COLUMN "slug" DROP DEFAULT;
19+
END IF;
20+
END $$;--> statement-breakpoint
21+
DO $$ BEGIN
22+
IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema = 'public' AND table_name = 'news_articles' AND column_name = 'id' AND data_type <> 'text') THEN
23+
ALTER TABLE "news_articles" ALTER COLUMN "id" SET DATA TYPE text USING id::text;
24+
END IF;
25+
END $$;--> statement-breakpoint
26+
DO $$ BEGIN
27+
IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema = 'public' AND table_name = 'pages' AND column_name = 'slug_name' AND column_default IS NOT NULL) THEN
28+
ALTER TABLE "pages" ALTER COLUMN "slug_name" DROP DEFAULT;
29+
END IF;
30+
END $$;--> statement-breakpoint
31+
UPDATE "news_articles" SET "author_id" = NULL WHERE "author_id" IS NOT NULL;--> statement-breakpoint
32+
DO $$ BEGIN
33+
IF NOT EXISTS (SELECT 1 FROM pg_constraint WHERE conname = 'news_articles_author_id_user_id_fk') THEN
34+
ALTER TABLE "news_articles" ADD CONSTRAINT "news_articles_author_id_user_id_fk" FOREIGN KEY ("author_id") REFERENCES "public"."user"("id") ON DELETE set null ON UPDATE no action;
35+
END IF;
36+
END $$;

scripts/repair-migration-state.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
*
66
* WARNING: Deletion is by row id (last 4), not by migration index. If your
77
* __drizzle_migrations rows are not in journal order, this can remove the wrong
8-
* records and cause re-runs of 0003, 0004, etc. Migrations 0003–0007 have been
8+
* records and cause re-runs of 0003, 0004, etc. Migrations 0003–0008 have been
99
* made idempotent (IF NOT EXISTS / conditional constraints) so re-running them
1010
* is safe.
1111
*

0 commit comments

Comments
 (0)