Skip to content

Commit

Permalink
Support rollback for formatted SQL changelogs
Browse files Browse the repository at this point in the history
  • Loading branch information
adangel committed Oct 1, 2023
1 parent 30ba744 commit 67f9f4c
Show file tree
Hide file tree
Showing 6 changed files with 181 additions and 11 deletions.
76 changes: 76 additions & 0 deletions src/it/addColumnWithRollbackSqlChangelog/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?xml version="1.0" encoding="UTF-8"?>

<!-- Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file
distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to you under
the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may
obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to
in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under
the License. -->

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>@[email protected]</groupId>
<artifactId>liquibase-percona-it-addColumnWithRollbackSqlChangelog</artifactId>
<version>1.0-SNAPSHOT</version>
<name>my-app</name>

<build>
<plugins>
<plugin>
<groupId>org.liquibase</groupId>
<artifactId>liquibase-maven-plugin</artifactId>
<version>@liquibase.version@</version>
<dependencies>
<dependency>
<groupId>@project.groupId@</groupId>
<artifactId>@project.artifactId@</artifactId>
<version>@project.version@</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>@mysql.version@</version>
</dependency>
</dependencies>
<configuration>
<changeLogFile>test-changelog.sql</changeLogFile>
<driver>com.mysql.jdbc.Driver</driver>
<url>jdbc:mysql://@config_host@:@config_port@/@config_dbname@?useSSL=false&amp;allowPublicKeyRetrieval=true</url>
<username>@config_user@</username>
<password>@config_password@</password>
</configuration>
<executions>
<execution>
<id>update</id>
<phase>pre-integration-test</phase>
<goals>
<goal>update</goal>
</goals>
</execution>
<execution>
<id>rollbackSQL</id>
<phase>pre-integration-test</phase>
<goals>
<goal>rollbackSQL</goal>
</goals>
<configuration>
<rollbackCount>3</rollbackCount>
</configuration>
</execution>
<execution>
<id>rollback</id>
<phase>pre-integration-test</phase>
<goals>
<goal>rollback</goal>
</goals>
<configuration>
<rollbackCount>3</rollbackCount>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
14 changes: 14 additions & 0 deletions src/it/addColumnWithRollbackSqlChangelog/test-changelog.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
--liquibase formatted sql

--changeset Alice:1
--rollback DROP TABLE person;
CREATE TABLE person (name VARCHAR(255) NOT NULL, CONSTRAINT PK_PERSON PRIMARY KEY (name));

--changeset Alice:2
--rollback ALTER TABLE person DROP COLUMN address;
ALTER TABLE person ADD address VARCHAR(255) NULL;

--changeset Alice:3
--liquibasePercona:usePercona="false"
--rollback ALTER TABLE person DROP COLUMN email;
ALTER TABLE person ADD email VARCHAR(255) NULL;
42 changes: 42 additions & 0 deletions src/it/addColumnWithRollbackSqlChangelog/verify.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

File buildLog = new File( basedir, 'build.log' )
assert buildLog.exists()
def buildLogText = buildLog.text;

// changeset 3: email has usePercona="false"
assert buildLogText.contains("Rolling Back Changeset: test-changelog.sql::3::Alice")
assert !buildLogText.contains("Executing: pt-online-schema-change --alter-foreign-keys-method=auto --nocheck-unique-key-change --alter=\"DROP COLUMN email\" --password=*** --execute h=${config_host},P=${config_port},u=${config_user},D=testdb,t=person");
// changeset 2: address
assert buildLogText.contains("Rolling Back Changeset: test-changelog.sql::2::Alice")
assert buildLogText.contains("Executing: pt-online-schema-change --alter-foreign-keys-method=auto --nocheck-unique-key-change --alter=\"DROP COLUMN address\" --password=*** --execute h=${config_host},P=${config_port},u=${config_user},D=testdb,t=person");
// changeset 1: table
assert buildLogText.contains("Rolling Back Changeset: test-changelog.sql::1::Alice")


