Skip to content

Commit

Permalink
Add a validate capability to Flyway
Browse files Browse the repository at this point in the history
Fixes: quarkusio#28625
(cherry picked from commit 56b52b9)
  • Loading branch information
geoand authored and gsmet committed Oct 28, 2022
1 parent 6b60931 commit 2c7dc68
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package io.quarkus.flyway.test;

import org.flywaydb.core.api.exception.FlywayValidateException;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import io.quarkus.test.QuarkusUnitTest;

public class FlywayExtensionValidateAtStartTest {

@RegisterExtension
static final QuarkusUnitTest config = new QuarkusUnitTest()
.withApplicationRoot((jar) -> jar
.addAsResource("db/migration/V1.0.0__Quarkus.sql")
.addAsResource("validate-at-start-config.properties", "application.properties"))
.setExpectedException(FlywayValidateException.class);

@Test
public void shouldNeverBeCalled() {

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
quarkus.datasource.db-kind=h2
quarkus.datasource.username=sa
quarkus.datasource.password=sa
quarkus.datasource.jdbc.url=jdbc:h2:tcp://localhost/mem:test-quarkus-validate-at-start;DB_CLOSE_DELAY=-1

# Flyway config properties
quarkus.flyway.validate-at-start=true
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,21 @@ public class FlywayContainer {
private final boolean cleanAtStart;
private final boolean migrateAtStart;
private final boolean repairAtStart;

private final boolean validateAtStart;
private final String dataSourceName;
private final boolean hasMigrations;
private final boolean createPossible;
private final String id;

public FlywayContainer(Flyway flyway, boolean cleanAtStart, boolean migrateAtStart, boolean repairAtStart,
boolean validateAtStart,
String dataSourceName, boolean hasMigrations, boolean createPossible) {
this.flyway = flyway;
this.cleanAtStart = cleanAtStart;
this.migrateAtStart = migrateAtStart;
this.repairAtStart = repairAtStart;
this.validateAtStart = validateAtStart;
this.dataSourceName = dataSourceName;
this.hasMigrations = hasMigrations;
this.createPossible = createPossible;
Expand All @@ -41,6 +45,10 @@ public boolean isRepairAtStart() {
return repairAtStart;
}

public boolean isValidateAtStart() {
return validateAtStart;
}

public String getDataSourceName() {
return dataSourceName;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public FlywayContainer createFlyway(DataSource dataSource, String dataSourceName
final Flyway flyway = new FlywayCreator(matchingRuntimeConfig, matchingBuildTimeConfig).withCallbacks(callbacks)
.createFlyway(dataSource);
return new FlywayContainer(flyway, matchingRuntimeConfig.cleanAtStart, matchingRuntimeConfig.migrateAtStart,
matchingRuntimeConfig.repairAtStart, dataSourceName, hasMigrations, createPossible);
matchingRuntimeConfig.repairAtStart, matchingRuntimeConfig.validateAtStart, dataSourceName, hasMigrations,
createPossible);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,13 @@ public static FlywayDataSourceRuntimeConfig defaultConfig() {
@ConfigItem
public boolean repairAtStart;

/**
* true to execute a Flyway validate command when the application starts, false otherwise.
*
*/
@ConfigItem
public boolean validateAtStart;

/**
* Enable the creation of the history table if it does not exist already.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ public void doStartActions() {
if (flywayContainer.isCleanAtStart()) {
flywayContainer.getFlyway().clean();
}
if (flywayContainer.isValidateAtStart()) {
flywayContainer.getFlyway().validate();
}
if (flywayContainer.isRepairAtStart()) {
flywayContainer.getFlyway().repair();
}
Expand Down

0 comments on commit 2c7dc68

Please sign in to comment.