manicanldes-backend/prisma/migrations/20260928183508_production_b.../migration.sql

96 lines
7.3 KiB
PL/PgSQL

CREATE TYPE "ProductionBatchStatus" AS ENUM ('DRAFT', 'IN_PROGRESS', 'COMPLETED', 'CANCELLED');
CREATE TABLE "production_batches" (
"id" UUID NOT NULL,
"organization_id" UUID NOT NULL,
"bom_id" UUID NOT NULL,
"created_by_id" UUID NOT NULL,
"reference" VARCHAR(32) NOT NULL,
"planned_quantity" INTEGER NOT NULL,
"status" "ProductionBatchStatus" NOT NULL DEFAULT 'DRAFT',
"scheduled_date" DATE,
"created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"started_at" TIMESTAMPTZ(3),
CONSTRAINT "production_batches_pkey" PRIMARY KEY ("id"),
CONSTRAINT "production_batches_values" CHECK ("reference" ~ '^[A-Z0-9][A-Z0-9_-]{0,31}$' AND "planned_quantity" BETWEEN 1 AND 100000),
CONSTRAINT "production_batches_started" CHECK (("status" = 'DRAFT' AND "started_at" IS NULL) OR ("status" IN ('IN_PROGRESS', 'COMPLETED') AND "started_at" IS NOT NULL) OR "status" = 'CANCELLED')
);
CREATE UNIQUE INDEX "production_batches_organization_id_reference_key" ON "production_batches"("organization_id", "reference");
CREATE UNIQUE INDEX "production_batches_id_organization_id_key" ON "production_batches"("id", "organization_id");
CREATE INDEX "production_batches_organization_id_status_created_at_id_idx" ON "production_batches"("organization_id", "status", "created_at", "id");
CREATE INDEX "production_batches_organization_id_bom_id_idx" ON "production_batches"("organization_id", "bom_id");
ALTER TABLE "production_batches" ADD CONSTRAINT "production_batches_organization_id_fkey" FOREIGN KEY ("organization_id") REFERENCES "organizations"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
ALTER TABLE "production_batches" ADD CONSTRAINT "production_batches_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 "production_batches" ADD CONSTRAINT "production_batches_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 "production_batch_completions" (
"batch_id" UUID NOT NULL,
"organization_id" UUID NOT NULL,
"completed_by_id" UUID NOT NULL,
"good_quantity" INTEGER NOT NULL,
"rejected_quantity" INTEGER NOT NULL,
"rejection_reason" VARCHAR(500),
"created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
CONSTRAINT "production_batch_completions_pkey" PRIMARY KEY ("batch_id"),
CONSTRAINT "production_batch_completions_values" CHECK ("good_quantity" >= 0 AND "rejected_quantity" >= 0 AND ("rejected_quantity" = 0 OR ("rejection_reason" IS NOT NULL AND LENGTH(TRIM("rejection_reason")) > 0)))
);
CREATE UNIQUE INDEX "production_batch_completions_batch_id_organization_id_key" ON "production_batch_completions"("batch_id", "organization_id");
ALTER TABLE "production_batch_completions" ADD CONSTRAINT "production_batch_completions_organization_id_fkey" FOREIGN KEY ("organization_id") REFERENCES "organizations"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
ALTER TABLE "production_batch_completions" ADD CONSTRAINT "production_batch_completions_batch_id_organization_id_fkey" FOREIGN KEY ("batch_id", "organization_id") REFERENCES "production_batches"("id", "organization_id") ON DELETE RESTRICT ON UPDATE CASCADE;
ALTER TABLE "production_batch_completions" ADD CONSTRAINT "production_batch_completions_completed_by_id_organization__fkey" FOREIGN KEY ("completed_by_id", "organization_id") REFERENCES "users"("id", "organization_id") ON DELETE RESTRICT ON UPDATE CASCADE;
CREATE TABLE "production_batch_cancellations" (
"batch_id" UUID NOT NULL,
"organization_id" UUID NOT NULL,
"cancelled_by_id" UUID NOT NULL,
"reason" VARCHAR(500) NOT NULL,
"created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
CONSTRAINT "production_batch_cancellations_pkey" PRIMARY KEY ("batch_id"),
CONSTRAINT "production_batch_cancellations_reason" CHECK (LENGTH(TRIM("reason")) > 0)
);
CREATE UNIQUE INDEX "production_batch_cancellations_batch_id_organization_id_key" ON "production_batch_cancellations"("batch_id", "organization_id");
ALTER TABLE "production_batch_cancellations" ADD CONSTRAINT "production_batch_cancellations_organization_id_fkey" FOREIGN KEY ("organization_id") REFERENCES "organizations"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
ALTER TABLE "production_batch_cancellations" ADD CONSTRAINT "production_batch_cancellations_batch_id_organization_id_fkey" FOREIGN KEY ("batch_id", "organization_id") REFERENCES "production_batches"("id", "organization_id") ON DELETE RESTRICT ON UPDATE CASCADE;
ALTER TABLE "production_batch_cancellations" ADD CONSTRAINT "production_batch_cancellations_cancelled_by_id_organizatio_fkey" FOREIGN KEY ("cancelled_by_id", "organization_id") REFERENCES "users"("id", "organization_id") ON DELETE RESTRICT ON UPDATE CASCADE;
CREATE FUNCTION guard_production_batch_transition() RETURNS trigger LANGUAGE plpgsql AS $$
BEGIN
IF TG_OP = 'INSERT' THEN
IF NEW.status <> 'DRAFT' OR NEW.started_at IS NOT NULL THEN RAISE EXCEPTION 'Production batch must begin as draft'; END IF;
RETURN NEW;
END IF;
IF TG_OP = 'DELETE' THEN RAISE EXCEPTION 'Production batches cannot be deleted'; END IF;
IF NEW.id <> OLD.id OR NEW.organization_id <> OLD.organization_id OR NEW.bom_id <> OLD.bom_id
OR NEW.created_by_id <> OLD.created_by_id OR NEW.reference <> OLD.reference
OR NEW.planned_quantity <> OLD.planned_quantity OR NEW.scheduled_date IS DISTINCT FROM OLD.scheduled_date
OR NEW.created_at <> OLD.created_at OR NOT (
(OLD.status = 'DRAFT' AND NEW.status = 'IN_PROGRESS' AND OLD.started_at IS NULL AND NEW.started_at IS NOT NULL)
OR (OLD.status = 'DRAFT' AND NEW.status = 'CANCELLED' AND NEW.started_at IS NULL)
OR (OLD.status = 'IN_PROGRESS' AND NEW.status IN ('COMPLETED', 'CANCELLED') AND NEW.started_at = OLD.started_at)
)
THEN RAISE EXCEPTION 'Invalid production batch transition'; END IF;
RETURN NEW;
END;
$$;
CREATE TRIGGER production_batches_guard BEFORE INSERT OR UPDATE OR DELETE ON production_batches FOR EACH ROW EXECUTE FUNCTION guard_production_batch_transition();
CREATE FUNCTION guard_production_batch_event() RETURNS trigger LANGUAGE plpgsql AS $$
DECLARE parent production_batches%ROWTYPE;
BEGIN
IF TG_OP <> 'INSERT' THEN RAISE EXCEPTION 'Production batch events are immutable'; END IF;
SELECT * INTO parent FROM production_batches WHERE id = NEW.batch_id AND organization_id = NEW.organization_id;
IF TG_TABLE_NAME = 'production_batch_completions' THEN
IF parent.status <> 'COMPLETED' OR NEW.good_quantity + NEW.rejected_quantity <> parent.planned_quantity
THEN RAISE EXCEPTION 'Production batch completion does not reconcile'; END IF;
ELSIF parent.status <> 'CANCELLED' THEN
RAISE EXCEPTION 'Production batch is not cancelled';
END IF;
RETURN NEW;
END;
$$;
CREATE TRIGGER production_batch_completions_guard BEFORE INSERT OR UPDATE OR DELETE ON production_batch_completions FOR EACH ROW EXECUTE FUNCTION guard_production_batch_event();
CREATE TRIGGER production_batch_cancellations_guard BEFORE INSERT OR UPDATE OR DELETE ON production_batch_cancellations FOR EACH ROW EXECUTE FUNCTION guard_production_batch_event();
UPDATE "roles" SET "permissions" = array_append(array_append("permissions", 'production.read'), 'production.manage')
WHERE "is_system" = true AND "name" = 'Owner' AND NOT ('production.read' = ANY("permissions"));