You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
ALTERTABLE"news_articles" ADD CONSTRAINT"news_articles_author_id_user_id_fk"FOREIGN KEY ("author_id") REFERENCES"public"."user"("id") ON DELETEsetnullONUPDATE no action;
1
+
DO $$ BEGIN
2
+
IF EXISTS (SELECT1FROMinformation_schema.columnsWHERE table_schema ='public'AND table_name ='news_articles'AND column_name ='author') THEN
3
+
ALTERTABLE"news_articles" RENAME COLUMN "author" TO "author_id";
4
+
END IF;
5
+
END $$;--> statement-breakpoint
6
+
DO $$ BEGIN
7
+
IF EXISTS (SELECT1FROMinformation_schema.columnsWHERE table_schema ='public'AND table_name ='news_articles'AND column_name ='author_id'AND is_nullable ='NO') THEN
8
+
ALTERTABLE"news_articles" ALTER COLUMN "author_id" DROP NOT NULL;
9
+
END IF;
10
+
END $$;--> statement-breakpoint
11
+
DO $$ BEGIN
12
+
IF EXISTS (SELECT1FROMinformation_schema.columns c WHEREc.table_schema='public'ANDc.table_name='news_articles'ANDc.column_name='author_id'ANDc.data_type<>'text') THEN
13
+
ALTERTABLE"news_articles" ALTER COLUMN "author_id"SET DATA TYPE text;
14
+
END IF;
15
+
END $$;--> statement-breakpoint
16
+
DO $$ BEGIN
17
+
IF EXISTS (SELECT1FROMinformation_schema.columnsWHERE table_schema ='public'AND table_name ='documents'AND column_name ='slug'AND column_default IS NOT NULL) THEN
18
+
ALTERTABLE"documents" ALTER COLUMN "slug" DROP DEFAULT;
19
+
END IF;
20
+
END $$;--> statement-breakpoint
21
+
DO $$ BEGIN
22
+
IF EXISTS (SELECT1FROMinformation_schema.columnsWHERE table_schema ='public'AND table_name ='news_articles'AND column_name ='id'AND data_type <>'text') THEN
23
+
ALTERTABLE"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 (SELECT1FROMinformation_schema.columnsWHERE table_schema ='public'AND table_name ='pages'AND column_name ='slug_name'AND column_default IS NOT NULL) THEN
28
+
ALTERTABLE"pages" ALTER COLUMN "slug_name" DROP DEFAULT;
29
+
END IF;
30
+
END $$;--> statement-breakpoint
31
+
UPDATE"news_articles"SET"author_id"=NULLWHERE"author_id"IS NOT NULL;--> statement-breakpoint
32
+
DO $$ BEGIN
33
+
IF NOT EXISTS (SELECT1FROM pg_constraint WHERE conname ='news_articles_author_id_user_id_fk') THEN
34
+
ALTERTABLE"news_articles" ADD CONSTRAINT"news_articles_author_id_user_id_fk"FOREIGN KEY ("author_id") REFERENCES"public"."user"("id") ON DELETEsetnullONUPDATE no action;
0 commit comments