002-production-v2.xml 4.03 KB
<?xml version="1.0" encoding="UTF-8"?>
<databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
                   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                   xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
                                       https://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-4.27.xsd">

    <!--
        pbc-production v2 schema additions.

        Two things:

        1) Widen the status CHECK constraint to admit IN_PROGRESS.
           v1 locked DRAFT / COMPLETED / CANCELLED only; v2 adds an
           IN_PROGRESS state between DRAFT and COMPLETED so
           started-but-not-finished work is observable.

        2) Create production__work_order_input — the BOM child table.
           Each row is one raw material consumed per unit of output.
           complete() iterates these lines and writes one
           MATERIAL_ISSUE ledger row per line before writing the
           PRODUCTION_RECEIPT, all in one transaction. An empty list
           is legal (complete() just writes the PRODUCTION_RECEIPT
           and nothing else), which lets the SalesOrderConfirmedSubscriber
           keep auto-spawning orders without knowing the BOM.

        NEITHER source_location_code NOR item_code is a foreign key —
        same cross-PBC rationale as the parent table. The application
        enforces existence through CatalogApi / LocationJpaRepository
        at create time.
    -->

    <changeSet id="production-v2-001-status-in-progress" author="vibe_erp">
        <comment>Widen production__work_order.status CHECK to allow IN_PROGRESS</comment>
        <sql>
            ALTER TABLE production__work_order
                DROP CONSTRAINT production__work_order_status_check;
            ALTER TABLE production__work_order
                ADD CONSTRAINT production__work_order_status_check
                CHECK (status IN ('DRAFT', 'IN_PROGRESS', 'COMPLETED', 'CANCELLED'));
        </sql>
        <rollback>
            ALTER TABLE production__work_order
                DROP CONSTRAINT production__work_order_status_check;
            ALTER TABLE production__work_order
                ADD CONSTRAINT production__work_order_status_check
                CHECK (status IN ('DRAFT', 'COMPLETED', 'CANCELLED'));
        </rollback>
    </changeSet>

    <changeSet id="production-v2-002-work-order-input" author="vibe_erp">
        <comment>Create production__work_order_input (BOM child table)</comment>
        <sql>
            CREATE TABLE production__work_order_input (
                id                      uuid PRIMARY KEY,
                work_order_id           uuid           NOT NULL
                    REFERENCES production__work_order(id) ON DELETE CASCADE,
                line_no                 integer        NOT NULL,
                item_code               varchar(64)    NOT NULL,
                quantity_per_unit       numeric(18,4)  NOT NULL,
                source_location_code    varchar(64)    NOT NULL,
                created_at              timestamptz    NOT NULL,
                created_by              varchar(128)   NOT NULL,
                updated_at              timestamptz    NOT NULL,
                updated_by              varchar(128)   NOT NULL,
                version                 bigint         NOT NULL DEFAULT 0,
                CONSTRAINT production__work_order_input_qty_pos
                    CHECK (quantity_per_unit &gt; 0),
                CONSTRAINT production__work_order_input_line_no_pos
                    CHECK (line_no &gt; 0),
                CONSTRAINT production__work_order_input_line_uk
                    UNIQUE (work_order_id, line_no)
            );
            CREATE INDEX production__work_order_input_wo_idx
                ON production__work_order_input (work_order_id);
            CREATE INDEX production__work_order_input_item_idx
                ON production__work_order_input (item_code);
        </sql>
        <rollback>
            DROP TABLE production__work_order_input;
        </rollback>
    </changeSet>

</databaseChangeLog>