From d8be11a33db2d6071dec13270ea5515d63d14c1b Mon Sep 17 00:00:00 2001 From: Guillaume Smet Date: Tue, 5 Sep 2023 18:01:52 +0200 Subject: [PATCH] Better handle the UnconfiguredDataSource case I wasn't exactly aware of this new development and it needs to be handled a bit differently. (cherry picked from commit 797162aa9a97cecabf6155d64df18bfa0a2f581d) --- .../quarkus/flyway/runtime/FlywayRecorder.java | 7 +++++-- .../UnconfiguredDataSourceFlywayContainer.java | 16 ++++++++++++++++ 2 files changed, 21 insertions(+), 2 deletions(-) create mode 100644 extensions/flyway/runtime/src/main/java/io/quarkus/flyway/runtime/UnconfiguredDataSourceFlywayContainer.java 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 7f82c9a5c3036..f8df08e4e7825 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 @@ -8,7 +8,6 @@ import javax.sql.DataSource; import jakarta.enterprise.inject.Default; -import jakarta.enterprise.inject.UnsatisfiedResolutionException; import org.flywaydb.core.Flyway; import org.flywaydb.core.FlywayExecutor; @@ -67,7 +66,7 @@ public Function, FlywayContainer> fl public FlywayContainer apply(SyntheticCreationalContext context) { DataSource dataSource = context.getInjectedReference(DataSources.class).getDataSource(dataSourceName); if (dataSource instanceof UnconfiguredDataSource) { - throw new UnsatisfiedResolutionException("No datasource present"); + return new UnconfiguredDataSourceFlywayContainer(dataSourceName); } FlywayContainerProducer flywayProducer = context.getInjectedReference(FlywayContainerProducer.class); @@ -106,6 +105,10 @@ public void doStartActions(String dataSourceName) { FlywayContainer flywayContainer = flywayContainerInstanceHandle.get(); + if (flywayContainer instanceof UnconfiguredDataSourceFlywayContainer) { + return; + } + if (flywayContainer.isCleanAtStart()) { flywayContainer.getFlyway().clean(); } diff --git a/extensions/flyway/runtime/src/main/java/io/quarkus/flyway/runtime/UnconfiguredDataSourceFlywayContainer.java b/extensions/flyway/runtime/src/main/java/io/quarkus/flyway/runtime/UnconfiguredDataSourceFlywayContainer.java new file mode 100644 index 0000000000000..a3206cd8141ae --- /dev/null +++ b/extensions/flyway/runtime/src/main/java/io/quarkus/flyway/runtime/UnconfiguredDataSourceFlywayContainer.java @@ -0,0 +1,16 @@ +package io.quarkus.flyway.runtime; + +import org.flywaydb.core.Flyway; + +public class UnconfiguredDataSourceFlywayContainer extends FlywayContainer { + + public UnconfiguredDataSourceFlywayContainer(String dataSourceName) { + super(null, false, false, false, false, false, dataSourceName, false, false); + } + + @Override + public Flyway getFlyway() { + throw new UnsupportedOperationException( + "Cannot get a Flyway instance for unconfigured datasource " + getDataSourceName()); + } +}