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 17, 2022
1 parent 7015944 commit a46d6e6
Show file tree
Hide file tree
Showing 8 changed files with 90 additions and 5 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,9 +208,10 @@ 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();

// once we are done running the migrations, we produce a build item indicating that the
Expand All @@ -216,6 +221,18 @@ 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", "QUARKUS_FLYWAY_ENABLED", "true"))
.withInheritEnvVars(true)
.withInheritMounts(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 @@ -27,6 +27,12 @@ public class FlywayRecorder {

static final List<FlywayContainer> FLYWAY_CONTAINERS = new ArrayList<>(2);

private final FlywayRuntimeConfig config;

public FlywayRecorder(FlywayRuntimeConfig config) {
this.config = config;
}

public void setApplicationMigrationFiles(Collection<String> migrationFiles) {
log.debugv("Setting the following application migration files: {0}", migrationFiles);
QuarkusPathLocationScanner.setApplicationMigrationFiles(migrationFiles);
Expand Down Expand Up @@ -69,6 +75,9 @@ public Flyway get() {
}

public void doStartActions() {
if (!config.enabled) {
return;
}
for (FlywayContainer flywayContainer : FLYWAY_CONTAINERS) {
if (flywayContainer.isCleanAtStart()) {
flywayContainer.getFlyway().clean();
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,6 +53,8 @@
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;
Expand Down Expand Up @@ -283,16 +287,27 @@ void createBeans(LiquibaseRecorder recorder,
ServiceStartBuildItem startLiquibase(LiquibaseRecorder recorder,
List<JdbcDataSourceBuildItem> jdbcDataSourceBuildItems,
BuildProducer<JdbcDataSourceSchemaReadyBuildItem> schemaReadyBuildItem) {
// will actually run the actions at runtime
recorder.doStartActions();

recorder.doStartActions();
// once we are done running the migrations, we produce a build item indicating that the
// schema is "ready"
schemaReadyBuildItem.produce(new JdbcDataSourceSchemaReadyBuildItem(getDataSourceNames(jdbcDataSourceBuildItems)));

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", "QUARKUS_LIQUIBASE_ENABLED", "true"))
.withInheritEnvVars(true)
.withInheritMounts(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 @@ -6,6 +6,8 @@
import javax.enterprise.inject.UnsatisfiedResolutionException;
import javax.sql.DataSource;

import org.jboss.logging.Logger;

import io.quarkus.agroal.runtime.DataSources;
import io.quarkus.agroal.runtime.UnconfiguredDataSource;
import io.quarkus.arc.Arc;
Expand All @@ -20,6 +22,14 @@
@Recorder
public class LiquibaseRecorder {

private static final Logger log = Logger.getLogger(LiquibaseRecorder.class);

private final LiquibaseRuntimeConfig config;

public LiquibaseRecorder(LiquibaseRuntimeConfig config) {
this.config = config;
}

public Supplier<LiquibaseFactory> liquibaseSupplier(String dataSourceName) {
DataSource dataSource = DataSources.fromName(dataSourceName);
if (dataSource instanceof UnconfiguredDataSource) {
Expand All @@ -41,6 +51,10 @@ public LiquibaseFactory get() {
}

public void doStartActions() {
if (!config.enabled) {
return;
}

try {
InjectableInstance<LiquibaseFactory> liquibaseFactoryInstance = Arc.container()
.select(LiquibaseFactory.class, Any.Literal.INSTANCE);
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 a46d6e6

Please sign in to comment.