From 57b6399950463f00f234438f6bf45d5ba4cb3ddb Mon Sep 17 00:00:00 2001
From: Michael Hoennig <michael@hoennig.de>
Date: Sat, 25 May 2019 16:40:41 +0200
Subject: [PATCH] historicization of JHI_USER_AUTHORITY table

---
 .../historicization_UserAuthority.xml         | 75 +++++++++++++++++++
 .../resources/config/liquibase/master.xml     |  1 +
 2 files changed, 76 insertions(+)
 create mode 100644 src/main/resources/config/liquibase/historicization/historicization_UserAuthority.xml

diff --git a/src/main/resources/config/liquibase/historicization/historicization_UserAuthority.xml b/src/main/resources/config/liquibase/historicization/historicization_UserAuthority.xml
new file mode 100644
index 00000000..fa896dc5
--- /dev/null
+++ b/src/main/resources/config/liquibase/historicization/historicization_UserAuthority.xml
@@ -0,0 +1,75 @@
+<?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 user data. -->
+    <changeSet id="20190526160000-1" author="mhierweck,mhoennig" dbms="postgresql">
+
+        <createTable tableName="jhi_user_authority_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>
+
+            <!-- columns like in base table, dropping unique constraints -->
+
+            <column name="user_id" type="bigint">
+                <constraints nullable="false"/>
+            </column>
+
+            <column name="authority_name" type="varchar(50)">
+                <constraints nullable="false"/>
+            </column>
+
+        </createTable>
+
+    </changeSet>
+
+    <changeSet id="20190526160000-2" author="mhierweck,mhoennig" dbms="postgresql">
+
+        <addForeignKeyConstraint baseColumnNames="history_transaction"
+                                 baseTableName="jhi_user_authority_history"
+                                 constraintName="fk_jhi_user_authority_history_transaction"
+                                 referencedColumnNames="history_transaction"
+                                 referencedTableName="history"/>
+
+    </changeSet>
+
+    <changeSet id="20190526160000-3" author="mhierweck,mhoennig" dbms="postgresql">
+        <createProcedure>
+            CREATE TRIGGER jhi_user_authority_historicize
+            AFTER INSERT OR DELETE OR UPDATE ON jhi_user_authority
+            FOR EACH ROW EXECUTE PROCEDURE historicize();
+        </createProcedure>
+        <rollback>
+            DROP TRIGGER jhi_user_authority_historicize
+        </rollback>
+    </changeSet>
+
+    <changeSet id="20190526160000-4" author="mhierweck,mhoennig" dbms="postgresql">
+        <createView viewName="jhi_user_authority_history_view" replaceIfExists="true">
+            SELECT *
+              FROM jhi_user_authority_history
+             WHERE history_id IN (
+                SELECT max(history_id) AS history_id
+                  FROM jhi_user_authority_history
+                 WHERE history_transaction &lt;= current_setting('history.transaction')::bigint
+                 GROUP BY user_id, authority_name) -- must have a unique constraint
+        </createView>
+    </changeSet>
+
+</databaseChangeLog>
diff --git a/src/main/resources/config/liquibase/master.xml b/src/main/resources/config/liquibase/master.xml
index 9c0f7345..60f9a193 100644
--- a/src/main/resources/config/liquibase/master.xml
+++ b/src/main/resources/config/liquibase/master.xml
@@ -10,6 +10,7 @@
     <!-- historicization for initial JHipster tables -->
     <include file="config/liquibase/historicization/historicization.xml" relativeToChangelogFile="false"/>
     <include file="config/liquibase/historicization/historicization_User.xml" relativeToChangelogFile="false"/>
+    <include file="config/liquibase/historicization/historicization_UserAuthority.xml" relativeToChangelogFile="false"/>
 
     <!-- sample data. comes after historicization triggers, thus historicization applies -->
     <include file="config/liquibase/sample-data/users.xml" relativeToChangelogFile="false"/>