<?xml version="1.0" encoding="utf-8"?>
<databaseChangeLog
    xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
    xmlns:ext="http://www.liquibase.org/xml/ns/dbchangelog-ext"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.5.xsd
                        http://www.liquibase.org/xml/ns/dbchangelog-ext http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-ext.xsd">

    <!-- Table for historical Customer data. -->
    <changeSet id="20190506150000-1" author="mhierweck,mhoennig" dbms="postgresql">
        <createTable tableName="customer_history">

            <!-- history-related columns -->

            <column name="history_id" type="bigint" autoIncrement="true">
                <constraints primaryKey="true" unique="true" nullable="false"/>
            </column>

            <column name="history_transaction" type="bigint">
                <constraints nullable="false" />
            </column>

            <column name="history_tombstone" type="bool">
                <constraints nullable="false" />
            </column>

            <!-- id-column like in base table, but no primary key nor unique -->
            <column name="id" type="bigint">
                <constraints primaryKey="true" nullable="false"/>
            </column>

            <!-- all following columns like in base table, dropping unique constraints -->

            <column name="reference" type="integer">
                <constraints nullable="false"/>
            </column>

            <column name="prefix" type="varchar(3)">
                <constraints nullable="false"/>
            </column>

            <column name="name" type="varchar(80)">
                <constraints nullable="false" />
            </column>

            <column name="kind" type="varchar(255)">
                <constraints nullable="false" />
            </column>

            <column name="birth_date" type="date">
                <constraints nullable="true" />
            </column>

            <column name="birth_place" type="varchar(80)">
                <constraints nullable="true" />
            </column>

            <column name="registration_court" type="varchar(80)">
                <constraints nullable="true" />
            </column>

            <column name="registration_number" type="varchar(80)">
                <constraints nullable="true" />
            </column>

            <column name="vat_region" type="varchar(255)">
                <constraints nullable="false" />
            </column>

            <column name="vat_number" type="varchar(40)">
                <constraints nullable="true" />
            </column>

            <column name="contractual_salutation" type="varchar(80)">
                <constraints nullable="true" />
            </column>

            <column name="contractual_address" type="varchar(400)">
                <constraints nullable="false" />
            </column>

            <column name="billing_salutation" type="varchar(80)">
                <constraints nullable="true" />
            </column>

            <column name="billing_address" type="varchar(400)">
                <constraints nullable="true" />
            </column>

            <column name="remark" type="varchar(160)">
                <constraints nullable="true" />
            </column>

        </createTable>

    </changeSet>

    <changeSet id="20190506150000-2" author="mhierweck,mhoennig" dbms="postgresql">

        <addForeignKeyConstraint baseColumnNames="history_transaction"
                                 baseTableName="customer_history"
                                 constraintName="fk_customer_history_transaction"
                                 referencedColumnNames="history_transaction"
                                 referencedTableName="history"/>

    </changeSet>

    <changeSet id="20190506150000-3" author="mhierweck,mhoennig" dbms="postgresql">
        <createProcedure>
            CREATE TRIGGER customer_historicize
            AFTER INSERT OR DELETE OR UPDATE ON customer
            FOR EACH ROW EXECUTE PROCEDURE historicize();
        </createProcedure>
        <rollback>
            DROP TRIGGER customer_historicize
        </rollback>
    </changeSet>

    <changeSet id="20190506150000-4" author="mhierweck,mhoennig" dbms="postgresql">
        <createView viewName="customer_history_view" replaceIfExists="true">
            SELECT *
              FROM customer_history
             WHERE history_id IN (
                SELECT max(history_id) AS history_id
                  FROM customer_history
                 WHERE history_transaction &lt;= current_setting('history.transaction')::bigint
                 GROUP BY name) -- must have a unique constraint
        </createView>
    </changeSet>

</databaseChangeLog>
