Skip to content

Commit

Permalink
Add table veresions migration
Browse files Browse the repository at this point in the history
  • Loading branch information
solomonhawk committed Nov 8, 2024
1 parent 8f95300 commit 39223dc
Show file tree
Hide file tree
Showing 3 changed files with 774 additions and 0 deletions.
39 changes: 39 additions & 0 deletions packages/db/drizzle/0012_sad_iron_monger.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
CREATE TABLE IF NOT EXISTS "table_version" (
"id" uuid DEFAULT gen_random_uuid() NOT NULL,
"owner_id" uuid NOT NULL,
"table_slug" text NOT NULL,
"version" integer NOT NULL,
"title" text NOT NULL,
"definition" text NOT NULL,
"description" text,
"available_tables" text[] DEFAULT ARRAY[]::text[] NOT NULL,
"created_at" timestamp DEFAULT now() NOT NULL,
"updated_at" timestamp DEFAULT now() NOT NULL,
"deleted_at" timestamp,
CONSTRAINT "table_version_owner_id_table_slug_version_pk" PRIMARY KEY("owner_id","table_slug","version"),
CONSTRAINT "table_version_id_unique" UNIQUE("id")
);
--> statement-breakpoint
ALTER TABLE "table" RENAME COLUMN "user_id" TO "owner_id";--> statement-breakpoint
ALTER TABLE "table" DROP CONSTRAINT "tables_user_id_slug_unique";--> statement-breakpoint
ALTER TABLE "table" DROP CONSTRAINT "table_user_id_user_id_fk";
--> statement-breakpoint
DROP INDEX IF EXISTS "tables_user_id_favorited_idx";--> statement-breakpoint

ALTER TABLE "table" DROP CONSTRAINT "table_pkey";--> statement-breakpoint
ALTER TABLE "table" ADD CONSTRAINT "table_owner_id_slug_pk" PRIMARY KEY("owner_id","slug");--> statement-breakpoint
ALTER TABLE "table" ADD COLUMN "available_tables" text[] DEFAULT ARRAY[]::text[] NOT NULL;--> statement-breakpoint
DO $$ BEGIN
ALTER TABLE "table_version" ADD CONSTRAINT "table_version_owner_id_table_slug_table_owner_id_slug_fk" FOREIGN KEY ("owner_id","table_slug") REFERENCES "public"."table"("owner_id","slug") ON DELETE no action ON UPDATE no action;
EXCEPTION
WHEN duplicate_object THEN null;
END $$;
--> statement-breakpoint
DO $$ BEGIN
ALTER TABLE "table" ADD CONSTRAINT "table_owner_id_user_id_fk" FOREIGN KEY ("owner_id") REFERENCES "public"."user"("id") ON DELETE cascade ON UPDATE no action;
EXCEPTION
WHEN duplicate_object THEN null;
END $$;
--> statement-breakpoint
CREATE INDEX IF NOT EXISTS "tables_owner_id_favorited_idx" ON "table" USING btree ("owner_id","favorited" DESC NULLS LAST);--> statement-breakpoint
ALTER TABLE "table" ADD CONSTRAINT "table_id_unique" UNIQUE("id");
Loading

0 comments on commit 39223dc

Please sign in to comment.