From e64afbbb5b1a677181730d3e80de81987b71ec31 Mon Sep 17 00:00:00 2001 From: Guillaume Smet Date: Mon, 4 Sep 2023 17:31:26 +0200 Subject: [PATCH] Do not load the FlywayContainer bean if not active (cherry picked from commit 28cf0b8f1d4299d100f4eeafddaca954a3dbe688) --- .../io/quarkus/flyway/FlywayProcessor.java | 7 +- .../flyway/runtime/FlywayRecorder.java | 70 +++++++++++-------- 2 files changed, 46 insertions(+), 31 deletions(-) diff --git a/extensions/flyway/deployment/src/main/java/io/quarkus/flyway/FlywayProcessor.java b/extensions/flyway/deployment/src/main/java/io/quarkus/flyway/FlywayProcessor.java index 61740d5cd0652..950894ff35b16 100644 --- a/extensions/flyway/deployment/src/main/java/io/quarkus/flyway/FlywayProcessor.java +++ b/extensions/flyway/deployment/src/main/java/io/quarkus/flyway/FlywayProcessor.java @@ -243,9 +243,14 @@ public ServiceStartBuildItem startActions(FlywayRecorder recorder, FlywayRuntimeConfig config, BuildProducer schemaReadyBuildItem, BuildProducer initializationCompleteBuildItem, + List jdbcDataSourceBuildItems, MigrationStateBuildItem migrationsBuildItem) { - recorder.doStartActions(); + Collection dataSourceNames = getDataSourceNames(jdbcDataSourceBuildItems); + + for (String dataSourceName : dataSourceNames) { + recorder.doStartActions(dataSourceName); + } // once we are done running the migrations, we produce a build item indicating that the // schema is "ready" 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 8200a4b18d730..7f82c9a5c3036 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 @@ -82,44 +82,54 @@ public Function, Flyway> flywayFunction(Strin return new Function<>() { @Override public Flyway apply(SyntheticCreationalContext context) { - Annotation flywayContainerQualifier; - if (DataSourceUtil.isDefault(dataSourceName)) { - flywayContainerQualifier = Default.Literal.INSTANCE; - } else { - flywayContainerQualifier = FlywayDataSourceLiteral.of(dataSourceName); - } - - FlywayContainer flywayContainer = context.getInjectedReference(FlywayContainer.class, flywayContainerQualifier); + FlywayContainer flywayContainer = context.getInjectedReference(FlywayContainer.class, + getFlywayContainerQualifier(dataSourceName)); return flywayContainer.getFlyway(); } }; } - public void doStartActions() { - for (InstanceHandle flywayContainerHandle : Arc.container().listAll(FlywayContainer.class)) { - FlywayContainer flywayContainer = flywayContainerHandle.get(); + public void doStartActions(String dataSourceName) { + FlywayDataSourceRuntimeConfig flywayDataSourceRuntimeConfig = config.getValue() + .getConfigForDataSourceName(dataSourceName); - if (!config.getValue().getConfigForDataSourceName(flywayContainer.getDataSourceName()).active) { - return; - } + if (!config.getValue().getConfigForDataSourceName(dataSourceName).active) { + return; + } - if (flywayContainer.isCleanAtStart()) { - flywayContainer.getFlyway().clean(); - } - if (flywayContainer.isValidateAtStart()) { - flywayContainer.getFlyway().validate(); - } - if (flywayContainer.isBaselineAtStart()) { - new FlywayExecutor(flywayContainer.getFlyway().getConfiguration()) - .execute(new BaselineCommand(flywayContainer.getFlyway()), true, null); - } - if (flywayContainer.isRepairAtStart()) { - flywayContainer.getFlyway().repair(); - } - if (flywayContainer.isMigrateAtStart()) { - flywayContainer.getFlyway().migrate(); - } + InstanceHandle flywayContainerInstanceHandle = Arc.container().instance(FlywayContainer.class, + getFlywayContainerQualifier(dataSourceName)); + + if (!flywayContainerInstanceHandle.isAvailable()) { + return; } + + FlywayContainer flywayContainer = flywayContainerInstanceHandle.get(); + + if (flywayContainer.isCleanAtStart()) { + flywayContainer.getFlyway().clean(); + } + if (flywayContainer.isValidateAtStart()) { + flywayContainer.getFlyway().validate(); + } + if (flywayContainer.isBaselineAtStart()) { + new FlywayExecutor(flywayContainer.getFlyway().getConfiguration()) + .execute(new BaselineCommand(flywayContainer.getFlyway()), true, null); + } + if (flywayContainer.isRepairAtStart()) { + flywayContainer.getFlyway().repair(); + } + if (flywayContainer.isMigrateAtStart()) { + flywayContainer.getFlyway().migrate(); + } + } + + private static Annotation getFlywayContainerQualifier(String dataSourceName) { + if (DataSourceUtil.isDefault(dataSourceName)) { + return Default.Literal.INSTANCE; + } + + return FlywayDataSourceLiteral.of(dataSourceName); } static class BaselineCommand implements FlywayExecutor.Command {