From 295e45e8b89366d3afcaa52a8319c4c4cfe84679 Mon Sep 17 00:00:00 2001 From: Michael Hoennig Date: Mon, 6 May 2019 18:20:10 +0200 Subject: [PATCH] ReplaceCustomChange: assume auto commit off during Liquibase-Changeset --- .../liquibase/ReplaceCustomChange.java | 5 +++-- .../liquibase/ReplaceCustomChangeUnitTest.java | 17 +++++++++++++++++ 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/hostsharing/hsadminng/liquibase/ReplaceCustomChange.java b/src/main/java/org/hostsharing/hsadminng/liquibase/ReplaceCustomChange.java index 33b31610..f358d125 100644 --- a/src/main/java/org/hostsharing/hsadminng/liquibase/ReplaceCustomChange.java +++ b/src/main/java/org/hostsharing/hsadminng/liquibase/ReplaceCustomChange.java @@ -1,6 +1,8 @@ // Licensed under Apache-2.0 package org.hostsharing.hsadminng.liquibase; +import static com.google.common.base.Verify.verify; + import liquibase.change.custom.CustomTaskChange; import liquibase.database.Database; import liquibase.database.jvm.JdbcConnection; @@ -28,7 +30,7 @@ public class ReplaceCustomChange implements CustomTaskChange { final JdbcConnection conn = (JdbcConnection) database.getConnection(); final boolean isH2 = "H2".equals(database.getDatabaseProductName()); try { - conn.setAutoCommit(false); + verify(!conn.getAutoCommit(), "assuming auto commit to be off in Liquibase changeset"); final Statement statement = conn.createStatement(); for (String columnName : columnNames.split(",")) { final String sql = "UPDATE " + tableName + " SET " + columnName + "= replace(" + columnName + ", '" + searchFor @@ -36,7 +38,6 @@ public class ReplaceCustomChange implements CustomTaskChange { (isH2 ? "STRINGDECODE('" + replaceWith + "')" : "E'" + replaceWith + "'") + ")"; statement.executeUpdate(sql); } - conn.commit(); } catch (DatabaseException | SQLException e) { throw new CustomChangeException("cannot perform search&replace", e); } diff --git a/src/test/java/org/hostsharing/hsadminng/liquibase/ReplaceCustomChangeUnitTest.java b/src/test/java/org/hostsharing/hsadminng/liquibase/ReplaceCustomChangeUnitTest.java index df9f5b1f..53c971c8 100644 --- a/src/test/java/org/hostsharing/hsadminng/liquibase/ReplaceCustomChangeUnitTest.java +++ b/src/test/java/org/hostsharing/hsadminng/liquibase/ReplaceCustomChangeUnitTest.java @@ -13,6 +13,8 @@ import liquibase.exception.CustomChangeException; import liquibase.exception.DatabaseException; import liquibase.exception.SetupException; +import com.google.common.base.VerifyException; + import org.junit.Before; import org.junit.Rule; import org.junit.Test; @@ -43,6 +45,7 @@ public class ReplaceCustomChangeUnitTest { @Before public void initMocks() throws DatabaseException { given(database.getConnection()).willReturn(connection); + given(connection.getAutoCommit()).willReturn(false); given(connection.createStatement()).willReturn(statement); } @@ -86,6 +89,20 @@ public class ReplaceCustomChangeUnitTest { assertThat(actual).isEqualTo("in table some_table / columns address,remark: replaced all '|' to '\\n'"); } + @Test + public void throwsValidationErrorIfAutoCommitIsOff() throws Exception { + // given + given(database.getDatabaseProductName()).willReturn(POSTGRES_DATABASE_PRODUCT_NAME); + final ReplaceCustomChange replaceCustomChange = givenReplaceCustomChange("some_table", "address,remark", "|", "\\n"); + given(connection.getAutoCommit()).willReturn(true); + + // when + final Throwable actual = catchThrowable(() -> replaceCustomChange.execute(database)); + + // then + assertThat(actual).isInstanceOf(VerifyException.class); + } + @Test public void onDatabaseExceptionThrowsCustomChangeException() throws Exception { // given