Skip to content

Commit

Permalink
feat: flyway & liquibase produce init containers
Browse files Browse the repository at this point in the history
  • Loading branch information
iocanel committed Nov 3, 2022
1 parent 2cd6d32 commit a8f2aa1
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 6 deletions.
8 changes: 8 additions & 0 deletions extensions/flyway/deployment/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@
<groupId>io.quarkus</groupId>
<artifactId>quarkus-flyway</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-container-image-spi</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-kubernetes-spi</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-junit5-internal</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import io.quarkus.arc.deployment.SyntheticBeansRuntimeInitBuildItem;
import io.quarkus.arc.processor.DotNames;
import io.quarkus.builder.item.SimpleBuildItem;
import io.quarkus.container.spi.ContainerImageInfoBuildItem;
import io.quarkus.datasource.common.runtime.DataSourceUtil;
import io.quarkus.deployment.Feature;
import io.quarkus.deployment.annotations.BuildProducer;
Expand All @@ -60,6 +61,9 @@
import io.quarkus.flyway.runtime.FlywayBuildTimeConfig;
import io.quarkus.flyway.runtime.FlywayContainerProducer;
import io.quarkus.flyway.runtime.FlywayRecorder;
import io.quarkus.flyway.runtime.FlywayRuntimeConfig;
import io.quarkus.kubernetes.spi.KubernetesEnvBuildItem;
import io.quarkus.kubernetes.spi.KubernetesInitContainerBuildItem;
import io.quarkus.runtime.util.ClassPathUtils;

