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:
parent
4c42d15c12
commit
f8ed5069fb
@ -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;
|
||||
}
|
||||
}
|
@ -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">
|
||||
|
||||
<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/20190430152137_added_entity_Membership.xml" relativeToChangelogFile="false"/>
|
||||
|
@ -16,26 +16,16 @@
|
||||
separator=";"
|
||||
tableName="asset">
|
||||
</loadData>
|
||||
</changeSet>
|
||||
|
||||
<changeSet id="20190502111400-2" author="mhoennig" context="sample-data,">
|
||||
|
||||
<!-- I've tried extracting this to a stored procedure, but a compatible call
|
||||
is only possible with PostgresSQL 11.x, otherwise the call syntax
|
||||
between H2 and PostgresSQL would be different which defeated the point.
|
||||
-->
|
||||
|
||||
<sql dbms="h2">
|
||||
UPDATE asset SET remark = replace(remark, '|', STRINGDECODE('\n'))
|
||||
</sql>
|
||||
|
||||
<sql dbms="postgresql">
|
||||
UPDATE asset SET remark = replace(remark, '|', E'\n')
|
||||
</sql>
|
||||
<customChange class="org.hostsharing.hsadminng.liquibase.ReplaceCustomChange">
|
||||
<param name="tableName" value="asset" />
|
||||
<param name="columnNames" value="remark" />
|
||||
<param name="searchFor" value="|" />
|
||||
<param name="replaceWith" value="\n" />
|
||||
</customChange>
|
||||
|
||||
<rollback>
|
||||
DELETE FROM asset;
|
||||
</rollback>
|
||||
</changeSet>
|
||||
|
||||
</databaseChangeLog>
|
||||
|
@ -10,32 +10,19 @@
|
||||
<property name="floatType" value="float4" dbms="postgresql, h2"/>
|
||||
<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"
|
||||
file="config/liquibase/sample-data/customers.csv"
|
||||
separator=";"
|
||||
tableName="customer">
|
||||
</loadData>
|
||||
</changeSet>
|
||||
|
||||
<changeSet id="20190403083736-3" author="mhoennig" context="sample-data">
|
||||
|
||||
<!-- I've tried extracting this to a stored procedure, but a compatible call
|
||||
is only possible with PostgresSQL 11.x, otherwise the call syntax
|
||||
between H2 and PostgresSQL would be different which defeated the point.
|
||||
-->
|
||||
|
||||
<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>
|
||||
<customChange class="org.hostsharing.hsadminng.liquibase.ReplaceCustomChange">
|
||||
<param name="tableName" value="customer" />
|
||||
<param name="columnNames" value="contractual_address,billing_address,remark" />
|
||||
<param name="searchFor" value="|" />
|
||||
<param name="replaceWith" value="\n" />
|
||||
</customChange>
|
||||
|
||||
<rollback>
|
||||
DELETE FROM customer;
|
||||
|
@ -16,22 +16,13 @@
|
||||
separator=";"
|
||||
tableName="membership">
|
||||
</loadData>
|
||||
</changeSet>
|
||||
|
||||
<changeSet id="20190502100700-2" author="mhoennig" context="sample-data">
|
||||
|
||||
<!-- I've tried extracting this to a stored procedure, but a compatible call
|
||||
is only possible with PostgresSQL 11.x, otherwise the call syntax
|
||||
between H2 and PostgresSQL would be different which defeated the point.
|
||||
-->
|
||||
|
||||
<sql dbms="h2">
|
||||
UPDATE membership SET remark = replace(remark, '|', STRINGDECODE('\n'))
|
||||
</sql>
|
||||
|
||||
<sql dbms="postgresql">
|
||||
UPDATE customer SET remark = replace(remark, '|', E'\n');
|
||||
</sql>
|
||||
<customChange class="org.hostsharing.hsadminng.liquibase.ReplaceCustomChange">
|
||||
<param name="tableName" value="membership" />
|
||||
<param name="columnNames" value="remark" />
|
||||
<param name="searchFor" value="|" />
|
||||
<param name="replaceWith" value="\n" />
|
||||
</customChange>
|
||||
|
||||
<rollback>
|
||||
DELETE FROM membership;
|
||||
|
@ -16,22 +16,13 @@
|
||||
separator=";"
|
||||
tableName="sepa_mandate">
|
||||
</loadData>
|
||||
</changeSet>
|
||||
|
||||
<changeSet id="20190503152800-2" author="mhoennig" context="sample-data">
|
||||
|
||||
<!-- I've tried extracting this to a stored procedure, but a compatible call
|
||||
is only possible with PostgresSQL 11.x, otherwise the call syntax
|
||||
between H2 and PostgresSQL would be different which defeated the point.
|
||||
-->
|
||||
|
||||
<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>
|
||||
<customChange class="org.hostsharing.hsadminng.liquibase.ReplaceCustomChange">
|
||||
<param name="tableName" value="sepa_mandate" />
|
||||
<param name="columnNames" value="remark" />
|
||||
<param name="searchFor" value="|" />
|
||||
<param name="replaceWith" value="\n" />
|
||||
</customChange>
|
||||
|
||||
<rollback>
|
||||
DELETE FROM sepa_mandate;
|
||||
|
@ -16,22 +16,13 @@
|
||||
separator=";"
|
||||
tableName="share">
|
||||
</loadData>
|
||||
</changeSet>
|
||||
|
||||
<changeSet id="20190502111400-2" author="mhoennig" context="sample-data">
|
||||
|
||||
<!-- I've tried extracting this to a stored procedure, but a compatible call
|
||||
is only possible with PostgresSQL 11.x, otherwise the call syntax
|
||||
between H2 and PostgresSQL would be different which defeated the point.
|
||||
-->
|
||||
|
||||
<sql dbms="h2">
|
||||
UPDATE share SET remark = replace(remark, '|', STRINGDECODE('\n'))
|
||||
</sql>
|
||||
|
||||
<sql dbms="postgresql">
|
||||
UPDATE customer SET remark = replace(remark, '|', E'\n');
|
||||
</sql>
|
||||
<customChange class="org.hostsharing.hsadminng.liquibase.ReplaceCustomChange">
|
||||
<param name="tableName" value="share" />
|
||||
<param name="columnNames" value="remark" />
|
||||
<param name="searchFor" value="|" />
|
||||
<param name="replaceWith" value="\n" />
|
||||
</customChange>
|
||||
|
||||
<rollback>
|
||||
DELETE FROM share;
|
||||
|
Loading…
Reference in New Issue
Block a user