004-finance-entry-lines.xml 2.44 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-finance v0.30 — Double-entry journal entry lines.

        Each JournalEntry can now have N debit/credit legs, each
        referencing an account from the chart of accounts. The
        balanced-entry invariant (sum of debits = sum of credits)
        is enforced by JournalEntryService, not by a DB constraint,
        because Postgres CHECK constraints can't span multiple rows.

        The parent JournalEntry's `amount` column is retained as a
        denormalized summary for backward compatibility; the lines
        are the source of truth for double-entry accounting.
    -->

    <changeSet id="finance-entry-lines-001" author="vibe_erp">
        <comment>Create finance__journal_entry_line table</comment>
        <sql>
            CREATE TABLE finance__journal_entry_line (
                id                  uuid PRIMARY KEY,
                journal_entry_id    uuid          NOT NULL REFERENCES finance__journal_entry(id) ON DELETE CASCADE,
                line_no             integer       NOT NULL,
                account_code        varchar(32)   NOT NULL,
                debit               numeric(18,4) NOT NULL DEFAULT 0,
                credit              numeric(18,4) NOT NULL DEFAULT 0,
                description         text,
                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 finance__jel_line_no_uk UNIQUE (journal_entry_id, line_no),
                CONSTRAINT finance__jel_nonneg_check CHECK (debit >= 0 AND credit >= 0)
            );
            CREATE INDEX finance__jel_entry_idx ON finance__journal_entry_line (journal_entry_id);
            CREATE INDEX finance__jel_account_idx ON finance__journal_entry_line (account_code);
        </sql>
        <rollback>
            DROP TABLE finance__journal_entry_line;
        </rollback>
    </changeSet>

</databaseChangeLog>