From 2c7dc68a430da502810fc1310a6fb23282f43542 Mon Sep 17 00:00:00 2001 From: Georgios Andrianakis Date: Wed, 26 Oct 2022 10:05:46 +0300 Subject: [PATCH] Add a validate capability to Flyway Fixes: #28625 (cherry picked from commit 56b52b9577b5a475d23328e71e22e533b94188ed) --- .../FlywayExtensionValidateAtStartTest.java | 23 +++++++++++++++++++ .../validate-at-start-config.properties | 7 ++++++ .../flyway/runtime/FlywayContainer.java | 8 +++++++ .../runtime/FlywayContainerProducer.java | 3 ++- .../FlywayDataSourceRuntimeConfig.java | 7 ++++++ .../flyway/runtime/FlywayRecorder.java | 3 +++ 6 files changed, 50 insertions(+), 1 deletion(-) create mode 100644 extensions/flyway/deployment/src/test/java/io/quarkus/flyway/test/FlywayExtensionValidateAtStartTest.java create mode 100644 extensions/flyway/deployment/src/test/resources/validate-at-start-config.properties diff --git a/extensions/flyway/deployment/src/test/java/io/quarkus/flyway/test/FlywayExtensionValidateAtStartTest.java b/extensions/flyway/deployment/src/test/java/io/quarkus/flyway/test/FlywayExtensionValidateAtStartTest.java new file mode 100644 index 0000000000000..9e685253a9bc4 --- /dev/null +++ b/extensions/flyway/deployment/src/test/java/io/quarkus/flyway/test/FlywayExtensionValidateAtStartTest.java @@ -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() { + + } + +} diff --git a/extensions/flyway/deployment/src/test/resources/validate-at-start-config.properties b/extensions/flyway/deployment/src/test/resources/validate-at-start-config.properties new file mode 100644 index 0000000000000..a0810ce17fb35 --- /dev/null +++ b/extensions/flyway/deployment/src/test/resources/validate-at-start-config.properties @@ -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 diff --git a/extensions/flyway/runtime/src/main/java/io/quarkus/flyway/runtime/FlywayContainer.java b/extensions/flyway/runtime/src/main/java/io/quarkus/flyway/runtime/FlywayContainer.java index a42d295ad4771..0a956422d1a6b 100644 --- a/extensions/flyway/runtime/src/main/java/io/quarkus/flyway/runtime/FlywayContainer.java +++ b/extensions/flyway/runtime/src/main/java/io/quarkus/flyway/runtime/FlywayContainer.java @@ -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; @@ -41,6 +45,10 @@ public boolean isRepairAtStart() { return repairAtStart; } + public boolean isValidateAtStart() { + return validateAtStart; + } + public String getDataSourceName() { return dataSourceName; } diff --git a/extensions/flyway/runtime/src/main/java/io/quarkus/flyway/runtime/FlywayContainerProducer.java b/extensions/flyway/runtime/src/main/java/io/quarkus/flyway/runtime/FlywayContainerProducer.java index d7f4c727e3a54..71e3a19cb2501 100644 --- a/extensions/flyway/runtime/src/main/java/io/quarkus/flyway/runtime/FlywayContainerProducer.java +++ b/extensions/flyway/runtime/src/main/java/io/quarkus/flyway/runtime/FlywayContainerProducer.java @@ -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); } } diff --git a/extensions/flyway/runtime/src/main/java/io/quarkus/flyway/runtime/FlywayDataSourceRuntimeConfig.java b/extensions/flyway/runtime/src/main/java/io/quarkus/flyway/runtime/FlywayDataSourceRuntimeConfig.java index 6162ab83fc048..9f9b05c9ada41 100644 --- a/extensions/flyway/runtime/src/main/java/io/quarkus/flyway/runtime/FlywayDataSourceRuntimeConfig.java +++ b/extensions/flyway/runtime/src/main/java/io/quarkus/flyway/runtime/FlywayDataSourceRuntimeConfig.java @@ -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. */ diff --git a/extensions/flyway/runtime/src/main/java/io/quarkus/flyway/runtime/FlywayRecorder.java b/extensions/flyway/runtime/src/main/java/io/quarkus/flyway/runtime/FlywayRecorder.java index cbfa3fb789e48..2bcaf79d62f87 100644 --- a/extensions/flyway/runtime/src/main/java/io/quarkus/flyway/runtime/FlywayRecorder.java +++ b/extensions/flyway/runtime/src/main/java/io/quarkus/flyway/runtime/FlywayRecorder.java @@ -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(); }