-- CreateEnum CREATE TYPE "ProductStatus" AS ENUM ('DRAFT', 'PUBLISHED', 'ARCHIVED'); -- CreateEnum CREATE TYPE "GroupKind" AS ENUM ('CATEGORY', 'COLLECTION'); -- CreateTable CREATE TABLE "addresses" ( "id" UUID NOT NULL, "user_id" UUID NOT NULL, "organization_id" UUID NOT NULL, "recipient" VARCHAR(160) NOT NULL, "line1" VARCHAR(200) NOT NULL, "line2" VARCHAR(200) NOT NULL DEFAULT '', "city" VARCHAR(100) NOT NULL, "region" VARCHAR(100) NOT NULL, "postal_code" VARCHAR(20) NOT NULL, "country_code" CHAR(2) NOT NULL, "phone" VARCHAR(16) NOT NULL, "is_default" BOOLEAN NOT NULL DEFAULT false, "created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, "updated_at" TIMESTAMPTZ(3) NOT NULL, CONSTRAINT "addresses_pkey" PRIMARY KEY ("id") ); -- CreateTable CREATE TABLE "products" ( "id" UUID NOT NULL, "organization_id" UUID NOT NULL, "name" VARCHAR(160) NOT NULL, "slug" VARCHAR(160) NOT NULL, "description" VARCHAR(5000) NOT NULL DEFAULT '', "status" "ProductStatus" NOT NULL DEFAULT 'DRAFT', "created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, "updated_at" TIMESTAMPTZ(3) NOT NULL, CONSTRAINT "products_pkey" PRIMARY KEY ("id") ); -- CreateTable CREATE TABLE "product_variants" ( "id" UUID NOT NULL, "product_id" UUID NOT NULL, "organization_id" UUID NOT NULL, "sku" VARCHAR(64) NOT NULL, "name" VARCHAR(160) NOT NULL, "price" DECIMAL(12,2) NOT NULL, "currency" CHAR(3) NOT NULL DEFAULT 'INR', "attributes" JSONB NOT NULL DEFAULT '{}', "active" BOOLEAN NOT NULL DEFAULT true, CONSTRAINT "product_variants_pkey" PRIMARY KEY ("id") ); -- CreateTable CREATE TABLE "catalog_groups" ( "id" UUID NOT NULL, "organization_id" UUID NOT NULL, "name" VARCHAR(100) NOT NULL, "slug" VARCHAR(100) NOT NULL, "kind" "GroupKind" NOT NULL, CONSTRAINT "catalog_groups_pkey" PRIMARY KEY ("id") ); -- CreateTable CREATE TABLE "product_groups" ( "product_id" UUID NOT NULL, "group_id" UUID NOT NULL, "organization_id" UUID NOT NULL, CONSTRAINT "product_groups_pkey" PRIMARY KEY ("product_id","group_id") ); -- CreateIndex CREATE INDEX "addresses_user_id_organization_id_created_at_id_idx" ON "addresses"("user_id", "organization_id", "created_at", "id"); -- CreateIndex CREATE INDEX "products_organization_id_status_created_at_id_idx" ON "products"("organization_id", "status", "created_at", "id"); -- CreateIndex CREATE UNIQUE INDEX "products_organization_id_slug_key" ON "products"("organization_id", "slug"); -- CreateIndex CREATE UNIQUE INDEX "products_id_organization_id_key" ON "products"("id", "organization_id"); -- CreateIndex CREATE INDEX "product_variants_product_id_active_idx" ON "product_variants"("product_id", "active"); -- CreateIndex CREATE UNIQUE INDEX "product_variants_organization_id_sku_key" ON "product_variants"("organization_id", "sku"); -- CreateIndex CREATE UNIQUE INDEX "product_variants_id_organization_id_key" ON "product_variants"("id", "organization_id"); -- CreateIndex CREATE UNIQUE INDEX "catalog_groups_organization_id_kind_slug_key" ON "catalog_groups"("organization_id", "kind", "slug"); -- CreateIndex CREATE UNIQUE INDEX "catalog_groups_id_organization_id_key" ON "catalog_groups"("id", "organization_id"); -- CreateIndex CREATE INDEX "product_groups_group_id_organization_id_idx" ON "product_groups"("group_id", "organization_id"); -- AddForeignKey ALTER TABLE "addresses" ADD CONSTRAINT "addresses_user_id_organization_id_fkey" FOREIGN KEY ("user_id", "organization_id") REFERENCES "users"("id", "organization_id") ON DELETE CASCADE ON UPDATE CASCADE; -- AddForeignKey ALTER TABLE "products" ADD CONSTRAINT "products_organization_id_fkey" FOREIGN KEY ("organization_id") REFERENCES "organizations"("id") ON DELETE RESTRICT ON UPDATE CASCADE; -- AddForeignKey ALTER TABLE "product_variants" ADD CONSTRAINT "product_variants_product_id_organization_id_fkey" FOREIGN KEY ("product_id", "organization_id") REFERENCES "products"("id", "organization_id") ON DELETE RESTRICT ON UPDATE CASCADE; -- AddForeignKey ALTER TABLE "catalog_groups" ADD CONSTRAINT "catalog_groups_organization_id_fkey" FOREIGN KEY ("organization_id") REFERENCES "organizations"("id") ON DELETE RESTRICT ON UPDATE CASCADE; -- AddForeignKey ALTER TABLE "product_groups" ADD CONSTRAINT "product_groups_product_id_organization_id_fkey" FOREIGN KEY ("product_id", "organization_id") REFERENCES "products"("id", "organization_id") ON DELETE CASCADE ON UPDATE CASCADE; -- AddForeignKey ALTER TABLE "product_groups" ADD CONSTRAINT "product_groups_group_id_organization_id_fkey" FOREIGN KEY ("group_id", "organization_id") REFERENCES "catalog_groups"("id", "organization_id") ON DELETE RESTRICT ON UPDATE CASCADE;