historicization trigger+view for Customer, Membership, SepaMandate, Share and Asset

This commit is contained in:
Michael Hoennig 2019-05-07 10:54:27 +02:00
parent e49323e99f
commit 5337e7a7e8
9 changed files with 556 additions and 3 deletions

View File

@ -0,0 +1,15 @@
<?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 http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.5.xsd">
<!-- the natural key used by the historical view must be unique -->
<changeSet id="20190507100000-1" author="mhoennig">
<!-- must match the GROUP BY clause in membership_history_view -->
<addUniqueConstraint constraintName="ux_membership" tableName="membership"
columnNames="customer_id, member_from_date"/>
</changeSet>
</databaseChangeLog>

View File

@ -7,6 +7,7 @@
<!-- the natural key used by the historical view must be unique --> <!-- the natural key used by the historical view must be unique -->
<changeSet id="201905061856-1" author="mhoennig"> <changeSet id="201905061856-1" author="mhoennig">
<!-- must match the GROUP BY clause in user_role_assignment_history_view -->
<addUniqueConstraint constraintName="ux_user_role_assignment" tableName="user_role_assignment" <addUniqueConstraint constraintName="ux_user_role_assignment" tableName="user_role_assignment"
columnNames="entity_type_id, entity_object_id, assigned_role, user_id"/> columnNames="entity_type_id, entity_object_id, assigned_role, user_id"/>

View File

@ -0,0 +1,96 @@
<?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 Asset data. -->
<changeSet id="20190507193200-1" author="mhierweck,mhoennig" dbms="postgresql">
<createTable tableName="asset_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="document_date" type="date">
<constraints nullable="false" />
</column>
<column name="value_date" type="date">
<constraints nullable="false" />
</column>
<column name="action" type="varchar(255)">
<constraints nullable="false" />
</column>
<column name="amount" type="decimal(10,2)">
<constraints nullable="false" />
</column>
<column name="remark" type="varchar(160)">
<constraints nullable="true" />
</column>
<column name="membership_id" type="bigint">
<constraints nullable="false" />
</column>
</createTable>
</changeSet>
<changeSet id="20190507193200-2" author="mhierweck,mhoennig" dbms="postgresql">
<addForeignKeyConstraint baseColumnNames="history_transaction"
baseTableName="asset_history"
constraintName="fk_asset_history_transaction"
referencedColumnNames="history_transaction"
referencedTableName="history"/>
</changeSet>
<changeSet id="20190507193200-3" author="mhierweck,mhoennig" dbms="postgresql">
<createProcedure>
CREATE TRIGGER asset_historicize
AFTER INSERT OR DELETE OR UPDATE ON asset
FOR EACH ROW EXECUTE PROCEDURE historicize();
</createProcedure>
<rollback>
DROP TRIGGER asset_historicize
</rollback>
</changeSet>
<changeSet id="20190507193200-4" author="mhierweck,mhoennig" dbms="postgresql">
<createView viewName="asset_history_view" replaceIfExists="true">
SELECT *
FROM asset_history
WHERE history_id IN (
SELECT max(history_id) AS history_id
FROM asset_history
WHERE history_transaction &lt;= current_setting('history.transaction')::bigint
GROUP BY id) -- assets are not deletable, thus id is enough
</createView>
</changeSet>
</databaseChangeLog>

View File

@ -0,0 +1,131 @@
<?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>

View File

