Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support rollback for formatted SQL changelogs #362

Merged
merged 3 commits into from
Oct 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -18,27 +18,31 @@
import java.util.regex.Pattern;
import java.util.stream.Collectors;

import liquibase.Scope;
import liquibase.change.Change;
import liquibase.change.core.RawSQLChange;
import liquibase.changelog.ChangeLogParameters;
import liquibase.changelog.ChangeSet;
import liquibase.changelog.DatabaseChangeLog;
import liquibase.exception.ChangeLogParseException;
import liquibase.logging.Logger;
import liquibase.parser.core.formattedsql.FormattedSqlChangeLogParser;
import liquibase.resource.ResourceAccessor;
import liquibase.servicelocator.PrioritizedService;

public class PerconaFormattedSqlChangeLogParser extends FormattedSqlChangeLogParser {
private static final Logger LOG = Scope.getCurrentScope().getLog(PerconaFormattedSqlChangeLogParser.class);

private static final Pattern USE_PERCONA_PATTERN = Pattern.compile("(?im)^\\s*\\-\\-\\s*liquibasePercona:usePercona=\"(false|true)\"\\s*$");
private static final Pattern PERCONA_OPTIONS_PATTERN = Pattern.compile("(?im)^\\s*\\-\\-\\s*liquibasePercona:perconaOptions=\"(.*)\"\\s*$");

@Override
public int getPriority() {
return PrioritizedService.PRIORITY_DEFAULT + 50;
}

@Override
public DatabaseChangeLog parse(String physicalChangeLogLocation, ChangeLogParameters changeLogParameters, ResourceAccessor resourceAccessor) throws ChangeLogParseException {
Pattern usePerconaPattern = Pattern.compile("(?im)^\\s*\\-\\-\\s*liquibasePercona:usePercona=\"(false|true)\"\\s*$");
Pattern perconaOptionsPattern = Pattern.compile("(?im)^\\s*\\-\\-\\s*liquibasePercona:perconaOptions=\"(.*)\"\\s*$");

DatabaseChangeLog changeLog = super.parse(physicalChangeLogLocation, changeLogParameters, resourceAccessor);

for (int i = 0; i < changeLog.getChangeSets().size(); i++) {
Expand All @@ -55,32 +59,67 @@ public DatabaseChangeLog parse(String physicalChangeLogLocation, ChangeLogParame
changeSet.getRunWith(), changeSet.getRunWithSpoolFile(), changeSet.isRunInTransaction(), changeSet.getObjectQuotingStrategy(),
changeLog);

LOG.fine(String.format("Changeset %s::%s::%s contains %d changes and %d rollback changes",
changeSet.getFilePath(), changeSet.getId(), changeSet.getAuthor(),
changeSet.getChanges().size(),
changeSet.getRollback().getChanges().size()));

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();
Matcher usePerconaMatcher = usePerconaPattern.matcher(sql);
Matcher usePerconaMatcher = USE_PERCONA_PATTERN.matcher(sql);
if (usePerconaMatcher.find()) {
perconaChange.setUsePercona(Boolean.valueOf(usePerconaMatcher.group(1)));
}

Matcher perconaOptionsMatcher = perconaOptionsPattern.matcher(sql);
Matcher perconaOptionsMatcher = PERCONA_OPTIONS_PATTERN.matcher(sql);
if (perconaOptionsMatcher.find()) {
perconaChange.setPerconaOptions(perconaOptionsMatcher.group(1));
}
}

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

if (!perconaChangeSet.getChanges().isEmpty()) {
if (perconaChangeSet.getChanges().size() > 1) {
LOG.warning(String.format("The changeset %s::%s::%s contains %d changes - using the first " +
"one to copy percona options",
perconaChangeSet.getFilePath(), perconaChangeSet.getId(), perconaChangeSet.getAuthor(),
perconaChangeSet.getChanges().size()));
}

PerconaRawSQLChange forwardChange = (PerconaRawSQLChange) perconaChangeSet.getChanges().get(0);
rollbackChange.setUsePercona(forwardChange.getUsePercona());
rollbackChange.setPerconaOptions(forwardChange.getPerconaOptions());
} else {
LOG.warning(String.format("The changeset %s::%s::%s contains 0 changes, but contains rollback - " +
"using default percona options",
perconaChangeSet.getFilePath(), perconaChangeSet.getId(), perconaChangeSet.getAuthor()));
}

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;