File sql = new File( basedir, 'target/liquibase/migrate.sql' )
assert sql.exists()
def sqlText = sql.text;
assert sqlText.contains("pt-online-schema-change")
// changeset 3: email has usePercona="false"
assert !sqlText.contains("-- pt-online-schema-change --alter-foreign-keys-method=auto --nocheck-unique-key-change --alter=\"DROP COLUMN email\" --password=*** --execute h=${config_host},P=${config_port},u=${config_user},D=testdb,t=person;");
// changeset 2: address
assert sqlText.contains("-- pt-online-schema-change --alter-foreign-keys-method=auto --nocheck-unique-key-change --alter=\"DROP COLUMN address\" --password=*** --execute h=${config_host},P=${config_port},u=${config_user},D=testdb,t=person;");
assert !sqlText.contains("password=${config_password}")
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,7 @@ public DatabaseChangeLog parse(String physicalChangeLogLocation, ChangeLogParame

for (Change change : changeSet.getChanges()) {
RawSQLChange rawSQLChange = (RawSQLChange) change;
PerconaRawSQLChange perconaChange = new PerconaRawSQLChange();
perconaChange.setSql(rawSQLChange.getSql());
perconaChange.setSplitStatements(rawSQLChange.isSplitStatements());
perconaChange.setStripComments(rawSQLChange.isStripComments());
perconaChange.setEndDelimiter(rawSQLChange.getEndDelimiter());
perconaChange.setRerunnable(rawSQLChange.isRerunnable());
perconaChange.setComment(rawSQLChange.getComment());
perconaChange.setDbms(rawSQLChange.getDbms());
PerconaRawSQLChange perconaChange = convert(rawSQLChange);
perconaChangeSet.addChange(perconaChange);

String sql = perconaChange.getSql();
Expand All @@ -78,9 +71,34 @@ public DatabaseChangeLog parse(String physicalChangeLogLocation, ChangeLogParame
perconaChange.setPerconaOptions(perconaOptionsMatcher.group(1));
}
}

for (Change change : changeSet.getRollback().getChanges()) {
RawSQLChange rawSQLChange = (RawSQLChange) change;
PerconaRawSQLChange rollbackChange = convert(rawSQLChange);

assert perconaChangeSet.getChanges().size() == 1;
PerconaRawSQLChange forwardChange = (PerconaRawSQLChange) perconaChangeSet.getChanges().get(0);
rollbackChange.setUsePercona(forwardChange.getUsePercona());
rollbackChange.setPerconaOptions(forwardChange.getPerconaOptions());

perconaChangeSet.getRollback().getChanges().add(rollbackChange);
}

changeLog.getChangeSets().set(i, perconaChangeSet);
}

return changeLog;
}

private static PerconaRawSQLChange convert(RawSQLChange rawSQLChange) {
PerconaRawSQLChange perconaChange = new PerconaRawSQLChange();
perconaChange.setSql(rawSQLChange.getSql());
perconaChange.setSplitStatements(rawSQLChange.isSplitStatements());
perconaChange.setStripComments(rawSQLChange.isStripComments());
perconaChange.setEndDelimiter(rawSQLChange.getEndDelimiter());
perconaChange.setRerunnable(rawSQLChange.isRerunnable());
perconaChange.setComment(rawSQLChange.getComment());
perconaChange.setDbms(rawSQLChange.getDbms());
return perconaChange;
}
}
23 changes: 20 additions & 3 deletions src/test/java/liquibase/ext/percona/ChangeLogParserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

import liquibase.change.Change;
import liquibase.changelog.ChangeLogParameters;
import liquibase.changelog.ChangeSet;
import liquibase.changelog.DatabaseChangeLog;
import liquibase.parser.ChangeLogParser;
import liquibase.parser.ChangeLogParserFactory;
Expand Down Expand Up @@ -78,11 +79,27 @@ public void testReadLiquibaseUsePerconaFlagXML() throws Exception {
public void testReadLiquibaseUsePerconaFlagSQL() throws Exception {
DatabaseChangeLog changelog = loadChangeLog("test-changelog.sql");
Assertions.assertEquals(3, changelog.getChangeSets().size());
Change change = changelog.getChangeSets().get(0).getChanges().get(0);

// changeset 1
ChangeSet changeSet = changelog.getChangeSets().get(0);
Change change = changeSet.getChanges().get(0);
assertChange(change, PerconaRawSQLChange.class, null, null);
change = changelog.getChangeSets().get(1).getChanges().get(0);
Assertions.assertEquals(1, changeSet.getRollback().getChanges().size());
Change rollback = changeSet.getRollback().getChanges().get(0);
assertChange(rollback, PerconaRawSQLChange.class, null, null);

// changeset 2
changeSet = changelog.getChangeSets().get(1);
change = changeSet.getChanges().get(0);
assertChange(change, PerconaRawSQLChange.class, Boolean.FALSE, null);
change = changelog.getChangeSets().get(2).getChanges().get(0);
rollback = changeSet.getRollback().getChanges().get(0);
assertChange(rollback, PerconaRawSQLChange.class, Boolean.FALSE, null);

// changeset 3
changeSet = changelog.getChangeSets().get(2);
change = changeSet.getChanges().get(0);
assertChange(change, PerconaRawSQLChange.class, null, "--foo");
rollback = changeSet.getRollback().getChanges().get(0);
assertChange(rollback, PerconaRawSQLChange.class, null, "--foo");
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
--liquibase formatted sql

--changeset Alice:1
--rollback DROP TABLE person;
CREATE TABLE person (name VARCHAR(255) NOT NULL, CONSTRAINT PK_PERSON PRIMARY KEY (name));

--changeset Alice:2
--liquibasePercona:usePercona="false"
--rollback ALTER TABLE person DROP COLUMN address;
ALTER TABLE person ADD address VARCHAR(255) NULL;

--changeset Alice:3
-- liquibasePercona:perconaOptions="--foo"
--rollback ALTER TABLE person DROP COLUMN email;
ALTER TABLE person ADD email VARCHAR(255) NULL;

0 comments on commit 67f9f4c

Please sign in to comment.