From 85893d3f80cd5e13fb1c4a6a9ea0694e22556401 Mon Sep 17 00:00:00 2001 From: Jens Pelzetter Date: Fri, 12 Jun 2020 13:37:38 +0200 Subject: [PATCH] DB-Migration for lifecycle UUIDs --- .../V7_0_0_24__add_lifecycle_uuid.java | 150 ++++++++++ .../scripts/002_create_ccm_cms_tables.sql | 14 +- .../scripts/002_create_ccm_cms_tables.sql | 14 +- .../002_create_ccm_cms_tables.sql | 266 +++++++++++++++++- 4 files changed, 437 insertions(+), 7 deletions(-) create mode 100644 ccm-cms/src/main/java/db/migrations/org/libreccm/ccm_cms/V7_0_0_24__add_lifecycle_uuid.java diff --git a/ccm-cms/src/main/java/db/migrations/org/libreccm/ccm_cms/V7_0_0_24__add_lifecycle_uuid.java b/ccm-cms/src/main/java/db/migrations/org/libreccm/ccm_cms/V7_0_0_24__add_lifecycle_uuid.java new file mode 100644 index 000000000..fc0925ddb --- /dev/null +++ b/ccm-cms/src/main/java/db/migrations/org/libreccm/ccm_cms/V7_0_0_24__add_lifecycle_uuid.java @@ -0,0 +1,150 @@ +package db.migrations.org.libreccm.ccm_cms; + +import org.flywaydb.core.api.migration.BaseJavaMigration; +import org.flywaydb.core.api.migration.Context; + +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.util.ArrayList; +import java.util.List; +import java.util.UUID; + +/** + * + * @author Jens Pelzetter + */ +public class V7_0_0_24__add_lifecycle_uuid extends BaseJavaMigration { + + @Override + public void migrate(final Context context) throws Exception { + + final Connection connection = context.getConnection(); + + final List lifecycleIds = retrieveLifecycleIds(connection); + final List definitionIds = retrieveDefinitionIds( + connection + ); + + addLifecycleUuidCol(connection); + addDefinitionLifecycleCol(connection); + + setLifecycleUuid(connection, lifecycleIds); + setDefinitionUuid(connection, definitionIds); + + addLifecycleUniqueConstraint(connection); + addDefinitionUniqueConstraint(connection); + + } + + private List retrieveLifecycleIds(final Connection connection) + throws Exception { + final List result = new ArrayList<>(); + try (PreparedStatement stmt = connection + .prepareStatement("select LIFECYCLE_ID from LIFECYCLES"); + ResultSet resultSet = stmt.executeQuery()) { + + while (resultSet.next()) { + result.add(resultSet.getLong("LIFECYCLE_ID")); + } + + } + + return result; + } + + private List retrieveDefinitionIds(final Connection connection) + throws Exception { + final List result = new ArrayList<>(); + try (PreparedStatement stmt = connection + .prepareStatement( + "select LIFECYCLE_DEFINITION_ID " + + "from LIFECYLE_DEFINITIONS" + ); + ResultSet resultSet = stmt.executeQuery()) { + + while (resultSet.next()) { + result.add(resultSet.getLong("LIFECYCLE_ID")); + } + + } + + return result; + } + + private void addLifecycleUuidCol(final Connection connection) + throws Exception { + try (PreparedStatement stmt = connection.prepareStatement( + "alter table LIFECYCLES add column UUID varchar(255)" + )) { + stmt.execute(); + } + } + + private void addDefinitionLifecycleCol(final Connection connection) + throws Exception { + try (PreparedStatement stmt = connection.prepareStatement( + "alter table LIFECYCLE_DEFINITIONS " + + "add column UUID varchar(255)" + )) { + stmt.execute(); + } + } + + private void setLifecycleUuid( + final Connection connection, final List lifecycleIds + ) throws Exception { + try (PreparedStatement stmt = connection.prepareStatement( + "update LIFECYCLES set UUID = ? where LIFECYCLE_ID = ?" + )) { + for (final Long lifecycleId : lifecycleIds) { + stmt.setString(1, UUID.randomUUID().toString()); + stmt.setLong(2, lifecycleId); + stmt.executeUpdate(); + stmt.clearParameters(); + } + } + } + + private void setDefinitionUuid( + final Connection connection, final List definitionIds + ) throws Exception { + try (PreparedStatement stmt = connection.prepareStatement( + "update LIFECYCLE_DEFINITIONS set UUID = ? " + + "where LIFECYCLE_DEFINITION_ID = ?" + )) { + for (final Long lifecycleId : definitionIds) { + stmt.setString(1, UUID.randomUUID().toString()); + stmt.setLong(2, lifecycleId); + stmt.executeUpdate(); + stmt.clearParameters(); + } + } + + } + + private void addLifecycleUniqueConstraint( + final Connection connection + ) throws Exception { + try (PreparedStatement stmt = connection.prepareStatement( + "alter table LIFECYCLES " + + "add constraint UK_40o4njo54m8c4xlwq6ctnsimd " + + "unique (UUID)" + )) { + stmt.execute(); + } + } + + private void addDefinitionUniqueConstraint( + final Connection connection + ) throws Exception { +try (PreparedStatement stmt = connection.prepareStatement( + "alter table LIFECYCLE_DEFINITIONS " + + "add constraint UK_n6ki3s5im2k2nccpocuctqqe3 " + + "unique (UUID)" + )) { + stmt.execute(); + } + } + +} diff --git a/ccm-cms/src/test/resources-wildfly-remote-h2-mem/scripts/002_create_ccm_cms_tables.sql b/ccm-cms/src/test/resources-wildfly-remote-h2-mem/scripts/002_create_ccm_cms_tables.sql index 0504ce2c1..7bebb982b 100644 --- a/ccm-cms/src/test/resources-wildfly-remote-h2-mem/scripts/002_create_ccm_cms_tables.sql +++ b/ccm-cms/src/test/resources-wildfly-remote-h2-mem/scripts/002_create_ccm_cms_tables.sql @@ -736,6 +736,7 @@ LISTENER varchar(1024), START_DATE_TIME date, STARTED boolean, + UUID varchar(255), DEFINITION_ID bigint, primary key (LIFECYCLE_ID) ); @@ -743,6 +744,7 @@ create table CCM_CMS.LIFECYLE_DEFINITIONS ( LIFECYCLE_DEFINITION_ID bigint not null, DEFAULT_LISTENER varchar(1024), + UUID varchar(255), primary key (LIFECYCLE_DEFINITION_ID) ); @@ -1040,6 +1042,12 @@ alter table CCM_CMS.CONTENT_SECTION_WORKFLOW_TEMPLATES add constraint UK_goj42ghwu4tf1akfb2r6ensns unique (WORKFLOW_TEMPLATE_ID); + alter table CCM_CMS.LIFECYCLES + add constraint UK_40o4njo54m8c4xlwq6ctnsimd unique (UUID); + + alter table CCM_CMS.LIFECYLE_DEFINITIONS + add constraint UK_n6ki3s5im2k2nccpocuctqqe3 unique (UUID); + create table CCM_CORE.APPLICATIONS ( APPLICATION_TYPE varchar(1024) not null, PRIMARY_URL varchar(1024) not null, @@ -1584,11 +1592,11 @@ SETTING_ID bigint not null, CONFIGURATION_CLASS varchar(512) not null, NAME varchar(512) not null, - SETTING_VALUE_LONG bigint, SETTING_VALUE_BIG_DECIMAL decimal(19,2), - SETTING_VALUE_DOUBLE double, - SETTING_VALUE_STRING varchar(1024), SETTING_VALUE_BOOLEAN boolean, + SETTING_VALUE_LONG bigint, + SETTING_VALUE_STRING varchar(1024), + SETTING_VALUE_DOUBLE double, primary key (SETTING_ID) ); diff --git a/ccm-cms/src/test/resources-wildfly-remote-pgsql/scripts/002_create_ccm_cms_tables.sql b/ccm-cms/src/test/resources-wildfly-remote-pgsql/scripts/002_create_ccm_cms_tables.sql index 615bbcd45..cb0531a2e 100644 --- a/ccm-cms/src/test/resources-wildfly-remote-pgsql/scripts/002_create_ccm_cms_tables.sql +++ b/ccm-cms/src/test/resources-wildfly-remote-pgsql/scripts/002_create_ccm_cms_tables.sql @@ -736,6 +736,7 @@ LISTENER varchar(1024), START_DATE_TIME date, STARTED boolean, + UUID varchar(255), DEFINITION_ID int8, primary key (LIFECYCLE_ID) ); @@ -743,6 +744,7 @@ create table CCM_CMS.LIFECYLE_DEFINITIONS ( LIFECYCLE_DEFINITION_ID int8 not null, DEFAULT_LISTENER varchar(1024), + UUID varchar(255), primary key (LIFECYCLE_DEFINITION_ID) ); @@ -1040,6 +1042,12 @@ alter table CCM_CMS.CONTENT_SECTION_WORKFLOW_TEMPLATES add constraint UK_goj42ghwu4tf1akfb2r6ensns unique (WORKFLOW_TEMPLATE_ID); + alter table CCM_CMS.LIFECYCLES + add constraint UK_40o4njo54m8c4xlwq6ctnsimd unique (UUID); + + alter table CCM_CMS.LIFECYLE_DEFINITIONS + add constraint UK_n6ki3s5im2k2nccpocuctqqe3 unique (UUID); + create table CCM_CORE.APPLICATIONS ( APPLICATION_TYPE varchar(1024) not null, PRIMARY_URL varchar(1024) not null, @@ -1584,11 +1592,11 @@ SETTING_ID int8 not null, CONFIGURATION_CLASS varchar(512) not null, NAME varchar(512) not null, - SETTING_VALUE_LONG int8, SETTING_VALUE_BIG_DECIMAL numeric(19, 2), - SETTING_VALUE_DOUBLE float8, - SETTING_VALUE_STRING varchar(1024), SETTING_VALUE_BOOLEAN boolean, + SETTING_VALUE_LONG int8, + SETTING_VALUE_STRING varchar(1024), + SETTING_VALUE_DOUBLE float8, primary key (SETTING_ID) ); diff --git a/ccm-cms/src/test/resources/scripts/datasets-test/002_create_ccm_cms_tables.sql b/ccm-cms/src/test/resources/scripts/datasets-test/002_create_ccm_cms_tables.sql index 38fa156d6..7bebb982b 100644 --- a/ccm-cms/src/test/resources/scripts/datasets-test/002_create_ccm_cms_tables.sql +++ b/ccm-cms/src/test/resources/scripts/datasets-test/002_create_ccm_cms_tables.sql @@ -221,6 +221,80 @@ primary key (COMPONENT_MODEL_ID) ); + create table CCM_CMS.CONTACT_ENTRIES ( + CONTACT_ENTRY_ID bigint not null, + ENTRY_ORDER bigint, + ENTRY_VALUE varchar(4096), + CONTACT_ENTRY_KEY_ID bigint, + CONTACTABLE_ID bigint, + primary key (CONTACT_ENTRY_ID) + ); + + create table CCM_CMS.CONTACT_ENTRIES_AUD ( + CONTACT_ENTRY_ID bigint not null, + REV integer not null, + REVTYPE tinyint, + REVEND integer, + ENTRY_ORDER bigint, + ENTRY_VALUE varchar(4096), + CONTACT_ENTRY_KEY_ID bigint, + primary key (CONTACT_ENTRY_ID, REV) + ); + + create table CCM_CMS.CONTACT_ENTRY_KEY_LABELS ( + KEY_ID bigint not null, + LOCALIZED_VALUE varchar(2147483647), + LOCALE varchar(255) not null, + primary key (KEY_ID, LOCALE) + ); + + create table CCM_CMS.CONTACT_ENTRY_KEY_LABELS_AUD ( + REV integer not null, + KEY_ID bigint not null, + LOCALIZED_VALUE varchar(2147483647) not null, + LOCALE varchar(255) not null, + REVTYPE tinyint, + REVEND integer, + primary key (REV, KEY_ID, LOCALIZED_VALUE, LOCALE) + ); + + create table CCM_CMS.CONTACT_ENTRY_KEYS ( + KEY_ID bigint not null, + ENTRY_KEY varchar(255), + primary key (KEY_ID) + ); + + create table CCM_CMS.CONTACT_ENTRY_KEYS_AUD ( + KEY_ID bigint not null, + REV integer not null, + REVTYPE tinyint, + REVEND integer, + ENTRY_KEY varchar(255), + primary key (KEY_ID, REV) + ); + + create table CCM_CMS.CONTACTABLE_ENTITIES ( + OBJECT_ID bigint not null, + POSTAL_ADDRESS_ID bigint, + primary key (OBJECT_ID) + ); + + create table CCM_CMS.CONTACTABLE_ENTITIES_AUD ( + OBJECT_ID bigint not null, + REV integer not null, + POSTAL_ADDRESS_ID bigint, + primary key (OBJECT_ID, REV) + ); + + create table CCM_CMS.ContactableEntity_ContactEntry_AUD ( + REV integer not null, + CONTACTABLE_ID bigint not null, + CONTACT_ENTRY_ID bigint not null, + REVTYPE tinyint, + REVEND integer, + primary key (REV, CONTACTABLE_ID, CONTACT_ENTRY_ID) + ); + create table CCM_CMS.CONTENT_ITEM_COMPONENTS ( MODE varchar(255), COMPONENT_MODEL_ID bigint not null, @@ -662,6 +736,7 @@ LISTENER varchar(1024), START_DATE_TIME date, STARTED boolean, + UUID varchar(255), DEFINITION_ID bigint, primary key (LIFECYCLE_ID) ); @@ -669,6 +744,7 @@ create table CCM_CMS.LIFECYLE_DEFINITIONS ( LIFECYCLE_DEFINITION_ID bigint not null, DEFAULT_LISTENER varchar(1024), + UUID varchar(255), primary key (LIFECYCLE_DEFINITION_ID) ); @@ -805,6 +881,19 @@ primary key (REV, OBJECT_ID, LOCALIZED_VALUE, LOCALE) ); + create table CCM_CMS.ORGANIZATIONS ( + NAME varchar(1024), + OBJECT_ID bigint not null, + primary key (OBJECT_ID) + ); + + create table CCM_CMS.ORGANIZATIONS_AUD ( + OBJECT_ID bigint not null, + REV integer not null, + NAME varchar(1024), + primary key (OBJECT_ID, REV) + ); + create table CCM_CMS.PAGE_THEME_CONFIGURATIONS ( PAGE_ID bigint not null, INDEX_PAGE_TEMPLATE varchar(255), @@ -827,6 +916,60 @@ primary key (OBJECT_ID) ); + create table CCM_CMS.PERSON_NAMES ( + PERSON_ID bigint not null, + GIVEN_NAME varchar(255), + NAME_PREFIX varchar(255), + SUFFIX varchar(255), + SURNAME varchar(255) + ); + + create table CCM_CMS.PERSON_NAMES_AUD ( + REV integer not null, + REVTYPE tinyint not null, + PERSON_ID bigint not null, + REVEND integer, + SURNAME varchar(255), + NAME_PREFIX varchar(255), + GIVEN_NAME varchar(255), + SUFFIX varchar(255), + primary key (REV, REVTYPE, PERSON_ID) + ); + + create table CCM_CMS.PERSONS ( + BIRTHDATE date, + OBJECT_ID bigint not null, + primary key (OBJECT_ID) + ); + + create table CCM_CMS.PERSONS_AUD ( + OBJECT_ID bigint not null, + REV integer not null, + BIRTHDATE date, + primary key (OBJECT_ID, REV) + ); + + create table CCM_CMS.POSTAL_ADDRESSES ( + ADDRESS varchar(2048), + CITY varchar(512), + ISO_COUNTRY_CODE varchar(10), + POSTAL_CODE varchar(255), + ADDRESS_STATE varchar(255), + OBJECT_ID bigint not null, + primary key (OBJECT_ID) + ); + + create table CCM_CMS.POSTAL_ADDRESSES_AUD ( + OBJECT_ID bigint not null, + REV integer not null, + ADDRESS varchar(2048), + CITY varchar(512), + ISO_COUNTRY_CODE varchar(10), + POSTAL_CODE varchar(255), + ADDRESS_STATE varchar(255), + primary key (OBJECT_ID, REV) + ); + create table CCM_CMS.RELATED_LINKS ( OBJECT_ID bigint not null, BOOKMARK_ID bigint, @@ -899,6 +1042,12 @@ alter table CCM_CMS.CONTENT_SECTION_WORKFLOW_TEMPLATES add constraint UK_goj42ghwu4tf1akfb2r6ensns unique (WORKFLOW_TEMPLATE_ID); + alter table CCM_CMS.LIFECYCLES + add constraint UK_40o4njo54m8c4xlwq6ctnsimd unique (UUID); + + alter table CCM_CMS.LIFECYLE_DEFINITIONS + add constraint UK_n6ki3s5im2k2nccpocuctqqe3 unique (UUID); + create table CCM_CORE.APPLICATIONS ( APPLICATION_TYPE varchar(1024) not null, PRIMARY_URL varchar(1024) not null, @@ -1444,10 +1593,10 @@ CONFIGURATION_CLASS varchar(512) not null, NAME varchar(512) not null, SETTING_VALUE_BIG_DECIMAL decimal(19,2), - SETTING_VALUE_DOUBLE double, SETTING_VALUE_BOOLEAN boolean, SETTING_VALUE_LONG bigint, SETTING_VALUE_STRING varchar(1024), + SETTING_VALUE_DOUBLE double, primary key (SETTING_ID) ); @@ -1917,6 +2066,76 @@ create sequence hibernate_sequence start with 1 increment by 1; foreign key (COMPONENT_MODEL_ID) references CCM_CORE.PAGE_MODEL_COMPONENT_MODELS; + alter table CCM_CMS.CONTACT_ENTRIES + add constraint FKirtfj8sm4y5myworl5hvs1l78 + foreign key (CONTACT_ENTRY_KEY_ID) + references CCM_CMS.CONTACT_ENTRY_KEYS; + + alter table CCM_CMS.CONTACT_ENTRIES + add constraint FKljrrfco44damal9eaqrnfam0m + foreign key (CONTACTABLE_ID) + references CCM_CMS.CONTACTABLE_ENTITIES; + + alter table CCM_CMS.CONTACT_ENTRIES_AUD + add constraint FKib8xp3ab8kdkc0six36f99e2g + foreign key (REV) + references CCM_CORE.CCM_REVISIONS; + + alter table CCM_CMS.CONTACT_ENTRIES_AUD + add constraint FKrse7ibjqsfnny5t1b2tqqs3pt + foreign key (REVEND) + references CCM_CORE.CCM_REVISIONS; + + alter table CCM_CMS.CONTACT_ENTRY_KEY_LABELS + add constraint FK243nk3buqm0pskkr5ifjqfxn5 + foreign key (KEY_ID) + references CCM_CMS.CONTACT_ENTRY_KEYS; + + alter table CCM_CMS.CONTACT_ENTRY_KEY_LABELS_AUD + add constraint FK6n995k5gao6v63gfcga3yaxcw + foreign key (REV) + references CCM_CORE.CCM_REVISIONS; + + alter table CCM_CMS.CONTACT_ENTRY_KEY_LABELS_AUD + add constraint FKdr8ujdpn1ej8l6omlxq8bsxbd + foreign key (REVEND) + references CCM_CORE.CCM_REVISIONS; + + alter table CCM_CMS.CONTACT_ENTRY_KEYS_AUD + add constraint FKcvn2b1h1d4uvvmtbf4qf81l0y + foreign key (REV) + references CCM_CORE.CCM_REVISIONS; + + alter table CCM_CMS.CONTACT_ENTRY_KEYS_AUD + add constraint FKkyy4v3tax8w5htnpkmmt8aec1 + foreign key (REVEND) + references CCM_CORE.CCM_REVISIONS; + + alter table CCM_CMS.CONTACTABLE_ENTITIES + add constraint FKqefwowr9adclj3xvpfje9rddr + foreign key (POSTAL_ADDRESS_ID) + references CCM_CMS.POSTAL_ADDRESSES; + + alter table CCM_CMS.CONTACTABLE_ENTITIES + add constraint FKhdwlhf3jp8wf5wxjkoynrcspj + foreign key (OBJECT_ID) + references CCM_CMS.ASSETS; + + alter table CCM_CMS.CONTACTABLE_ENTITIES_AUD + add constraint FKjx8trfvt96fkdn6bafnh839id + foreign key (OBJECT_ID, REV) + references CCM_CMS.ASSETS_AUD; + + alter table CCM_CMS.ContactableEntity_ContactEntry_AUD + add constraint FKs5tfdp1auj9ocgvfa9ivec517 + foreign key (REV) + references CCM_CORE.CCM_REVISIONS; + + alter table CCM_CMS.ContactableEntity_ContactEntry_AUD + add constraint FKskn2ovg24tnnnwd2o8y0biyje + foreign key (REVEND) + references CCM_CORE.CCM_REVISIONS; + alter table CCM_CMS.CONTENT_ITEM_COMPONENTS add constraint FKp83o82kxo2ipa0xo03wxp4dcr foreign key (COMPONENT_MODEL_ID) @@ -2447,6 +2666,16 @@ create sequence hibernate_sequence start with 1 increment by 1; foreign key (REVEND) references CCM_CORE.CCM_REVISIONS; + alter table CCM_CMS.ORGANIZATIONS + add constraint FK77ig0to48xrlfx8qsc0vlfsp6 + foreign key (OBJECT_ID) + references CCM_CMS.CONTACTABLE_ENTITIES; + + alter table CCM_CMS.ORGANIZATIONS_AUD + add constraint FKp0k3bf008pih96sguio80siql + foreign key (OBJECT_ID, REV) + references CCM_CMS.CONTACTABLE_ENTITIES_AUD; + alter table CCM_CMS.PAGE_THEME_CONFIGURATIONS add constraint FK6l6xp6ex6sh2uuxfmeekf6ckn foreign key (PAGE_ID) @@ -2477,6 +2706,41 @@ create sequence hibernate_sequence start with 1 increment by 1; foreign key (OBJECT_ID) references CCM_CORE.SITE_AWARE_APPLICATIONS; + alter table CCM_CMS.PERSON_NAMES + add constraint FK2yluyhmpuhwxafcbna6u8txrt + foreign key (PERSON_ID) + references CCM_CMS.PERSONS; + + alter table CCM_CMS.PERSON_NAMES_AUD + add constraint FKtqtlwx8pa9ydh009sudtpfxie + foreign key (REV) + references CCM_CORE.CCM_REVISIONS; + + alter table CCM_CMS.PERSON_NAMES_AUD + add constraint FKs6m8tgbp8agrd5q3klwbtcujg + foreign key (REVEND) + references CCM_CORE.CCM_REVISIONS; + + alter table CCM_CMS.PERSONS + add constraint FKiv4ydysjekfx64pkb5v4vd9yj + foreign key (OBJECT_ID) + references CCM_CMS.CONTACTABLE_ENTITIES; + + alter table CCM_CMS.PERSONS_AUD + add constraint FKpup1q3295qkuovaptq8aj5lxp + foreign key (OBJECT_ID, REV) + references CCM_CMS.CONTACTABLE_ENTITIES_AUD; + + alter table CCM_CMS.POSTAL_ADDRESSES + add constraint FK4vajjjjo8ro0wns58t8f3i782 + foreign key (OBJECT_ID) + references CCM_CMS.ASSETS; + + alter table CCM_CMS.POSTAL_ADDRESSES_AUD + add constraint FKcrxgaot6kcp9rbxlg8gpp4grg + foreign key (OBJECT_ID, REV) + references CCM_CMS.ASSETS_AUD; + alter table CCM_CMS.RELATED_LINKS add constraint FKb517dnfj56oby2s34jp1omuim foreign key (BOOKMARK_ID)