001-identity-init.xml 4.15 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-identity initial schema.

        Owns: identity__user, identity__role, identity__user_role.

        vibe_erp is single-tenant per instance: one running process serves
        exactly one company against an isolated database. There are no
        tenant_id columns and no Row-Level Security policies on these
        tables — customer isolation happens at the deployment level.

        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="identity-init-001" author="vibe_erp">
        <comment>Create identity__user table</comment>
        <sql>
            CREATE TABLE identity__user (
                id           uuid PRIMARY KEY,
                username     varchar(128) NOT NULL,
                display_name varchar(256) NOT NULL,
                email        varchar(320),
                enabled      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 identity__user_username_uk
                ON identity__user (username);
            CREATE INDEX identity__user_ext_gin
                ON identity__user USING GIN (ext jsonb_path_ops);
        </sql>
        <rollback>
            DROP TABLE identity__user;
        </rollback>
    </changeSet>

    <changeSet id="identity-init-002" author="vibe_erp">
        <comment>Create identity__role table</comment>
        <sql>
            CREATE TABLE identity__role (
                id           uuid PRIMARY KEY,
                code         varchar(64)  NOT NULL,
                name         varchar(256) NOT NULL,
                description  text,
                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 identity__role_code_uk
                ON identity__role (code);
            CREATE INDEX identity__role_ext_gin
                ON identity__role USING GIN (ext jsonb_path_ops);
        </sql>
        <rollback>
            DROP TABLE identity__role;
        </rollback>
    </changeSet>

    <changeSet id="identity-init-003" author="vibe_erp">
        <comment>Create identity__user_role join table</comment>
        <sql>
            CREATE TABLE identity__user_role (
                id           uuid PRIMARY KEY,
                user_id      uuid         NOT NULL REFERENCES identity__user(id) ON DELETE CASCADE,
                role_id      uuid         NOT NULL REFERENCES identity__role(id) ON DELETE CASCADE,
                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 identity__user_role_uk
                ON identity__user_role (user_id, role_id);
        </sql>
        <rollback>
            DROP TABLE identity__user_role;
        </rollback>
    </changeSet>

</databaseChangeLog>