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

Fixes #492: Effectively disable sql parser when defaultOn=false #493

Merged
merged 2 commits into from
Sep 28, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ public DatabaseChangeLog parse(String physicalChangeLogLocation, ChangeLogParame
changeSet.getChanges().size(),
changeSet.getRollback().getChanges().size()));

boolean usePerconaChangeSet = Configuration.getDefaultOn();
for (Change change : changeSet.getChanges()) {
RawSQLChange rawSQLChange = (RawSQLChange) change;
PerconaRawSQLChange perconaChange = convert(rawSQLChange);
Expand All @@ -71,7 +72,9 @@ public DatabaseChangeLog parse(String physicalChangeLogLocation, ChangeLogParame
String sql = perconaChange.getSql();
Matcher usePerconaMatcher = USE_PERCONA_PATTERN.matcher(sql);
if (usePerconaMatcher.find()) {
perconaChange.setUsePercona(Boolean.valueOf(usePerconaMatcher.group(1)));
Boolean usePercona = Boolean.valueOf(usePerconaMatcher.group(1));
perconaChange.setUsePercona(usePercona);
usePerconaChangeSet = usePerconaChangeSet || usePercona;
}

Matcher perconaOptionsMatcher = PERCONA_OPTIONS_PATTERN.matcher(sql);
Expand All @@ -80,6 +83,12 @@ public DatabaseChangeLog parse(String physicalChangeLogLocation, ChangeLogParame
}
}

if (!usePerconaChangeSet) {
LOG.fine(String.format("Not using percona toolkit for changeset %s::%s::%s, because no change requests it",
perconaChangeSet.getFilePath(), perconaChangeSet.getId(), perconaChangeSet.getAuthor()));
continue;
}

for (Change change : changeSet.getRollback().getChanges()) {
RawSQLChange rawSQLChange = (RawSQLChange) change;
PerconaRawSQLChange rollbackChange = convert(rawSQLChange);
Expand Down
49 changes: 48 additions & 1 deletion src/test/java/liquibase/ext/percona/ChangeLogParserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public void testReadLiquibaseUsePerconaFlagXML() throws Exception {
@Test
public void testReadLiquibaseUsePerconaFlagSQL() throws Exception {
DatabaseChangeLog changelog = loadChangeLog("test-changelog.sql");
Assertions.assertEquals(3, changelog.getChangeSets().size());
Assertions.assertEquals(4, changelog.getChangeSets().size());

// changeset 1
ChangeSet changeSet = changelog.getChangeSets().get(0);
Expand All @@ -101,5 +101,52 @@ public void testReadLiquibaseUsePerconaFlagSQL() throws Exception {
assertChange(change, PerconaRawSQLChange.class, null, "--foo");
rollback = changeSet.getRollback().getChanges().get(0);
assertChange(rollback, PerconaRawSQLChange.class, null, "--foo");

// changeset 4
changeSet = changelog.getChangeSets().get(3);
change = changeSet.getChanges().get(0);
assertChange(change, PerconaRawSQLChange.class, Boolean.TRUE, null);
rollback = changeSet.getRollback().getChanges().get(0);
assertChange(rollback, PerconaRawSQLChange.class, Boolean.TRUE, null);
}

@Test
public void testReadLiquibaseUsePerconaFlagSQL_defaultOff() throws Exception {
System.setProperty(Configuration.DEFAULT_ON, "false");
try {
DatabaseChangeLog changelog = loadChangeLog("test-changelog.sql");
Assertions.assertEquals(4, changelog.getChangeSets().size());

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

// changeset 2
changeSet = changelog.getChangeSets().get(1);
change = changeSet.getChanges().get(0);
Assertions.assertFalse(change instanceof PerconaRawSQLChange);
rollback = changeSet.getRollback().getChanges().get(0);
Assertions.assertFalse(rollback instanceof PerconaRawSQLChange);

// changeset 3
changeSet = changelog.getChangeSets().get(2);
change = changeSet.getChanges().get(0);
Assertions.assertFalse(change instanceof PerconaRawSQLChange);
rollback = changeSet.getRollback().getChanges().get(0);
Assertions.assertFalse(rollback instanceof PerconaRawSQLChange);

// changeset 4
changeSet = changelog.getChangeSets().get(3);
change = changeSet.getChanges().get(0);
assertChange(change, PerconaRawSQLChange.class, Boolean.TRUE, null);
rollback = changeSet.getRollback().getChanges().get(0);
assertChange(rollback, PerconaRawSQLChange.class, Boolean.TRUE, null);
} finally {
System.clearProperty(Configuration.DEFAULT_ON);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,8 @@ ALTER TABLE person ADD address VARCHAR(255) NULL;
-- liquibasePercona:perconaOptions="--foo"
--rollback ALTER TABLE person DROP COLUMN email;
ALTER TABLE person ADD email VARCHAR(255) NULL;

--changeset Alice:4
--liquibasePercona:usePercona="true"
--rollback ALTER TABLE person DROP COLUMN phone;
ALTER TABLE person ADD phone VARCHAR(255) NULL;
Loading