general historicization table+function for Postgres

This commit is contained in:
Michael Hoennig 2019-05-06 18:42:11 +02:00
parent 295e45e8b8
commit f3c207528e
2 changed files with 54 additions and 1 deletions

View File

@ -0,0 +1,46 @@
<?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">
<changeSet id="20190506000000-1" author="mhierweck,mhoennig" dbms="postgresql">
<createTable tableName="history">
<column name="history_transaction" type="bigint">
<constraints primaryKey="true" nullable="false"/>
</column>
<column name="history_timestamp" type="timestamp">
<constraints nullable="false" />
</column>
</createTable>
</changeSet>
<changeSet id="20190506000000-2" author="mhierweck,mhoennig" dbms="postgresql">
<createProcedure>
CREATE OR REPLACE FUNCTION historicize()
RETURNS trigger
AS $$
BEGIN
IF (TG_OP = 'INSERT') OR (TG_OP = 'UPDATE') THEN
EXECUTE 'INSERT INTO history VALUES (txid_current(), now()) ON CONFLICT DO NOTHING';
EXECUTE format('INSERT INTO %I_history VALUES (DEFAULT, txid_current(), False, $1.*)', TG_TABLE_NAME) USING NEW;
RETURN NEW;
ELSE -- assuming TG_OP = 'DELETE'
EXECUTE 'INSERT INTO history VALUES (txid_current(), now()) ON CONFLICT DO NOTHING';
EXECUTE format('INSERT INTO %I_history VALUES (DEFAULT, txid_current(), True, $1.*)', TG_TABLE_NAME) USING OLD;
RETURN OLD;
END IF;
END;
$$
LANGUAGE plpgsql;
</createProcedure>
</changeSet>
</databaseChangeLog>

View File

@ -3,6 +3,8 @@
xmlns="http://www.liquibase.org/xml/ns/dbchangelog" xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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"> xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.5.xsd">
<!-- JHipster generated entity table changesets -->
<include file="config/liquibase/changelog/00000000000000_initial_schema.xml" relativeToChangelogFile="false"/> <include file="config/liquibase/changelog/00000000000000_initial_schema.xml" relativeToChangelogFile="false"/>
<include file="config/liquibase/changelog/20190430152136_added_entity_Customer.xml" relativeToChangelogFile="false"/> <include file="config/liquibase/changelog/20190430152136_added_entity_Customer.xml" relativeToChangelogFile="false"/>
<include file="config/liquibase/changelog/20190430152137_added_entity_Membership.xml" relativeToChangelogFile="false"/> <include file="config/liquibase/changelog/20190430152137_added_entity_Membership.xml" relativeToChangelogFile="false"/>
@ -11,6 +13,8 @@
<include file="config/liquibase/changelog/20190430152140_added_entity_SepaMandate.xml" relativeToChangelogFile="false"/> <include file="config/liquibase/changelog/20190430152140_added_entity_SepaMandate.xml" relativeToChangelogFile="false"/>
<include file="config/liquibase/changelog/20190430154711_added_entity_UserRoleAssignment.xml" relativeToChangelogFile="false"/> <include file="config/liquibase/changelog/20190430154711_added_entity_UserRoleAssignment.xml" relativeToChangelogFile="false"/>
<!-- jhipster-needle-liquibase-add-changelog - JHipster will add liquibase changelogs here --> <!-- jhipster-needle-liquibase-add-changelog - JHipster will add liquibase changelogs here -->
<!-- JHipster generated constraints changesets -->
<include file="config/liquibase/changelog/20190430152137_added_entity_constraints_Membership.xml" relativeToChangelogFile="false"/> <include file="config/liquibase/changelog/20190430152137_added_entity_constraints_Membership.xml" relativeToChangelogFile="false"/>
<include file="config/liquibase/changelog/20190430152138_added_entity_constraints_Share.xml" relativeToChangelogFile="false"/> <include file="config/liquibase/changelog/20190430152138_added_entity_constraints_Share.xml" relativeToChangelogFile="false"/>
<include file="config/liquibase/changelog/20190430152139_added_entity_constraints_Asset.xml" relativeToChangelogFile="false"/> <include file="config/liquibase/changelog/20190430152139_added_entity_constraints_Asset.xml" relativeToChangelogFile="false"/>
@ -18,7 +22,10 @@
<include file="config/liquibase/changelog/20190430154711_added_entity_constraints_UserRoleAssignment.xml" relativeToChangelogFile="false"/> <include file="config/liquibase/changelog/20190430154711_added_entity_constraints_UserRoleAssignment.xml" relativeToChangelogFile="false"/>
<!-- 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 -->
<!-- sample data --> <!-- historicization -->
<include file="config/liquibase/historicization/historicization.xml" relativeToChangelogFile="false"/>
<!-- sample data. comes after historicization 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"/>