From 09a6f076d64a8095fc00d1e2afadf559e242d866 Mon Sep 17 00:00:00 2001 From: fullsizemalt <106900403+fullsizemalt@users.noreply.github.com> Date: Wed, 17 Dec 2025 00:17:33 -0800 Subject: [PATCH] fix(db): Make initial migration idempotent with IF NOT EXISTS --- apps/web/drizzle/0000_initial_schema.sql | 124 +++++++++++++++++++++++ 1 file changed, 124 insertions(+) create mode 100644 apps/web/drizzle/0000_initial_schema.sql diff --git a/apps/web/drizzle/0000_initial_schema.sql b/apps/web/drizzle/0000_initial_schema.sql new file mode 100644 index 0000000..60bcecb --- /dev/null +++ b/apps/web/drizzle/0000_initial_schema.sql @@ -0,0 +1,124 @@ +CREATE TABLE IF NOT EXISTS "user" ( + "id" text PRIMARY KEY NOT NULL, + "name" text, + "email" text NOT NULL, + "emailVerified" timestamp, + "image" text +); +CREATE TABLE IF NOT EXISTS "pastes" ( + "id" text PRIMARY KEY NOT NULL, + "title" text, + "content" text NOT NULL, + "contentType" text DEFAULT 'text/plain', + "syntax" text, + "visibility" text DEFAULT 'public', + "author_id" text, + "session_id" text, + "expires_at" timestamp, + "created_at" timestamp DEFAULT now(), + "updated_at" timestamp DEFAULT now() +); +CREATE TABLE IF NOT EXISTS "tags" ( + "id" text PRIMARY KEY NOT NULL, + "name" text NOT NULL UNIQUE, + "color" text, + "description" text +); +CREATE TABLE IF NOT EXISTS "pastes_to_tags" ( + "paste_id" text NOT NULL, + "tag_id" text NOT NULL, + CONSTRAINT "pastes_to_tags_paste_id_tag_id_pk" PRIMARY KEY("paste_id", "tag_id") +); +CREATE TABLE IF NOT EXISTS "agent_sessions" ( + "id" text PRIMARY KEY NOT NULL, + "title" text, + "agent_name" text, + "user_id" text, + "created_at" timestamp DEFAULT now() +); +CREATE TABLE IF NOT EXISTS "account" ( + "userId" text NOT NULL, + "type" text NOT NULL, + "provider" text NOT NULL, + "providerAccountId" text NOT NULL, + "refresh_token" text, + "access_token" text, + "expires_at" integer, + "token_type" text, + "scope" text, + "id_token" text, + "session_state" text, + CONSTRAINT "account_provider_providerAccountId_pk" PRIMARY KEY("provider", "providerAccountId") +); +CREATE TABLE IF NOT EXISTS "session" ( + "sessionToken" text PRIMARY KEY NOT NULL, + "userId" text NOT NULL, + "expires" timestamp NOT NULL +); +CREATE TABLE IF NOT EXISTS "verificationToken" ( + "identifier" text NOT NULL, + "token" text NOT NULL, + "expires" timestamp NOT NULL, + CONSTRAINT "verificationToken_identifier_token_pk" PRIMARY KEY("identifier", "token") +); +CREATE TABLE IF NOT EXISTS "authenticator" ( + "credentialID" text PRIMARY KEY NOT NULL UNIQUE, + "userId" text NOT NULL, + "providerAccountId" text NOT NULL, + "credentialPublicKey" text NOT NULL, + "counter" integer NOT NULL, + "credentialDeviceType" text NOT NULL, + "credentialBackedUp" boolean NOT NULL, + "transports" text, + CONSTRAINT "authenticator_userId_credentialID_pk" PRIMARY KEY("userId", "credentialID") +); +DO $$ BEGIN +ALTER TABLE "pastes" +ADD CONSTRAINT "pastes_author_id_user_id_fk" FOREIGN KEY ("author_id") REFERENCES "user"("id") ON DELETE +set null ON UPDATE no action; +EXCEPTION +WHEN duplicate_object THEN null; +END $$; +DO $$ BEGIN +ALTER TABLE "pastes" +ADD CONSTRAINT "pastes_session_id_agent_sessions_id_fk" FOREIGN KEY ("session_id") REFERENCES "agent_sessions"("id") ON DELETE +set null ON UPDATE no action; +EXCEPTION +WHEN duplicate_object THEN null; +END $$; +DO $$ BEGIN +ALTER TABLE "pastes_to_tags" +ADD CONSTRAINT "pastes_to_tags_paste_id_pastes_id_fk" FOREIGN KEY ("paste_id") REFERENCES "pastes"("id") ON DELETE cascade ON UPDATE no action; +EXCEPTION +WHEN duplicate_object THEN null; +END $$; +DO $$ BEGIN +ALTER TABLE "pastes_to_tags" +ADD CONSTRAINT "pastes_to_tags_tag_id_tags_id_fk" FOREIGN KEY ("tag_id") REFERENCES "tags"("id") ON DELETE cascade ON UPDATE no action; +EXCEPTION +WHEN duplicate_object THEN null; +END $$; +DO $$ BEGIN +ALTER TABLE "agent_sessions" +ADD CONSTRAINT "agent_sessions_user_id_user_id_fk" FOREIGN KEY ("user_id") REFERENCES "user"("id") ON DELETE cascade ON UPDATE no action; +EXCEPTION +WHEN duplicate_object THEN null; +END $$; +DO $$ BEGIN +ALTER TABLE "account" +ADD CONSTRAINT "account_userId_user_id_fk" FOREIGN KEY ("userId") REFERENCES "user"("id") ON DELETE cascade ON UPDATE no action; +EXCEPTION +WHEN duplicate_object THEN null; +END $$; +DO $$ BEGIN +ALTER TABLE "session" +ADD CONSTRAINT "session_userId_user_id_fk" FOREIGN KEY ("userId") REFERENCES "user"("id") ON DELETE cascade ON UPDATE no action; +EXCEPTION +WHEN duplicate_object THEN null; +END $$; +DO $$ BEGIN +ALTER TABLE "authenticator" +ADD CONSTRAINT "authenticator_userId_user_id_fk" FOREIGN KEY ("userId") REFERENCES "user"("id") ON DELETE cascade ON UPDATE no action; +EXCEPTION +WHEN duplicate_object THEN null; +END $$; \ No newline at end of file