From f8ed5069fb0064b896c91380cf971823a92c289d Mon Sep 17 00:00:00 2001 From: Michael Hoennig Date: Sun, 5 May 2019 14:15:07 +0200 Subject: [PATCH] sample-data: replacing pipes ('|') with newlines now via customChange this avoids database-specific code spread over all sample-data/*.xml files --- .../liquibase/ReplaceCustomChange.java | 99 +++++++++++++++++++ .../resources/config/liquibase/master.xml | 1 - .../config/liquibase/sample-data/assets.xml | 22 ++--- .../liquibase/sample-data/customers.xml | 27 ++--- .../liquibase/sample-data/memberships.xml | 21 ++-- .../liquibase/sample-data/sepamandates.xml | 21 ++-- .../config/liquibase/sample-data/shares.xml | 21 ++-- 7 files changed, 130 insertions(+), 82 deletions(-) create mode 100644 src/main/java/org/hostsharing/hsadminng/liquibase/ReplaceCustomChange.java diff --git a/src/main/java/org/hostsharing/hsadminng/liquibase/ReplaceCustomChange.java b/src/main/java/org/hostsharing/hsadminng/liquibase/ReplaceCustomChange.java new file mode 100644 index 00000000..5cb2998e --- /dev/null +++ b/src/main/java/org/hostsharing/hsadminng/liquibase/ReplaceCustomChange.java @@ -0,0 +1,99 @@ +// Licensed under Apache-2.0 +package org.hostsharing.hsadminng.liquibase; + +import liquibase.change.custom.CustomTaskChange; +import liquibase.database.Database; +import liquibase.database.jvm.JdbcConnection; +import liquibase.exception.CustomChangeException; +import liquibase.exception.DatabaseException; +import liquibase.exception.SetupException; +import liquibase.exception.ValidationErrors; +import liquibase.resource.ResourceAccessor; + +import java.sql.SQLException; +import java.sql.Statement; + +/** + * Used in Liquibase for database-independent search and replace with special characters. + */ +public class ReplaceCustomChange implements CustomTaskChange { + + private String tableName; + private String columnNames; + private String searchFor; + private String replaceWith; + + @SuppressWarnings("unused") + private ResourceAccessor resourceAccessor; + + @Override + public void execute(final Database database) throws CustomChangeException { + final JdbcConnection conn = (JdbcConnection) database.getConnection(); + final boolean isH2 = "H2".equals(database.getDatabaseProductName()); + try { + conn.setAutoCommit(false); + final Statement statement = conn.createStatement(); + for (String columnName : columnNames.split(",")) { + final String sql = "UPDATE " + tableName + " SET " + columnName + "= replace(" + columnName + ", '|', " + + (isH2 ? "STRINGDECODE('\n') " : "E'\\n'") + ")"; + statement.executeUpdate(sql); + } + conn.commit(); + } catch (DatabaseException | SQLException e) { + throw new CustomChangeException("cannot perform search&replace", e); + } + } + + @Override + public String getConfirmationMessage() { + return "table " + tableName + " / columns " + columnNames + ": replaced all '" + searchFor + "' to '" + replaceWith + + "'"; + } + + @Override + public void setUp() throws SetupException { + + } + + @Override + public void setFileOpener(final ResourceAccessor resourceAccessor) { + + } + + @Override + public ValidationErrors validate(final Database database) { + return null; + } + + public String getTableName() { + return tableName; + } + + public void setTableName(final String tableName) { + this.tableName = tableName; + } + + public String getColumnNames() { + return columnNames; + } + + public void setColumnNames(final String columns) { + this.columnNames = columns; + } + + public String getSearchFor() { + return searchFor; + } + + public void setSearchFor(final String searchFor) { + this.searchFor = searchFor; + } + + public String getReplaceWith() { + return replaceWith; + } + + public void setReplaceWith(final String replaceWith) { + this.replaceWith = replaceWith; + } +} diff --git a/src/main/resources/config/liquibase/master.xml b/src/main/resources/config/liquibase/master.xml index 36848208..c4daff8a 100644 --- a/src/main/resources/config/liquibase/master.xml +++ b/src/main/resources/config/liquibase/master.xml @@ -3,7 +3,6 @@ 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"> - diff --git a/src/main/resources/config/liquibase/sample-data/assets.xml b/src/main/resources/config/liquibase/sample-data/assets.xml index 7352b4ee..d880a765 100644 --- a/src/main/resources/config/liquibase/sample-data/assets.xml +++ b/src/main/resources/config/liquibase/sample-data/assets.xml @@ -16,26 +16,16 @@ separator=";" tableName="asset"> - - - - - - - UPDATE asset SET remark = replace(remark, '|', STRINGDECODE('\n')) - - - - UPDATE asset SET remark = replace(remark, '|', E'\n') - + + + + + + DELETE FROM asset; - diff --git a/src/main/resources/config/liquibase/sample-data/customers.xml b/src/main/resources/config/liquibase/sample-data/customers.xml index a6e45e4b..9ada85c7 100644 --- a/src/main/resources/config/liquibase/sample-data/customers.xml +++ b/src/main/resources/config/liquibase/sample-data/customers.xml @@ -10,32 +10,19 @@ - + - - - - - - - UPDATE customer SET contractual_address = replace(contractual_address, '|', STRINGDECODE('\n')); - UPDATE customer SET billing_address = replace(billing_address, '|', STRINGDECODE('\n')); - UPDATE customer SET remark = replace(remark, '|', STRINGDECODE('\n')); - - - - UPDATE customer SET contractual_address = replace(contractual_address, '|', '\n'); - UPDATE customer SET billing_address = replace(billing_address, '|', '\n'); - UPDATE customer SET remark = replace(remark, '|', E'\n'); - + + + + + + DELETE FROM customer; diff --git a/src/main/resources/config/liquibase/sample-data/memberships.xml b/src/main/resources/config/liquibase/sample-data/memberships.xml index e7d0936d..38bb4ca5 100644 --- a/src/main/resources/config/liquibase/sample-data/memberships.xml +++ b/src/main/resources/config/liquibase/sample-data/memberships.xml @@ -16,22 +16,13 @@ separator=";" tableName="membership"> - - - - - - - UPDATE membership SET remark = replace(remark, '|', STRINGDECODE('\n')) - - - - UPDATE customer SET remark = replace(remark, '|', E'\n'); - + + + + + + DELETE FROM membership; diff --git a/src/main/resources/config/liquibase/sample-data/sepamandates.xml b/src/main/resources/config/liquibase/sample-data/sepamandates.xml index fa8ef3f7..dee524a7 100644 --- a/src/main/resources/config/liquibase/sample-data/sepamandates.xml +++ b/src/main/resources/config/liquibase/sample-data/sepamandates.xml @@ -16,22 +16,13 @@ separator=";" tableName="sepa_mandate"> - - - - - - - UPDATE sepa_mandate SET remark = replace(remark, '|', STRINGDECODE('\n')) - - - - UPDATE customer SET remark = replace(remark, '|', E'\n'); - + + + + + + DELETE FROM sepa_mandate; diff --git a/src/main/resources/config/liquibase/sample-data/shares.xml b/src/main/resources/config/liquibase/sample-data/shares.xml index 19b221a3..e19760b4 100644 --- a/src/main/resources/config/liquibase/sample-data/shares.xml +++ b/src/main/resources/config/liquibase/sample-data/shares.xml @@ -16,22 +16,13 @@ separator=";" tableName="share"> - - - - - - - UPDATE share SET remark = replace(remark, '|', STRINGDECODE('\n')) - - - - UPDATE customer SET remark = replace(remark, '|', E'\n'); - + + + + + + DELETE FROM share;