001-catalog-init.xml 5.48 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-catalog initial schema.

        Owns: catalog__uom, catalog__item.

        vibe_erp is single-tenant per instance — there are no tenant_id
        columns and no Row-Level Security policies on these tables.

        Conventions enforced for every business table in vibe_erp:
          • UUID primary key
          • Audit columns: created_at, created_by, updated_at, updated_by
          • Optimistic-locking version column
          • ext jsonb NOT NULL DEFAULT '{}' for key-user custom fields
          • GIN index on ext for fast custom-field queries
    -->

    <changeSet id="catalog-init-001" author="vibe_erp">
        <comment>Create catalog__uom table</comment>
        <sql>
            CREATE TABLE catalog__uom (
                id           uuid PRIMARY KEY,
                code         varchar(16)  NOT NULL,
                name         varchar(128) NOT NULL,
                dimension    varchar(32)  NOT NULL,
                ext          jsonb        NOT NULL DEFAULT '{}'::jsonb,
                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
            );
            CREATE UNIQUE INDEX catalog__uom_code_uk ON catalog__uom (code);
            CREATE INDEX catalog__uom_ext_gin ON catalog__uom USING GIN (ext jsonb_path_ops);
        </sql>
        <rollback>
            DROP TABLE catalog__uom;
        </rollback>
    </changeSet>

    <changeSet id="catalog-init-002" author="vibe_erp">
        <comment>Create catalog__item table (FK to catalog__uom by code)</comment>
        <sql>
            CREATE TABLE catalog__item (
                id              uuid PRIMARY KEY,
                code            varchar(64)  NOT NULL,
                name            varchar(256) NOT NULL,
                description     text,
                item_type       varchar(32)  NOT NULL,
                base_uom_code   varchar(16)  NOT NULL REFERENCES catalog__uom(code),
                active          boolean      NOT NULL DEFAULT true,
                ext             jsonb        NOT NULL DEFAULT '{}'::jsonb,
                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
            );
            CREATE UNIQUE INDEX catalog__item_code_uk ON catalog__item (code);
            CREATE INDEX catalog__item_ext_gin ON catalog__item USING GIN (ext jsonb_path_ops);
            CREATE INDEX catalog__item_active_idx ON catalog__item (active);
        </sql>
        <rollback>
            DROP TABLE catalog__item;
        </rollback>
    </changeSet>

    <!--
        Seed canonical UoMs. Tagged source='core' via the audit columns
        so the future metadata uninstall logic can leave them alone when
        a plug-in is removed. The set covers the units a printing-shop
        plug-in (and most other v1.0 plug-ins) will need on day one.
    -->
    <changeSet id="catalog-init-003" author="vibe_erp">
        <comment>Seed canonical UoMs</comment>
        <sql>
            INSERT INTO catalog__uom (id, code, name, dimension, created_at, created_by, updated_at, updated_by) VALUES
                (gen_random_uuid(), 'kg',    'Kilogram',  'mass',   now(), '__seed__', now(), '__seed__'),
                (gen_random_uuid(), 'g',     'Gram',      'mass',   now(), '__seed__', now(), '__seed__'),
                (gen_random_uuid(), 't',     'Tonne',     'mass',   now(), '__seed__', now(), '__seed__'),
                (gen_random_uuid(), 'm',     'Metre',     'length', now(), '__seed__', now(), '__seed__'),
                (gen_random_uuid(), 'cm',    'Centimetre','length', now(), '__seed__', now(), '__seed__'),
                (gen_random_uuid(), 'mm',    'Millimetre','length', now(), '__seed__', now(), '__seed__'),
                (gen_random_uuid(), 'km',    'Kilometre', 'length', now(), '__seed__', now(), '__seed__'),
                (gen_random_uuid(), 'm2',    'Square metre','area', now(), '__seed__', now(), '__seed__'),
                (gen_random_uuid(), 'l',     'Litre',     'volume', now(), '__seed__', now(), '__seed__'),
                (gen_random_uuid(), 'ml',    'Millilitre','volume', now(), '__seed__', now(), '__seed__'),
                (gen_random_uuid(), 'ea',    'Each',      'count',  now(), '__seed__', now(), '__seed__'),
                (gen_random_uuid(), 'sheet', 'Sheet',     'count',  now(), '__seed__', now(), '__seed__'),
                (gen_random_uuid(), 'pack',  'Pack',      'count',  now(), '__seed__', now(), '__seed__'),
                (gen_random_uuid(), 'h',     'Hour',      'time',   now(), '__seed__', now(), '__seed__'),
                (gen_random_uuid(), 'min',   'Minute',    'time',   now(), '__seed__', now(), '__seed__');
        </sql>
        <rollback>
            DELETE FROM catalog__uom WHERE created_by = '__seed__';
        </rollback>
    </changeSet>

</databaseChangeLog>