sample-data: replacing pipes ('|') with newlines now via customChange

this avoids database-specific code spread over all sample-data/*.xml files
This commit is contained in:
Michael Hoennig 2019-05-05 14:15:07 +02:00
parent 4c42d15c12
commit f8ed5069fb
7 changed files with 130 additions and 82 deletions

View File

@ -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 <customChange/> 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;
}
}

View File

@ -3,7 +3,6 @@
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">
<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"/>

View File

@ -16,26 +16,16 @@
separator=";" separator=";"
tableName="asset"> tableName="asset">
</loadData> </loadData>
</changeSet>
<changeSet id="20190502111400-2" author="mhoennig" context="sample-data,"> <customChange class="org.hostsharing.hsadminng.liquibase.ReplaceCustomChange">
<param name="tableName" value="asset" />
<!-- I've tried extracting this to a stored procedure, but a compatible call <param name="columnNames" value="remark" />
is only possible with PostgresSQL 11.x, otherwise the call syntax <param name="searchFor" value="|" />
between H2 and PostgresSQL would be different which defeated the point. <param name="replaceWith" value="\n" />
--> </customChange>
<sql dbms="h2">
UPDATE asset SET remark = replace(remark, '|', STRINGDECODE('\n'))
</sql>
<sql dbms="postgresql">
UPDATE asset SET remark = replace(remark, '|', E'\n')
</sql>
<rollback> <rollback>
DELETE FROM asset; DELETE FROM asset;
</rollback> </rollback>
</changeSet> </changeSet>
</databaseChangeLog> </databaseChangeLog>

View File

@ -10,32 +10,19 @@
<property name="floatType" value="float4" dbms="postgresql, h2"/> <property name="floatType" value="float4" dbms="postgresql, h2"/>
<property name="floatType" value="float" dbms="mysql, oracle, mssql"/> <property name="floatType" value="float" dbms="mysql, oracle, mssql"/>
<changeSet id="20190403083736-2" author="mhoennig" context="sample-data"> <changeSet id="20190505130300-1" author="mhoennig" context="sample-data">
<loadData encoding="UTF-8" <loadData encoding="UTF-8"
file="config/liquibase/sample-data/customers.csv" file="config/liquibase/sample-data/customers.csv"
separator=";" separator=";"
tableName="customer"> tableName="customer">
</loadData> </loadData>
</changeSet>
<changeSet id="20190403083736-3" author="mhoennig" context="sample-data"> <customChange class="org.hostsharing.hsadminng.liquibase.ReplaceCustomChange">
<param name="tableName" value="customer" />
<!-- I've tried extracting this to a stored procedure, but a compatible call <param name="columnNames" value="contractual_address,billing_address,remark" />
is only possible with PostgresSQL 11.x, otherwise the call syntax <param name="searchFor" value="|" />
between H2 and PostgresSQL would be different which defeated the point. <param name="replaceWith" value="\n" />
--> </customChange>
<sql dbms="h2">
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'));
</sql>
<sql dbms="postgresql">
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');
</sql>
<rollback> <rollback>
DELETE FROM customer; DELETE FROM customer;

View File

@ -16,22 +16,13 @@
separator=";" separator=";"
tableName="membership"> tableName="membership">
</loadData> </loadData>
</changeSet>
<changeSet id="20190502100700-2" author="mhoennig" context="sample-data"> <customChange class="org.hostsharing.hsadminng.liquibase.ReplaceCustomChange">
<param name="tableName" value="membership" />
<!-- I've tried extracting this to a stored procedure, but a compatible call <param name="columnNames" value="remark" />
is only possible with PostgresSQL 11.x, otherwise the call syntax <param name="searchFor" value="|" />
between H2 and PostgresSQL would be different which defeated the point. <param name="replaceWith" value="\n" />
--> </customChange>
<sql dbms="h2">
UPDATE membership SET remark = replace(remark, '|', STRINGDECODE('\n'))
</sql>
<sql dbms="postgresql">
UPDATE customer SET remark = replace(remark, '|', E'\n');
</sql>
<rollback> <rollback>
DELETE FROM membership; DELETE FROM membership;

View File

@ -16,22 +16,13 @@
separator=";" separator=";"
tableName="sepa_mandate"> tableName="sepa_mandate">
</loadData> </loadData>
</changeSet>
<changeSet id="20190503152800-2" author="mhoennig" context="sample-data"> <customChange class="org.hostsharing.hsadminng.liquibase.ReplaceCustomChange">
<param name="tableName" value="sepa_mandate" />
<!-- I've tried extracting this to a stored procedure, but a compatible call <param name="columnNames" value="remark" />
is only possible with PostgresSQL 11.x, otherwise the call syntax <param name="searchFor" value="|" />
between H2 and PostgresSQL would be different which defeated the point. <param name="replaceWith" value="\n" />
--> </customChange>
<sql dbms="h2">
UPDATE sepa_mandate SET remark = replace(remark, '|', STRINGDECODE('\n'))
</sql>
<sql dbms="postgresql">
UPDATE customer SET remark = replace(remark, '|', E'\n');
</sql>
<rollback> <rollback>
DELETE FROM sepa_mandate; DELETE FROM sepa_mandate;

View File

@ -16,22 +16,13 @@
separator=";" separator=";"
tableName="share"> tableName="share">
</loadData> </loadData>
</changeSet>
<changeSet id="20190502111400-2" author="mhoennig" context="sample-data"> <customChange class="org.hostsharing.hsadminng.liquibase.ReplaceCustomChange">
<param name="tableName" value="share" />
<!-- I've tried extracting this to a stored procedure, but a compatible call <param name="columnNames" value="remark" />
is only possible with PostgresSQL 11.x, otherwise the call syntax <param name="searchFor" value="|" />
between H2 and PostgresSQL would be different which defeated the point. <param name="replaceWith" value="\n" />
--> </customChange>
<sql dbms="h2">
UPDATE share SET remark = replace(remark, '|', STRINGDECODE('\n'))
</sql>
<sql dbms="postgresql">
UPDATE customer SET remark = replace(remark, '|', E'\n');
</sql>
<rollback> <rollback>
DELETE FROM share; DELETE FROM share;