manicanldes-backend/prisma/migrations/20260928153219_product_boms/migration.sql

78 lines
5.0 KiB
PL/PgSQL

CREATE TYPE "BomStatus" AS ENUM ('DRAFT', 'ACTIVE', 'SUPERSEDED');
CREATE TABLE "product_boms" (
"id" UUID NOT NULL,
"organization_id" UUID NOT NULL,
"variant_id" UUID NOT NULL,
"created_by_id" UUID NOT NULL,
"revision" INTEGER NOT NULL,
"output_quantity" INTEGER NOT NULL,
"status" "BomStatus" NOT NULL DEFAULT 'DRAFT',
"created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"activated_at" TIMESTAMPTZ(3),
CONSTRAINT "product_boms_pkey" PRIMARY KEY ("id"),
CONSTRAINT "product_boms_quantity" CHECK ("revision" BETWEEN 1 AND 100 AND "output_quantity" BETWEEN 1 AND 100000),
CONSTRAINT "product_boms_activation" CHECK (("status" = 'DRAFT' AND "activated_at" IS NULL) OR ("status" <> 'DRAFT' AND "activated_at" IS NOT NULL))
);
CREATE UNIQUE INDEX "product_boms_variant_id_revision_key" ON "product_boms"("variant_id", "revision");
CREATE UNIQUE INDEX "product_boms_id_organization_id_key" ON "product_boms"("id", "organization_id");
CREATE UNIQUE INDEX "product_boms_one_active_idx" ON "product_boms"("variant_id") WHERE "status" = 'ACTIVE';
CREATE INDEX "product_boms_organization_id_variant_id_created_at_id_idx" ON "product_boms"("organization_id", "variant_id", "created_at", "id");
ALTER TABLE "product_boms" ADD CONSTRAINT "product_boms_organization_id_fkey" FOREIGN KEY ("organization_id") REFERENCES "organizations"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
ALTER TABLE "product_boms" ADD CONSTRAINT "product_boms_variant_id_organization_id_fkey" FOREIGN KEY ("variant_id", "organization_id") REFERENCES "product_variants"("id", "organization_id") ON DELETE RESTRICT ON UPDATE CASCADE;
ALTER TABLE "product_boms" ADD CONSTRAINT "product_boms_created_by_id_organization_id_fkey" FOREIGN KEY ("created_by_id", "organization_id") REFERENCES "users"("id", "organization_id") ON DELETE RESTRICT ON UPDATE CASCADE;
CREATE TABLE "product_bom_lines" (
"id" UUID NOT NULL,
"organization_id" UUID NOT NULL,
"bom_id" UUID NOT NULL,
"material_id" UUID NOT NULL,
"material_name" VARCHAR(160) NOT NULL,
"material_code" VARCHAR(32) NOT NULL,
"unit" "MaterialUnit" NOT NULL,
"quantity" DECIMAL(12,3) NOT NULL,
CONSTRAINT "product_bom_lines_pkey" PRIMARY KEY ("id"),
CONSTRAINT "product_bom_lines_quantity" CHECK ("quantity" > 0)
);
CREATE UNIQUE INDEX "product_bom_lines_bom_id_material_id_key" ON "product_bom_lines"("bom_id", "material_id");
CREATE INDEX "product_bom_lines_organization_id_material_id_idx" ON "product_bom_lines"("organization_id", "material_id");
ALTER TABLE "product_bom_lines" ADD CONSTRAINT "product_bom_lines_bom_id_organization_id_fkey" FOREIGN KEY ("bom_id", "organization_id") REFERENCES "product_boms"("id", "organization_id") ON DELETE RESTRICT ON UPDATE CASCADE;
ALTER TABLE "product_bom_lines" ADD CONSTRAINT "product_bom_lines_material_id_organization_id_fkey" FOREIGN KEY ("material_id", "organization_id") REFERENCES "materials"("id", "organization_id") ON DELETE RESTRICT ON UPDATE CASCADE;
CREATE FUNCTION check_product_bom_revision() RETURNS trigger LANGUAGE plpgsql AS $$
DECLARE previous_revision INTEGER;
BEGIN
PERFORM id FROM product_variants WHERE id = NEW.variant_id FOR UPDATE;
SELECT COALESCE(MAX(revision), 0) INTO previous_revision FROM product_boms WHERE variant_id = NEW.variant_id;
IF NEW.revision <> previous_revision + 1 THEN RAISE EXCEPTION 'Product BOM revision must be sequential'; END IF;
RETURN NEW;
END;
$$;
CREATE TRIGGER product_bom_revision_sequence BEFORE INSERT ON product_boms FOR EACH ROW EXECUTE FUNCTION check_product_bom_revision();
CREATE FUNCTION guard_product_bom_mutation() RETURNS trigger LANGUAGE plpgsql AS $$
BEGIN
IF TG_OP = 'DELETE' OR NEW.id <> OLD.id OR NEW.organization_id <> OLD.organization_id OR NEW.variant_id <> OLD.variant_id
OR NEW.created_by_id <> OLD.created_by_id OR NEW.revision <> OLD.revision OR NEW.output_quantity <> OLD.output_quantity
OR NEW.created_at <> OLD.created_at
OR NOT ((OLD.status = 'DRAFT' AND NEW.status = 'ACTIVE' AND OLD.activated_at IS NULL AND NEW.activated_at IS NOT NULL)
OR (OLD.status = 'ACTIVE' AND NEW.status = 'SUPERSEDED' AND NEW.activated_at = OLD.activated_at))
THEN RAISE EXCEPTION 'Product BOM mutation is not allowed'; END IF;
RETURN NEW;
END;
$$;
CREATE TRIGGER product_boms_guard BEFORE UPDATE OR DELETE ON product_boms FOR EACH ROW EXECUTE FUNCTION guard_product_bom_mutation();
CREATE FUNCTION guard_product_bom_line() RETURNS trigger LANGUAGE plpgsql AS $$
BEGIN
IF TG_OP <> 'INSERT' THEN RAISE EXCEPTION 'Product BOM lines are immutable'; END IF;
IF NOT EXISTS (SELECT 1 FROM product_boms WHERE id = NEW.bom_id AND organization_id = NEW.organization_id AND status = 'DRAFT')
THEN RAISE EXCEPTION 'Product BOM lines require a draft'; END IF;
RETURN NEW;
END;
$$;
CREATE TRIGGER product_bom_lines_guard BEFORE INSERT OR UPDATE OR DELETE ON product_bom_lines FOR EACH ROW EXECUTE FUNCTION guard_product_bom_line();
UPDATE "roles" SET "permissions" = array_append(array_append("permissions", 'bom.read'), 'bom.manage')
WHERE "is_system" = true AND "name" = 'Owner' AND NOT ('bom.read' = ANY("permissions"));