class FlywayProcessor {
Expand Down Expand Up @@ -204,10 +208,14 @@ void createBeans(FlywayRecorder recorder,
@Consume(SyntheticBeansRuntimeInitBuildItem.class)
@Record(ExecutionTime.RUNTIME_INIT)
public ServiceStartBuildItem startActions(FlywayRecorder recorder,
FlywayRuntimeConfig config,
BuildProducer<JdbcDataSourceSchemaReadyBuildItem> schemaReadyBuildItem,
MigrationStateBuildItem migrationsBuildItem) {
// will actually run the actions at runtime
recorder.doStartActions();

if (config.enabled) {
// will actually run the actions at runtime
recorder.doStartActions();
}

// once we are done running the migrations, we produce a build item indicating that the
// schema is "ready"
Expand All @@ -216,6 +224,16 @@ public ServiceStartBuildItem startActions(FlywayRecorder recorder,
return new ServiceStartBuildItem("flyway");
}

@BuildStep
void configureKubernetes(ContainerImageInfoBuildItem imageInfo,
BuildProducer<KubernetesInitContainerBuildItem> initContainers, BuildProducer<KubernetesEnvBuildItem> env) {
initContainers.produce(KubernetesInitContainerBuildItem.create("flyway")
.withImage(imageInfo.getImage())
.withEnvVars(Map.of("QUARKUS_FLYWAY_RUN_AND_EXIT", "true")));

env.produce(KubernetesEnvBuildItem.createSimpleVar("QUARKUS_FLYWAY_ENABLED", "false", null));
}

private Set<String> getDataSourceNames(List<JdbcDataSourceBuildItem> jdbcDataSourceBuildItems) {
Set<String> result = new HashSet<>(jdbcDataSourceBuildItems.size());
for (JdbcDataSourceBuildItem item : jdbcDataSourceBuildItems) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ public FlywayDataSourceRuntimeConfig getConfigForDataSourceName(String dataSourc
return namedDataSources.getOrDefault(dataSourceName, FlywayDataSourceRuntimeConfig.defaultConfig());
}

/**
* Flag to enable / disable Liquibase.
*
*/
@ConfigItem(defaultValue = "true")
public boolean enabled;

/**
* Flyway configuration for the default datasource.
*/
Expand All @@ -32,4 +39,4 @@ public FlywayDataSourceRuntimeConfig getConfigForDataSourceName(String dataSourc
*/
@ConfigItem(name = ConfigItem.PARENT)
public Map<String, FlywayDataSourceRuntimeConfig> namedDataSources = Collections.emptyMap();
}
}
8 changes: 8 additions & 0 deletions extensions/liquibase/deployment/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@
<groupId>io.quarkus</groupId>
<artifactId>quarkus-liquibase</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-container-image-spi</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-kubernetes-spi</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-junit5-internal</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.function.BiConsumer;
Expand All @@ -31,6 +32,7 @@
import io.quarkus.arc.deployment.SyntheticBeanBuildItem;
import io.quarkus.arc.deployment.SyntheticBeansRuntimeInitBuildItem;
import io.quarkus.arc.processor.DotNames;
import io.quarkus.container.spi.ContainerImageInfoBuildItem;
import io.quarkus.datasource.common.runtime.DataSourceUtil;
import io.quarkus.deployment.Feature;
import io.quarkus.deployment.annotations.BuildProducer;
Expand All @@ -51,11 +53,14 @@
import io.quarkus.deployment.builditem.nativeimage.ServiceProviderBuildItem;
import io.quarkus.deployment.pkg.steps.NativeOrNativeSourcesBuild;
import io.quarkus.deployment.util.ServiceUtil;
import io.quarkus.kubernetes.spi.KubernetesEnvBuildItem;
import io.quarkus.kubernetes.spi.KubernetesInitContainerBuildItem;
import io.quarkus.liquibase.LiquibaseDataSource;
import io.quarkus.liquibase.LiquibaseFactory;
import io.quarkus.liquibase.runtime.LiquibaseBuildTimeConfig;
import io.quarkus.liquibase.runtime.LiquibaseFactoryProducer;
import io.quarkus.liquibase.runtime.LiquibaseRecorder;
import io.quarkus.liquibase.runtime.LiquibaseRuntimeConfig;
import liquibase.change.Change;
import liquibase.change.DatabaseChangeProperty;
import liquibase.change.core.CreateProcedureChange;
Expand Down Expand Up @@ -278,10 +283,13 @@ void createBeans(LiquibaseRecorder recorder,
@Record(ExecutionTime.RUNTIME_INIT)
@Consume(SyntheticBeansRuntimeInitBuildItem.class)
ServiceStartBuildItem startLiquibase(LiquibaseRecorder recorder,
LiquibaseRuntimeConfig config,
List<JdbcDataSourceBuildItem> jdbcDataSourceBuildItems,
BuildProducer<JdbcDataSourceSchemaReadyBuildItem> schemaReadyBuildItem) {
// will actually run the actions at runtime
recorder.doStartActions();
if (config.enabled) {
// will actually run the actions at runtime
recorder.doStartActions();
}

// once we are done running the migrations, we produce a build item indicating that the
// schema is "ready"
Expand All @@ -290,6 +298,16 @@ ServiceStartBuildItem startLiquibase(LiquibaseRecorder recorder,
return new ServiceStartBuildItem("liquibase");
}

@BuildStep
void configureKubernetes(ContainerImageInfoBuildItem imageInfo,
BuildProducer<KubernetesInitContainerBuildItem> initContainers, BuildProducer<KubernetesEnvBuildItem> env) {
initContainers.produce(KubernetesInitContainerBuildItem.create("liquibase")
.withImage(imageInfo.getImage())
.withEnvVars(Map.of("QUARKUS_LIQUIBASE_RUN_AND_EXIT", "true")));

env.produce(KubernetesEnvBuildItem.createSimpleVar("QUARKUS_LIQUIBASE_ENABLED", "false", null));
}

private Set<String> getDataSourceNames(List<JdbcDataSourceBuildItem> jdbcDataSourceBuildItems) {
Set<String> result = new HashSet<>(jdbcDataSourceBuildItems.size());
for (JdbcDataSourceBuildItem item : jdbcDataSourceBuildItems) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@ public LiquibaseDataSourceRuntimeConfig getConfigForDataSourceName(String dataSo
return namedDataSources.getOrDefault(dataSourceName, LiquibaseDataSourceRuntimeConfig.defaultConfig());
}

/**
* Flag to enable / disable Liquibase.
*
*/
@ConfigItem(defaultValue = "true")
public boolean enabled;

/**
* Liquibase configuration for the default datasource.
*/
Expand All @@ -40,4 +47,4 @@ public LiquibaseDataSourceRuntimeConfig getConfigForDataSourceName(String dataSo
*/
@ConfigItem(name = ConfigItem.PARENT)
public Map<String, LiquibaseDataSourceRuntimeConfig> namedDataSources = Collections.emptyMap();
}
}

0 comments on commit a8f2aa1

Please sign in to comment.