Skip to content

Commit

Permalink
Support ignoreMigrationPatterns config in Flyway Added ignoreMigratio…
Browse files Browse the repository at this point in the history
…nPatterns parameter Added ignoreMigrationPatterns test

Signed-off-by: Theodor Mihalache <[email protected]>
  • Loading branch information
tmihalac committed Aug 9, 2022
1 parent b08a139 commit 7d3e0ef
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,16 +60,23 @@ public Flyway createFlyway(DataSource dataSource) {
configure.baselineOnMigrate(flywayRuntimeConfig.baselineOnMigrate);
configure.validateOnMigrate(flywayRuntimeConfig.validateOnMigrate);
configure.validateMigrationNaming(flywayRuntimeConfig.validateMigrationNaming);
List<String> patterns = new ArrayList<>(2);
//https://flywaydb.org/documentation/configuration/parameters/ignoreMigrationPatterns
if (flywayRuntimeConfig.ignoreMissingMigrations) {
patterns.add("*:Missing");
}
if (flywayRuntimeConfig.ignoreFutureMigrations) {
patterns.add("*:Future");

final String[] ignoreMigrationPatterns;
if (flywayRuntimeConfig.ignoreMigrationPatterns.isPresent()) {
ignoreMigrationPatterns = flywayRuntimeConfig.ignoreMigrationPatterns.get();
} else {
List<String> patterns = new ArrayList<>(2);
if (flywayRuntimeConfig.ignoreMissingMigrations) {
patterns.add("*:Missing");
}
if (flywayRuntimeConfig.ignoreFutureMigrations) {
patterns.add("*:Future");
}
// Default is *:Future
ignoreMigrationPatterns = patterns.toArray(new String[0]);
}
// Default is *:Future
configure.ignoreMigrationPatterns(patterns.toArray(new String[0]));

configure.ignoreMigrationPatterns(ignoreMigrationPatterns);
configure.cleanOnValidationError(flywayRuntimeConfig.cleanOnValidationError);
configure.outOfOrder(flywayRuntimeConfig.outOfOrder);
if (flywayRuntimeConfig.baselineVersion.isPresent()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,4 +187,11 @@ public static final FlywayDataSourceRuntimeConfig defaultConfig() {
*/
@ConfigItem
public boolean validateMigrationNaming;

/**
* Ignore migrations during validate and repair according to a given list of patterns.
* Patterns are comma separated.
*/
@ConfigItem
public Optional<String[]> ignoreMigrationPatterns;
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.quarkus.flyway.runtime;

import static java.util.Arrays.asList;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
Expand All @@ -15,6 +16,7 @@
import org.flywaydb.core.Flyway;
import org.flywaydb.core.api.Location;
import org.flywaydb.core.api.configuration.Configuration;
import org.flywaydb.core.api.pattern.ValidatePattern;
import org.flywaydb.core.internal.util.ValidatePatternUtils;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -258,6 +260,23 @@ void testValidateMigrationNaming() {
assertTrue(createdFlywayConfig().isValidateMigrationNaming());
}

@Test
@DisplayName("validateIgnoreMigrationPatterns defaults to false and it is correctly set")
void testIgnoreMigrationPatterns() {
creator = new FlywayCreator(runtimeConfig, buildConfig);
assertEquals(0, createdFlywayConfig().getIgnoreMigrationPatterns().length);
assertFalse(runtimeConfig.ignoreMigrationPatterns.isPresent());

runtimeConfig.ignoreMigrationPatterns = Optional.of(new String[] { "*:missing" });
creator = new FlywayCreator(runtimeConfig, buildConfig);
final ValidatePattern[] existingIgnoreMigrationPatterns = createdFlywayConfig().getIgnoreMigrationPatterns();
assertEquals(1, existingIgnoreMigrationPatterns.length);
final String[] ignoreMigrationPatterns = runtimeConfig.ignoreMigrationPatterns.get();
final ValidatePattern[] validatePatterns = Arrays.stream(ignoreMigrationPatterns)
.map(ValidatePattern::fromPattern).toArray(ValidatePattern[]::new);
assertArrayEquals(validatePatterns, existingIgnoreMigrationPatterns);
}

private static List<String> pathList(Location[] locations) {
return Stream.of(locations).map(Location::getPath).collect(Collectors.toList());
}
Expand Down

0 comments on commit 7d3e0ef

Please sign in to comment.