@ -0,0 +1,95 @@
<?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 Membership data. -->
<changeSet id="20190507100000-1" author="mhierweck,mhoennig" dbms="postgresql">
<createTable tableName="membership_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="admission_document_date" type="date">
<constraints nullable="false" />
</column>
<column name="cancellation_document_date" type="date">
<constraints nullable="true" />
</column>
<column name="member_from_date" type="date">
<constraints nullable="false" />
</column>
<column name="member_until_date" type="date">
<constraints nullable="true" />
</column>
<column name="remark" type="varchar(160)">
<constraints nullable="true" />
</column>
<column name="customer_id" type="bigint">
<constraints nullable="false" />
</column>
</createTable>
</changeSet>
<changeSet id="20190507100000-2" author="mhierweck,mhoennig" dbms="postgresql">
<addForeignKeyConstraint baseColumnNames="history_transaction"
baseTableName="membership_history"
constraintName="fk_membership_history_transaction"
referencedColumnNames="history_transaction"
referencedTableName="history"/>
</changeSet>
<changeSet id="20190507100000-3" author="mhierweck,mhoennig" dbms="postgresql">
<createProcedure>
CREATE TRIGGER membership_historicize
AFTER INSERT OR DELETE OR UPDATE ON membership
FOR EACH ROW EXECUTE PROCEDURE historicize();
</createProcedure>
<rollback>
DROP TRIGGER membership_historicize
</rollback>
</changeSet>
<changeSet id="20190507100000-4" author="mhierweck,mhoennig" dbms="postgresql">
<createView viewName="membership_history_view" replaceIfExists="true">
SELECT *
FROM membership_history
WHERE history_id IN (
SELECT max(history_id) AS history_id
FROM membership_history
WHERE history_transaction &lt;= current_setting('history.transaction')::bigint
GROUP BY customer_id, member_from_date) -- must have a unique constraint
</createView>
</changeSet>
</databaseChangeLog>

View File

@ -0,0 +1,112 @@
<?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 SepaMandate data. -->
<changeSet id="20190507101800-1" author="mhierweck,mhoennig" dbms="postgresql">
<createTable tableName="sepa_mandate_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="varchar(40)">
<constraints nullable="false" />
</column>
<column name="iban" type="varchar(34)">
<constraints nullable="true" />
</column>
<column name="bic" type="varchar(11)">
<constraints nullable="true" />
</column>
<column name="granting_document_date" type="date">
<constraints nullable="false" />
</column>
<column name="revokation_document_date" type="date">
<constraints nullable="true" />
</column>
<column name="valid_from_date" type="date">
<constraints nullable="false" />
</column>
<column name="valid_until_date" type="date">
<constraints nullable="true" />
</column>
<column name="last_used_date" type="date">
<constraints nullable="true" />
</column>
<column name="remark" type="varchar(160)">
<constraints nullable="true" />
</column>
<column name="customer_id" type="bigint">
<constraints nullable="false" />
</column>
</createTable>
</changeSet>
<changeSet id="20190507101800-2" author="mhierweck,mhoennig" dbms="postgresql">
<addForeignKeyConstraint baseColumnNames="history_transaction"
baseTableName="sepa_mandate_history"
constraintName="fk_sepa_mandate_history_transaction"
referencedColumnNames="history_transaction"
referencedTableName="history"/>
</changeSet>
<changeSet id="20190507101800-3" author="mhierweck,mhoennig" dbms="postgresql">
<createProcedure>
CREATE TRIGGER sepa_mandate_historicize
AFTER INSERT OR DELETE OR UPDATE ON sepa_mandate
FOR EACH ROW EXECUTE PROCEDURE historicize();
</createProcedure>
<rollback>
DROP TRIGGER sepa_mandate_historicize
</rollback>
</changeSet>
<changeSet id="20190507101800-4" author="mhierweck,mhoennig" dbms="postgresql">
<createView viewName="sepa_mandate_history_view" replaceIfExists="true">
SELECT *
FROM sepa_mandate_history
WHERE history_id IN (
SELECT max(history_id) AS history_id
FROM sepa_mandate_history
WHERE history_transaction &lt;= current_setting('history.transaction')::bigint
GROUP BY reference) -- must have a unique constraint
</createView>
</changeSet>
</databaseChangeLog>

View File

