Skip to content

Commit

Permalink
Effectively disable sql parser when defaultOn=false
Browse files Browse the repository at this point in the history
  • Loading branch information
westse committed Aug 16, 2024
1 parent 9efdac3 commit 9af00d0
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 2 deletions.
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;

0 comments on commit 9af00d0

Please sign in to comment.