CREATE TABLE IF NOT EXISTS "organizations" ( "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL, "name" text NOT NULL, "slug" text NOT NULL, "email" text NOT NULL, "created_at" timestamp DEFAULT now() NOT NULL, "updated_at" timestamp DEFAULT now() NOT NULL, CONSTRAINT "organizations_slug_unique" UNIQUE("slug"), CONSTRAINT "organizations_email_unique" UNIQUE("email") ); --> statement-breakpoint CREATE TABLE IF NOT EXISTS "projects" ( "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL, "name" text NOT NULL, "slug" text NOT NULL, "organization_id" uuid NOT NULL, "created_at" timestamp DEFAULT now() NOT NULL, "updated_at" timestamp DEFAULT now() NOT NULL, CONSTRAINT "projects_organization_id_slug_unique" UNIQUE("organization_id","slug") ); --> statement-breakpoint CREATE TABLE IF NOT EXISTS "api_keys" ( "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL, "key_hash" text NOT NULL, "key_prefix" text DEFAULT 'bnt_' NOT NULL, "key_type" text NOT NULL, "organization_id" uuid, "project_id" uuid, "scopes" jsonb DEFAULT '["generate"]'::jsonb NOT NULL, "created_at" timestamp DEFAULT now() NOT NULL, "expires_at" timestamp, "last_used_at" timestamp, "is_active" boolean DEFAULT true NOT NULL, "name" text, "created_by" uuid, CONSTRAINT "api_keys_key_hash_unique" UNIQUE("key_hash") ); --> statement-breakpoint DO $$ BEGIN ALTER TABLE "projects" ADD CONSTRAINT "projects_organization_id_organizations_id_fk" FOREIGN KEY ("organization_id") REFERENCES "public"."organizations"("id") ON DELETE cascade ON UPDATE no action; EXCEPTION WHEN duplicate_object THEN null; END $$; --> statement-breakpoint DO $$ BEGIN ALTER TABLE "api_keys" ADD CONSTRAINT "api_keys_organization_id_organizations_id_fk" FOREIGN KEY ("organization_id") REFERENCES "public"."organizations"("id") ON DELETE cascade ON UPDATE no action; EXCEPTION WHEN duplicate_object THEN null; END $$; --> statement-breakpoint DO $$ BEGIN ALTER TABLE "api_keys" ADD CONSTRAINT "api_keys_project_id_projects_id_fk" FOREIGN KEY ("project_id") REFERENCES "public"."projects"("id") ON DELETE cascade ON UPDATE no action; EXCEPTION WHEN duplicate_object THEN null; END $$;