@ -0,0 +1,96 @@
<?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 Share data. -->
<changeSet id="20190507193400-1" author="mhierweck,mhoennig" dbms="postgresql">
<createTable tableName="share_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="document_date" type="date">
<constraints nullable="false" />
</column>
<column name="value_date" type="date">
<constraints nullable="false" />
</column>
<column name="action" type="varchar(255)">
<constraints nullable="false" />
</column>
<column name="quantity" type="integer">
<constraints nullable="false" />
</column>
<column name="remark" type="varchar(160)">
<constraints nullable="true" />
</column>
<column name="membership_id" type="bigint">
<constraints nullable="false" />
</column>
</createTable>
</changeSet>
<changeSet id="20190507193400-2" author="mhierweck,mhoennig" dbms="postgresql">
<addForeignKeyConstraint baseColumnNames="history_transaction"
baseTableName="share_history"
constraintName="fk_share_history_transaction"
referencedColumnNames="history_transaction"
referencedTableName="history"/>
</changeSet>
<changeSet id="20190507193400-3" author="mhierweck,mhoennig" dbms="postgresql">
<createProcedure>
CREATE TRIGGER share_historicize
AFTER INSERT OR DELETE OR UPDATE ON share
FOR EACH ROW EXECUTE PROCEDURE historicize();
</createProcedure>
<rollback>
DROP TRIGGER share_historicize
</rollback>
</changeSet>
<changeSet id="20190507193400-4" author="mhierweck,mhoennig" dbms="postgresql">
<createView viewName="share_history_view" replaceIfExists="true">
SELECT *
FROM share_history
WHERE history_id IN (
SELECT max(history_id) AS history_id
FROM share_history
WHERE history_transaction &lt;= current_setting('history.transaction')::bigint
GROUP BY id) -- shares are not deletable, thus id is enough
</createView>
</changeSet>
</databaseChangeLog>

View File

@ -80,7 +80,7 @@
SELECT max(history_id) AS history_id SELECT max(history_id) AS history_id
FROM user_role_assignment_history FROM user_role_assignment_history
WHERE history_transaction &lt;= current_setting('history.transaction')::bigint WHERE history_transaction &lt;= current_setting('history.transaction')::bigint
GROUP BY entity_type_id, entity_object_id, assigned_role, user_id) GROUP BY entity_type_id, entity_object_id, assigned_role, user_id) -- must have a unique constraint
</createView> </createView>
</changeSet> </changeSet>

View File

@ -23,13 +23,20 @@
<!-- jhipster-needle-liquibase-add-constraints-changelog - JHipster will add liquibase constraints changelogs here --> <!-- jhipster-needle-liquibase-add-constraints-changelog - JHipster will add liquibase constraints changelogs here -->
<!-- additional changesets, not generated by JHipster --> <!-- additional changesets, not generated by JHipster -->
<include file="config/liquibase/changelog/20190506185600_constraints_UserRoleAssignment.xml" relativeToChangelogFile="false"/> <include file="config/liquibase/changelog/constraints_UserRoleAssignment.xml" relativeToChangelogFile="false"/>
<include file="config/liquibase/changelog/constraints_Membership.xml" relativeToChangelogFile="false"/>
<!-- historicization --> <!-- historicization -->
<include file="config/liquibase/historicization/historicization.xml" relativeToChangelogFile="false"/> <include file="config/liquibase/historicization/historicization.xml" relativeToChangelogFile="false"/>
<include file="config/liquibase/historicization/historicization_UserRoleAssignment.xml" relativeToChangelogFile="false"/> <include file="config/liquibase/historicization/historicization_UserRoleAssignment.xml" relativeToChangelogFile="false"/>
<include file="config/liquibase/historicization/historicization_Customer.xml" relativeToChangelogFile="false"/>
<include file="config/liquibase/historicization/historicization_Membership.xml" relativeToChangelogFile="false"/>
<include file="config/liquibase/historicization/historicization_SepaMandate.xml" relativeToChangelogFile="false"/>
<include file="config/liquibase/historicization/historicization_Share.xml" relativeToChangelogFile="false"/>
<include file="config/liquibase/historicization/historicization_Asset.xml" relativeToChangelogFile="false"/>
<!-- sample data. comes after historicization triggers, historicization applies --> <!-- sample data. comes after histo
ricization triggers, historicization applies -->
<include file="config/liquibase/sample-data/customers.xml" relativeToChangelogFile="false"/> <include file="config/liquibase/sample-data/customers.xml" relativeToChangelogFile="false"/>
<include file="config/liquibase/sample-data/memberships.xml" relativeToChangelogFile="false"/> <include file="config/liquibase/sample-data/memberships.xml" relativeToChangelogFile="false"/>
<include file="config/liquibase/sample-data/shares.xml" relativeToChangelogFile="false"/> <include file="config/liquibase/sample-data/shares.xml" relativeToChangelogFile="false"/>