From fbc3ad590ce8d3138794a6841373e622cfe49fee Mon Sep 17 00:00:00 2001 From: jensp Date: Fri, 6 Apr 2018 13:07:54 +0000 Subject: [PATCH] CCM NG: Database migrations for Page Model Containers git-svn-id: https://svn.libreccm.org/ccm/ccm_ng@5365 8810af33-2d31-482b-a856-94f89814c4df --- ..._0_0_23__move_components_to_container.java | 162 ++++++++++++++++++ ...0_23__move_components_to_container_h2.java | 39 +++++ ...3__move_components_to_container_pgsql.java | 39 +++++ .../libreccm/pagemodel/ContainerModel.java | 4 +- .../pagemodel/styles/CssProperty.java | 4 +- .../V7_0_0_22__add_page_model_containers.sql | 103 +++++++++++ .../h2/V7_0_0_24__page_model_cleanup.sql | 7 + .../V7_0_0_22__add_page_model_containers.sql | 102 +++++++++++ .../pgsql/V7_0_0_24__page_model_cleanup.sql | 7 + 9 files changed, 464 insertions(+), 3 deletions(-) create mode 100644 ccm-core/src/main/java/db/migrations/org/libreccm/ccm_core/V7_0_0_23__move_components_to_container.java create mode 100644 ccm-core/src/main/java/db/migrations/org/libreccm/ccm_core/h2/V7_0_0_23__move_components_to_container_h2.java create mode 100644 ccm-core/src/main/java/db/migrations/org/libreccm/ccm_core/pgsql/V7_0_0_23__move_components_to_container_pgsql.java create mode 100644 ccm-core/src/main/resources/db/migrations/org/libreccm/ccm_core/h2/V7_0_0_22__add_page_model_containers.sql create mode 100644 ccm-core/src/main/resources/db/migrations/org/libreccm/ccm_core/h2/V7_0_0_24__page_model_cleanup.sql create mode 100644 ccm-core/src/main/resources/db/migrations/org/libreccm/ccm_core/pgsql/V7_0_0_22__add_page_model_containers.sql create mode 100644 ccm-core/src/main/resources/db/migrations/org/libreccm/ccm_core/pgsql/V7_0_0_24__page_model_cleanup.sql diff --git a/ccm-core/src/main/java/db/migrations/org/libreccm/ccm_core/V7_0_0_23__move_components_to_container.java b/ccm-core/src/main/java/db/migrations/org/libreccm/ccm_core/V7_0_0_23__move_components_to_container.java new file mode 100644 index 000000000..29cdfbdb5 --- /dev/null +++ b/ccm-core/src/main/java/db/migrations/org/libreccm/ccm_core/V7_0_0_23__move_components_to_container.java @@ -0,0 +1,162 @@ +/* + * Copyright (C) 2018 LibreCCM Foundation. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301 USA + */ +package db.migrations.org.libreccm.ccm_core; + +import org.flywaydb.core.api.migration.jdbc.JdbcMigration; + +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.util.UUID; + +/** + * + * @author Jens Pelzetter + */ +public class V7_0_0_23__move_components_to_container implements JdbcMigration { + + @Override + public void migrate(final Connection connection) throws Exception { + + // Get all draft PageModels from ccm_core.page_models + final PreparedStatement retrievePageModels = connection + .prepareStatement("select PAGE_MODEL_ID, MODEL_UUID " + + "from CCM_CORE.PAGE_MODELS " + + "where VERSION = 'DRAFT'"); + + + /* + For each PageModel: + * Create a single container (by inserting the data into + ccm_core.page_model_container_models) + * set container_id of each component_model of the page_model + to the ID of the new container + * if the PageModel has a public version do the same but reuse the + containerUuid + */ + final PreparedStatement createContainerId = connection + .prepareStatement("select nextval('hibernate_sequence')"); + final PreparedStatement createContainer = connection + .prepareStatement( + "insert into CCM_CORE.PAGE_MODEL_CONTAINER_MODELS (" + + " CONTAINER_ID, " + + " CONTAINER_UUID, " + + " CONTAINER_KEY, " + + " UUID, " + + " PAGE_MODEL_ID" + + ") " + + "VALUES (" + + " ?," + + " ?," + + " ?," + + " ?," + + " ?" + + ")"); + final PreparedStatement checkForLivePageModel = connection + .prepareStatement("select count(PAGE_MODEL_ID) " + + "from CCM_CORE.PAGE_MODELS " + + "where VERSION = 'LIVE' " + + "and model_uuid = ?"); + final PreparedStatement retrieveLivePage = connection + .prepareStatement("select PAGE_MODEL_ID " + + "from CCM_CORE.PAGE_MODELS " + + "where VERSION = 'LIVE' " + + "and model_uuid = ?"); + final PreparedStatement updateComponents = connection + .prepareStatement("update PAGE_MODEL_COMPONENT_MODELS " + + "set CONTAINER_ID = ? " + + "where COMPONENT_MODEL_ID in (" + + " select COMPONENT_MODEL_ID " + + " from PAGE_MODEL_COMPONENT_MODELS " + + " where PAGE_MODEL_ID = ?" + + ")"); + try (final ResultSet pageModelsResultSet + = retrievePageModels.executeQuery()) { + + while (pageModelsResultSet.next()) { + + final long pageModelId = pageModelsResultSet + .getLong("PAGE_MODEL_ID"); + final String modelUuid = pageModelsResultSet + .getString("MODEL_UUID"); + + final String containerKey = "container"; + final String containerUuid = UUID.randomUUID().toString(); + + final long containerId; + try (final ResultSet containerIdResultSet + = createContainerId.executeQuery()) { + + containerIdResultSet.next(); + containerId = containerIdResultSet.getLong("nextval"); + } + + createContainer.setLong(1, containerId); + createContainer.setString(2, containerUuid); + createContainer.setString(3, containerKey); + createContainer.setString(4, containerUuid); + createContainer.setLong(5, pageModelId); + createContainer.executeUpdate(); + + updateComponents.setLong(1, containerId); + updateComponents.setLong(2, pageModelId); + updateComponents.executeUpdate(); + + checkForLivePageModel.setString(1, modelUuid); + final long liveCount; + try (final ResultSet liveCountResultSet + = checkForLivePageModel.executeQuery()) { + + liveCountResultSet.next(); + liveCount = liveCountResultSet.getLong("COUNT"); + } + if (liveCount > 0) { + + retrieveLivePage.setString(1, modelUuid); + final long livePageModelId; + try (final ResultSet liveResultSet + = retrieveLivePage.executeQuery()) { + liveResultSet.next(); + livePageModelId = liveResultSet.getLong("PAGE_MODEL_ID"); + } + + final long liveContainerId; + try (final ResultSet liveContainerIdResultSet + = createContainerId.executeQuery()) { + liveContainerIdResultSet.next(); + liveContainerId = liveContainerIdResultSet + .getLong("nextval"); + } + + createContainer.setLong(1, liveContainerId); + createContainer.setString(2, containerUuid); + createContainer.setString(3, containerKey); + createContainer.setString(4, UUID.randomUUID().toString()); + createContainer.setLong(5, livePageModelId); + createContainer.executeUpdate(); + + updateComponents.setLong(1, liveContainerId); + updateComponents.setLong(2, livePageModelId); + updateComponents.executeUpdate(); + } + } + } + } + +} diff --git a/ccm-core/src/main/java/db/migrations/org/libreccm/ccm_core/h2/V7_0_0_23__move_components_to_container_h2.java b/ccm-core/src/main/java/db/migrations/org/libreccm/ccm_core/h2/V7_0_0_23__move_components_to_container_h2.java new file mode 100644 index 000000000..a7b90ba4f --- /dev/null +++ b/ccm-core/src/main/java/db/migrations/org/libreccm/ccm_core/h2/V7_0_0_23__move_components_to_container_h2.java @@ -0,0 +1,39 @@ +/* + * Copyright (C) 2018 LibreCCM Foundation. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301 USA + */ +package db.migrations.org.libreccm.ccm_core.h2; + +import db.migrations.org.libreccm.ccm_core.V7_0_0_23__move_components_to_container; +import org.flywaydb.core.api.migration.jdbc.JdbcMigration; + +import java.sql.Connection; + +/** + * + * @author Jens Pelzetter + */ +public class V7_0_0_23__move_components_to_container_h2 + extends V7_0_0_23__move_components_to_container + implements JdbcMigration { + + @Override + public void migrate(final Connection connection) throws Exception { + super.migrate(connection); + } + +} diff --git a/ccm-core/src/main/java/db/migrations/org/libreccm/ccm_core/pgsql/V7_0_0_23__move_components_to_container_pgsql.java b/ccm-core/src/main/java/db/migrations/org/libreccm/ccm_core/pgsql/V7_0_0_23__move_components_to_container_pgsql.java new file mode 100644 index 000000000..4daa36bd5 --- /dev/null +++ b/ccm-core/src/main/java/db/migrations/org/libreccm/ccm_core/pgsql/V7_0_0_23__move_components_to_container_pgsql.java @@ -0,0 +1,39 @@ +/* + * Copyright (C) 2018 LibreCCM Foundation. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301 USA + */ +package db.migrations.org.libreccm.ccm_core.pgsql; + +import db.migrations.org.libreccm.ccm_core.V7_0_0_23__move_components_to_container; +import org.flywaydb.core.api.migration.jdbc.JdbcMigration; + +import java.sql.Connection; + +/** + * + * @author Jens Pelzetter + */ +public class V7_0_0_23__move_components_to_container_pgsql + extends V7_0_0_23__move_components_to_container + implements JdbcMigration { + + @Override + public void migrate(final Connection connection) throws Exception { + super.migrate(connection); + } + +} diff --git a/ccm-core/src/main/java/org/libreccm/pagemodel/ContainerModel.java b/ccm-core/src/main/java/org/libreccm/pagemodel/ContainerModel.java index f340223b6..9dbbd2776 100644 --- a/ccm-core/src/main/java/org/libreccm/pagemodel/ContainerModel.java +++ b/ccm-core/src/main/java/org/libreccm/pagemodel/ContainerModel.java @@ -55,7 +55,7 @@ import javax.validation.constraints.NotNull; @Table(name = "PAGE_MODEL_CONTAINER_MODELS", schema = CoreConstants.DB_SCHEMA) @NamedQueries({ @NamedQuery(name = "ContainerModel.findByKeyAndPage", - query = "SELECT c FROM Container c " + query = "SELECT c FROM ContainerModel c " + "WHERE c.key = :key " + "AND c.pageModel = :pageModel") }) @@ -89,7 +89,7 @@ public class ContainerModel implements Serializable { * used for the value of the {@code id} or {@code class} attribute in HTML. * It is recommended the use semantic names. */ - @Column(name = "COMPONENT_KEY", length = 255) + @Column(name = "CONTAINER_KEY", length = 255) private String key; /** diff --git a/ccm-core/src/main/java/org/libreccm/pagemodel/styles/CssProperty.java b/ccm-core/src/main/java/org/libreccm/pagemodel/styles/CssProperty.java index 8f07f4d72..d4d0bfe16 100644 --- a/ccm-core/src/main/java/org/libreccm/pagemodel/styles/CssProperty.java +++ b/ccm-core/src/main/java/org/libreccm/pagemodel/styles/CssProperty.java @@ -18,6 +18,8 @@ */ package org.libreccm.pagemodel.styles; +import org.libreccm.core.CoreConstants; + import java.io.Serializable; import java.util.Objects; @@ -34,7 +36,7 @@ import javax.persistence.Table; * @author Jens Pelzetter */ @Entity -@Table(name = "STYLE_PROPERTIES") +@Table(name = "STYLE_PROPERTIES", schema = CoreConstants.DB_SCHEMA) public class CssProperty implements Serializable { private static final long serialVersionUID = -4697757123207731769L; diff --git a/ccm-core/src/main/resources/db/migrations/org/libreccm/ccm_core/h2/V7_0_0_22__add_page_model_containers.sql b/ccm-core/src/main/resources/db/migrations/org/libreccm/ccm_core/h2/V7_0_0_22__add_page_model_containers.sql new file mode 100644 index 000000000..5ac1dc4bd --- /dev/null +++ b/ccm-core/src/main/resources/db/migrations/org/libreccm/ccm_core/h2/V7_0_0_22__add_page_model_containers.sql @@ -0,0 +1,103 @@ +-- Remove obsolete tables + +alter table CCM_CORE.FLEX_LAYOUT_BOXES + drop constraint FKeiqh69t1lr7u09hjuxfyxsbs; + +alter table CCM_CORE.FLEX_LAYOUT_BOXES + drop constraint FKmrobhhqidcf1657ugcgatrd0y; + +alter table CCM_CORE.FLEX_LAYOUT_COMPONENTS + drop constraint FK8qxnqt75ikxtedx0xreoeiygg; + +drop table CCM_CORE.FLEX_LAYOUT_COMPONENTS; + +drop table CCM_CORE.FLEX_LAYOUT_BOXES; + +-- Create new tables and columns + +alter table CCM_CORE.PAGE_MODEL_COMPONENT_MODELS + add column CONTAINER_ID bigint; + +create table CCM_CORE.PAGE_MODEL_CONTAINER_MODELS ( + CONTAINER_ID bigint not null, + CONTAINER_UUID varchar(255) not null, + CONTAINER_KEY varchar(255), + UUID varchar(255) not null, + PAGE_MODEL_ID bigint, + STYLE_ID bigint, + primary key (CONTAINER_ID) +); + +create table CCM_CORE.STYLE_MEDIA_QUERIES ( + MEDIA_QUERY_ID bigint not null, + MAX_WIDTH_UNIT varchar(255), + MAX_WIDTH_VALUE float, + MEDIA_TYPE varchar(255), + MIN_WIDTH_UNIT varchar(255), + MIN_WIDTH_VALUE float, + primary key (MEDIA_QUERY_ID) +); + +create table CCM_CORE.STYLE_MEDIA_RULES ( + MEDIA_RULE_ID bigint not null, + MEDIA_QUERY_ID bigint, + STYLE_ID bigint, + primary key (MEDIA_RULE_ID) +); + +create table CCM_CORE.STYLE_PROPERTIES ( + PROPERTY_ID bigint not null, + NAME varchar(256), + PROPERTY_VALUE varchar(4096), + RULE_ID bigint, + primary key (PROPERTY_ID) +); + +create table CCM_CORE.STYLE_RULES ( + RULE_ID bigint not null, + SELECTOR varchar(2048), + STYLE_ID bigint, + primary key (RULE_ID) +); + +create table CCM_CORE.STYLES ( + STYLE_ID bigint not null, + STYLENAME varchar(255), + primary key (STYLE_ID) +); + +alter table CCM_CORE.PAGE_MODEL_COMPONENT_MODELS + add constraint FK1uvkayybawff8sqkmerqt60bk + foreign key (CONTAINER_ID) + references CCM_CORE.PAGE_MODEL_CONTAINER_MODELS; + +alter table CCM_CORE.PAGE_MODEL_CONTAINER_MODELS + add constraint FK1c6drneacxveol92vpum79fxb + foreign key (PAGE_MODEL_ID) + references CCM_CORE.PAGE_MODELS; + +alter table CCM_CORE.PAGE_MODEL_CONTAINER_MODELS + add constraint FKoi5wphv3vtwryc19akku28p24 + foreign key (STYLE_ID) + references CCM_CORE.STYLES; + + +alter table CCM_CORE.STYLE_MEDIA_RULES + add constraint FKdq24a4atxp4c1sbqs8g6lpkx0 + foreign key (MEDIA_QUERY_ID) + references CCM_CORE.STYLE_MEDIA_QUERIES; + +alter table CCM_CORE.STYLE_MEDIA_RULES + add constraint FKf67h8q9kkjft9go2xo2572n17 + foreign key (STYLE_ID) + references CCM_CORE.STYLES; + +alter table CCM_CORE.STYLE_PROPERTIES + add constraint FKg2g0n7jmce3vjmula0898yp94 + foreign key (RULE_ID) + references CCM_CORE.STYLE_RULES; + +alter table CCM_CORE.STYLE_RULES + add constraint FKcbr0k93g001jix7i4kncsce1w + foreign key (STYLE_ID) + references CCM_CORE.STYLES; diff --git a/ccm-core/src/main/resources/db/migrations/org/libreccm/ccm_core/h2/V7_0_0_24__page_model_cleanup.sql b/ccm-core/src/main/resources/db/migrations/org/libreccm/ccm_core/h2/V7_0_0_24__page_model_cleanup.sql new file mode 100644 index 000000000..72562b0ef --- /dev/null +++ b/ccm-core/src/main/resources/db/migrations/org/libreccm/ccm_core/h2/V7_0_0_24__page_model_cleanup.sql @@ -0,0 +1,7 @@ +-- drop obsolete columns + +alter table CCM_CORE.PAGE_MODEL_COMPONENT_MODELS + drop constraint FKo696ch035fe7rrueol1po13od + +alter table CCM_CORE.PAGE_MODEL_COMPONENT_MODELS add column PAGE_MODEL_ID bigint; + diff --git a/ccm-core/src/main/resources/db/migrations/org/libreccm/ccm_core/pgsql/V7_0_0_22__add_page_model_containers.sql b/ccm-core/src/main/resources/db/migrations/org/libreccm/ccm_core/pgsql/V7_0_0_22__add_page_model_containers.sql new file mode 100644 index 000000000..e26c67641 --- /dev/null +++ b/ccm-core/src/main/resources/db/migrations/org/libreccm/ccm_core/pgsql/V7_0_0_22__add_page_model_containers.sql @@ -0,0 +1,102 @@ +-- Remove obsolete tables + +alter table CCM_CORE.FLEX_LAYOUT_BOXES + drop constraint FKeiqh69t1lr7u09hjuxfyxsbs; + +alter table CCM_CORE.FLEX_LAYOUT_BOXES + drop constraint FKmrobhhqidcf1657ugcgatrd0y; + +alter table CCM_CORE.FLEX_LAYOUT_COMPONENTS + drop constraint FK8qxnqt75ikxtedx0xreoeiygg; + +drop table CCM_CORE.FLEX_LAYOUT_COMPONENTS; + +drop table CCM_CORE.FLEX_LAYOUT_BOXES; + +-- Create new tables and columns + +alter table CCM_CORE.PAGE_MODEL_COMPONENT_MODELS + add column CONTAINER_ID int8; + +create table CCM_CORE.PAGE_MODEL_CONTAINER_MODELS ( + CONTAINER_ID int8 not null, + CONTAINER_UUID varchar(255) not null, + CONTAINER_KEY varchar(255), + UUID varchar(255) not null, + PAGE_MODEL_ID int8, + STYLE_ID int8, + primary key (CONTAINER_ID) +); + +create table CCM_CORE.STYLE_MEDIA_QUERIES ( + MEDIA_QUERY_ID int8 not null, + MAX_WIDTH_UNIT varchar(255), + MAX_WIDTH_VALUE float4, + MEDIA_TYPE varchar(255), + MIN_WIDTH_UNIT varchar(255), + MIN_WIDTH_VALUE float4, + primary key (MEDIA_QUERY_ID) +); + +create table CCM_CORE.STYLE_MEDIA_RULES ( + MEDIA_RULE_ID int8 not null, + MEDIA_QUERY_ID int8, + STYLE_ID int8, + primary key (MEDIA_RULE_ID) +); + +create table CCM_CORE.STYLE_PROPERTIES ( + PROPERTY_ID int8 not null, + NAME varchar(256), + PROPERTY_VALUE varchar(4096), + RULE_ID int8, + primary key (PROPERTY_ID) +); + +create table CCM_CORE.STYLE_RULES ( + RULE_ID int8 not null, + SELECTOR varchar(2048), + STYLE_ID int8, + primary key (RULE_ID) +); + +create table CCM_CORE.STYLES ( + STYLE_ID int8 not null, + STYLENAME varchar(255), + primary key (STYLE_ID) +); + +alter table CCM_CORE.PAGE_MODEL_COMPONENT_MODELS + add constraint FK1uvkayybawff8sqkmerqt60bk + foreign key (CONTAINER_ID) + references CCM_CORE.PAGE_MODEL_CONTAINER_MODELS; + +alter table CCM_CORE.PAGE_MODEL_CONTAINER_MODELS + add constraint FK1c6drneacxveol92vpum79fxb + foreign key (PAGE_MODEL_ID) + references CCM_CORE.PAGE_MODELS; + +alter table CCM_CORE.PAGE_MODEL_CONTAINER_MODELS + add constraint FKoi5wphv3vtwryc19akku28p24 + foreign key (STYLE_ID) + references CCM_CORE.STYLES; + +alter table CCM_CORE.STYLE_MEDIA_RULES + add constraint FKdq24a4atxp4c1sbqs8g6lpkx0 + foreign key (MEDIA_QUERY_ID) + references CCM_CORE.STYLE_MEDIA_QUERIES; + +alter table CCM_CORE.STYLE_MEDIA_RULES + add constraint FKf67h8q9kkjft9go2xo2572n17 + foreign key (STYLE_ID) + references CCM_CORE.STYLES; + +alter table CCM_CORE.STYLE_PROPERTIES + add constraint FKg2g0n7jmce3vjmula0898yp94 + foreign key (RULE_ID) + references CCM_CORE.STYLE_RULES; + +alter table CCM_CORE.STYLE_RULES + add constraint FKcbr0k93g001jix7i4kncsce1w + foreign key (STYLE_ID) + references CCM_CORE.STYLES; diff --git a/ccm-core/src/main/resources/db/migrations/org/libreccm/ccm_core/pgsql/V7_0_0_24__page_model_cleanup.sql b/ccm-core/src/main/resources/db/migrations/org/libreccm/ccm_core/pgsql/V7_0_0_24__page_model_cleanup.sql new file mode 100644 index 000000000..fbff826f2 --- /dev/null +++ b/ccm-core/src/main/resources/db/migrations/org/libreccm/ccm_core/pgsql/V7_0_0_24__page_model_cleanup.sql @@ -0,0 +1,7 @@ +-- drop obsolete columns + +alter table CCM_CORE.PAGE_MODEL_COMPONENT_MODELS + drop constraint FKo696ch035fe7rrueol1po13od; + +alter table CCM_CORE.PAGE_MODEL_COMPONENT_MODELS drop column PAGE_MODEL_ID; +