ALTER TYPE "public"."post_type" RENAME TO "post_type_legacy";--> statement-breakpoint CREATE TABLE "post_type" ( "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL, "key" text NOT NULL, "label_de" text NOT NULL, "label_en" text NOT NULL, "color" text DEFAULT '#191c20' NOT NULL, "sort" integer DEFAULT 0 NOT NULL, "is_active" boolean DEFAULT true NOT NULL, "created_at" timestamp with time zone DEFAULT now() NOT NULL, CONSTRAINT "post_type_key_unique" UNIQUE("key") ); --> statement-breakpoint INSERT INTO "post_type" ("key", "label_de", "label_en", "color", "sort") VALUES ('feature', 'Feature', 'Feature', '#2b4a9b', 1), ('improvement', 'Verbesserung', 'Improvement', '#2e7d5b', 2), ('fix', 'Fix', 'Fix', '#b4761a', 3), ('breaking', 'Breaking', 'Breaking', '#c43a22', 4), ('info', 'Info', 'Info', '#4a5560', 5); --> statement-breakpoint CREATE OR REPLACE FUNCTION post_type_default() RETURNS uuid LANGUAGE sql STABLE AS $$ SELECT "id" FROM "post_type" ORDER BY "is_active" DESC, "sort", "key" LIMIT 1 $$; --> statement-breakpoint CREATE TABLE "api_idempotency_key" ( "client_id" uuid NOT NULL, "key" text NOT NULL, "post_id" uuid NOT NULL, "created_at" timestamp with time zone DEFAULT now() NOT NULL, CONSTRAINT "api_idempotency_key_client_id_key_pk" PRIMARY KEY("client_id","key") ); --> statement-breakpoint ALTER TABLE "post" ADD COLUMN "type_id" uuid DEFAULT post_type_default() NOT NULL;--> statement-breakpoint UPDATE "post" SET "type_id" = "post_type"."id" FROM "post_type" WHERE "post_type"."key" = "post"."type"::text;--> statement-breakpoint ALTER TABLE "api_idempotency_key" ADD CONSTRAINT "api_idempotency_key_client_id_api_client_id_fk" FOREIGN KEY ("client_id") REFERENCES "public"."api_client"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint ALTER TABLE "api_idempotency_key" ADD CONSTRAINT "api_idempotency_key_post_id_post_id_fk" FOREIGN KEY ("post_id") REFERENCES "public"."post"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint CREATE INDEX "post_type_sort" ON "post_type" USING btree ("sort","key");--> statement-breakpoint ALTER TABLE "post" ADD CONSTRAINT "post_type_id_post_type_id_fk" FOREIGN KEY ("type_id") REFERENCES "public"."post_type"("id") ON DELETE restrict ON UPDATE no action;