CREATE TABLE "aisles" ( "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL, "store_id" uuid NOT NULL, "name" varchar(255) NOT NULL, "order" integer NOT NULL, "colour" varchar(7) NOT NULL, "created_at" timestamp DEFAULT now() ); --> statement-breakpoint CREATE TABLE "household_invite_codes" ( "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL, "household_id" uuid NOT NULL, "code" varchar(8) NOT NULL, "created_by" uuid NOT NULL, "expires_at" timestamp with time zone NOT NULL, "created_at" timestamp DEFAULT now(), CONSTRAINT "household_invite_codes_code_unique" UNIQUE("code") ); --> statement-breakpoint CREATE TABLE "items" ( "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL, "household_id" uuid NOT NULL, "name" varchar(255) NOT NULL, "barcode" varchar(255), "aisle_id" uuid, "store_id" uuid, "alternatives" json, "created_at" timestamp DEFAULT now(), "updated_at" timestamp DEFAULT now() ); --> statement-breakpoint CREATE TABLE "list_items" ( "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL, "shopping_list_id" uuid NOT NULL, "item_id" uuid NOT NULL, "quantity" integer DEFAULT 1 NOT NULL, "checked" boolean DEFAULT false NOT NULL, "added_by" uuid NOT NULL, "excluded_from_preset" boolean DEFAULT false NOT NULL, "created_at" timestamp DEFAULT now(), "updated_at" timestamp DEFAULT now() ); --> statement-breakpoint CREATE TABLE "scan_history" ( "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL, "household_id" uuid NOT NULL, "barcode" varchar(255) NOT NULL, "item_id" uuid NOT NULL, "scanned_at" timestamp with time zone DEFAULT now() NOT NULL ); --> statement-breakpoint CREATE TABLE "shopping_lists" ( "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL, "household_id" uuid NOT NULL, "store_id" uuid NOT NULL, "name" varchar(255) NOT NULL, "is_preset" boolean DEFAULT false NOT NULL, "auto_reset" boolean DEFAULT false NOT NULL, "created_at" timestamp DEFAULT now(), "updated_at" timestamp DEFAULT now() ); --> statement-breakpoint CREATE TABLE "stores" ( "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL, "household_id" uuid NOT NULL, "name" varchar(255) NOT NULL, "location" text, "created_at" timestamp DEFAULT now(), "updated_at" timestamp DEFAULT now() ); --> statement-breakpoint CREATE TABLE "account" ( "id" text PRIMARY KEY NOT NULL, "account_id" text NOT NULL, "provider_id" text NOT NULL, "user_id" text NOT NULL, "access_token" text, "refresh_token" text, "id_token" text, "access_token_expires_at" timestamp, "refresh_token_expires_at" timestamp, "scope" text, "password" text, "created_at" timestamp DEFAULT now() NOT NULL, "updated_at" timestamp NOT NULL ); --> statement-breakpoint CREATE TABLE "invitation" ( "id" text PRIMARY KEY NOT NULL, "organization_id" text NOT NULL, "email" text NOT NULL, "role" text, "status" text DEFAULT 'pending' NOT NULL, "expires_at" timestamp NOT NULL, "created_at" timestamp DEFAULT now() NOT NULL, "inviter_id" text NOT NULL ); --> statement-breakpoint CREATE TABLE "member" ( "id" text PRIMARY KEY NOT NULL, "organization_id" text NOT NULL, "user_id" text NOT NULL, "role" text DEFAULT 'member' NOT NULL, "created_at" timestamp NOT NULL ); --> statement-breakpoint CREATE TABLE "organization" ( "id" text PRIMARY KEY NOT NULL, "name" text NOT NULL, "slug" text NOT NULL, "logo" text, "created_at" timestamp NOT NULL, "metadata" text, CONSTRAINT "organization_slug_unique" UNIQUE("slug") ); --> statement-breakpoint CREATE TABLE "passkey" ( "id" text PRIMARY KEY NOT NULL, "name" text, "public_key" text NOT NULL, "user_id" text NOT NULL, "credential_id" text NOT NULL, "counter" integer NOT NULL, "device_type" text NOT NULL, "backed_up" boolean NOT NULL, "transports" text, "created_at" timestamp, "aaguid" text ); --> statement-breakpoint CREATE TABLE "session" ( "id" text PRIMARY KEY NOT NULL, "expires_at" timestamp NOT NULL, "token" text NOT NULL, "created_at" timestamp DEFAULT now() NOT NULL, "updated_at" timestamp NOT NULL, "ip_address" text, "user_agent" text, "user_id" text NOT NULL, "active_organization_id" text, CONSTRAINT "session_token_unique" UNIQUE("token") ); --> statement-breakpoint CREATE TABLE "user" ( "id" text PRIMARY KEY NOT NULL, "name" text NOT NULL, "email" text NOT NULL, "email_verified" boolean DEFAULT false NOT NULL, "image" text, "created_at" timestamp DEFAULT now() NOT NULL, "updated_at" timestamp DEFAULT now() NOT NULL, CONSTRAINT "user_email_unique" UNIQUE("email") ); --> statement-breakpoint CREATE TABLE "verification" ( "id" text PRIMARY KEY NOT NULL, "identifier" text NOT NULL, "value" text NOT NULL, "expires_at" timestamp NOT NULL, "created_at" timestamp DEFAULT now() NOT NULL, "updated_at" timestamp DEFAULT now() NOT NULL ); --> statement-breakpoint ALTER TABLE "aisles" ADD CONSTRAINT "aisles_store_id_stores_id_fk" FOREIGN KEY ("store_id") REFERENCES "public"."stores"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint ALTER TABLE "items" ADD CONSTRAINT "items_aisle_id_aisles_id_fk" FOREIGN KEY ("aisle_id") REFERENCES "public"."aisles"("id") ON DELETE set null ON UPDATE no action;--> statement-breakpoint ALTER TABLE "items" ADD CONSTRAINT "items_store_id_stores_id_fk" FOREIGN KEY ("store_id") REFERENCES "public"."stores"("id") ON DELETE set null ON UPDATE no action;--> statement-breakpoint ALTER TABLE "list_items" ADD CONSTRAINT "list_items_shopping_list_id_shopping_lists_id_fk" FOREIGN KEY ("shopping_list_id") REFERENCES "public"."shopping_lists"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint ALTER TABLE "list_items" ADD CONSTRAINT "list_items_item_id_items_id_fk" FOREIGN KEY ("item_id") REFERENCES "public"."items"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint ALTER TABLE "scan_history" ADD CONSTRAINT "scan_history_item_id_items_id_fk" FOREIGN KEY ("item_id") REFERENCES "public"."items"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint ALTER TABLE "shopping_lists" ADD CONSTRAINT "shopping_lists_store_id_stores_id_fk" FOREIGN KEY ("store_id") REFERENCES "public"."stores"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint ALTER TABLE "account" ADD CONSTRAINT "account_user_id_user_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."user"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint ALTER TABLE "invitation" ADD CONSTRAINT "invitation_organization_id_organization_id_fk" FOREIGN KEY ("organization_id") REFERENCES "public"."organization"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint ALTER TABLE "invitation" ADD CONSTRAINT "invitation_inviter_id_user_id_fk" FOREIGN KEY ("inviter_id") REFERENCES "public"."user"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint ALTER TABLE "member" ADD CONSTRAINT "member_organization_id_organization_id_fk" FOREIGN KEY ("organization_id") REFERENCES "public"."organization"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint ALTER TABLE "member" ADD CONSTRAINT "member_user_id_user_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."user"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint ALTER TABLE "passkey" ADD CONSTRAINT "passkey_user_id_user_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."user"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint ALTER TABLE "session" ADD CONSTRAINT "session_user_id_user_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."user"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint CREATE INDEX "account_userId_idx" ON "account" USING btree ("user_id");--> statement-breakpoint CREATE INDEX "invitation_organizationId_idx" ON "invitation" USING btree ("organization_id");--> statement-breakpoint CREATE INDEX "invitation_email_idx" ON "invitation" USING btree ("email");--> statement-breakpoint CREATE INDEX "member_organizationId_idx" ON "member" USING btree ("organization_id");--> statement-breakpoint CREATE INDEX "member_userId_idx" ON "member" USING btree ("user_id");--> statement-breakpoint CREATE UNIQUE INDEX "organization_slug_uidx" ON "organization" USING btree ("slug");--> statement-breakpoint CREATE INDEX "passkey_userId_idx" ON "passkey" USING btree ("user_id");--> statement-breakpoint CREATE INDEX "passkey_credentialID_idx" ON "passkey" USING btree ("credential_id");--> statement-breakpoint CREATE INDEX "session_userId_idx" ON "session" USING btree ("user_id");--> statement-breakpoint CREATE INDEX "verification_identifier_idx" ON "verification" USING btree ("identifier");