001-warehousing-init.xml 4.3 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-warehousing initial schema (P5.4).

        Owns: warehousing__stock_transfer + warehousing__stock_transfer_line.

        StockTransfer is a first-class orchestration aggregate on top of
        the flat InventoryApi.recordMovement primitive. Confirming a DRAFT
        transfer writes one TRANSFER_OUT + TRANSFER_IN pair per line
        atomically in a single transaction, referenced TR:<code>.

        Neither item_code nor the location codes are foreign keys — they
        are cross-PBC references. DB-level FKs across PBCs would couple
        pbc-warehousing's schema with pbc-catalog and pbc-inventory at
        the storage level, defeating the bounded-context rule. Existence
        is enforced at the application layer via CatalogApi at create time
        and InventoryApi.recordMovement at confirm time.
    -->

    <changeSet id="warehousing-init-001" author="vibe_erp">
        <comment>Create warehousing__stock_transfer + warehousing__stock_transfer_line tables</comment>
        <sql>
            CREATE TABLE warehousing__stock_transfer (
                id                      uuid PRIMARY KEY,
                code                    varchar(64)   NOT NULL,
                from_location_code      varchar(64)   NOT NULL,
                to_location_code        varchar(64)   NOT NULL,
                status                  varchar(16)   NOT NULL,
                transfer_date           date,
                note                    varchar(512),
                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 warehousing__stock_transfer_status_check
                    CHECK (status IN ('DRAFT', 'CONFIRMED', 'CANCELLED')),
                CONSTRAINT warehousing__stock_transfer_locations_distinct
                    CHECK (from_location_code &lt;&gt; to_location_code)
            );
            CREATE UNIQUE INDEX warehousing__stock_transfer_code_uk
                ON warehousing__stock_transfer (code);
            CREATE INDEX warehousing__stock_transfer_status_idx
                ON warehousing__stock_transfer (status);
            CREATE INDEX warehousing__stock_transfer_from_idx
                ON warehousing__stock_transfer (from_location_code);
            CREATE INDEX warehousing__stock_transfer_to_idx
                ON warehousing__stock_transfer (to_location_code);

            CREATE TABLE warehousing__stock_transfer_line (
                id                      uuid PRIMARY KEY,
                transfer_id             uuid          NOT NULL
                    REFERENCES warehousing__stock_transfer (id) ON DELETE CASCADE,
                line_no                 integer       NOT NULL,
                item_code               varchar(64)   NOT NULL,
                quantity                numeric(18,4) 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 warehousing__stock_transfer_line_qty_pos
                    CHECK (quantity &gt; 0)
            );
            CREATE UNIQUE INDEX warehousing__stock_transfer_line_uk
                ON warehousing__stock_transfer_line (transfer_id, line_no);
            CREATE INDEX warehousing__stock_transfer_line_item_idx
                ON warehousing__stock_transfer_line (item_code);
        </sql>
        <rollback>
            DROP TABLE warehousing__stock_transfer_line;
            DROP TABLE warehousing__stock_transfer;
        </rollback>
    </changeSet>

</databaseChangeLog>