Skip to content

Commit

Permalink
Do not load the FlywayContainer bean if not active
Browse files Browse the repository at this point in the history
(cherry picked from commit 28cf0b8)
  • Loading branch information
gsmet committed Sep 19, 2023
1 parent 8bb35c4 commit e64afbb
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -243,9 +243,14 @@ public ServiceStartBuildItem startActions(FlywayRecorder recorder,
FlywayRuntimeConfig config,
BuildProducer<JdbcDataSourceSchemaReadyBuildItem> schemaReadyBuildItem,
BuildProducer<InitTaskCompletedBuildItem> initializationCompleteBuildItem,
List<JdbcDataSourceBuildItem> jdbcDataSourceBuildItems,
MigrationStateBuildItem migrationsBuildItem) {

recorder.doStartActions();
Collection<String> 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"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,44 +82,54 @@ public Function<SyntheticCreationalContext<Flyway>, Flyway> flywayFunction(Strin
return new Function<>() {
@Override
public Flyway apply(SyntheticCreationalContext<Flyway> 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<FlywayContainer> 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<FlywayContainer> 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<BaselineResult> {
Expand Down

0 comments on commit e64afbb

Please sign in to comment.