Created
September 28, 2022 10:35
-
-
Save seeseemelk/27c601ad8ce5a6a89d423a476d6e9237 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.MigrationService; | |
import io.quarkus.arc.Arc; | |
import io.quarkus.arc.InstanceHandle; | |
import io.quarkus.flyway.runtime.FlywayContainer; | |
import io.quarkus.flyway.runtime.FlywayContainerProducer; | |
import io.quarkus.flyway.runtime.QuarkusPathLocationScanner; | |
import io.quarkus.runtime.StartupEvent; | |
import org.eclipse.microprofile.config.inject.ConfigProperty; | |
import org.flywaydb.core.Flyway; | |
import org.jboss.logging.Logger; | |
import javax.enterprise.context.ApplicationScoped; | |
import javax.enterprise.event.Observes; | |
import javax.inject.Inject; | |
import javax.sql.DataSource; | |
import java.util.List; | |
import java.util.stream.Collectors; | |
@ApplicationScoped | |
public class MigrationService | |
{ | |
@Inject | |
Logger logger; | |
@ConfigProperty(name = "quarkus.datasource.reactive.url") | |
String datasourceUrl; | |
@ConfigProperty(name = "quarkus.datasource.username") | |
String datasourceUsername; | |
@ConfigProperty(name = "quarkus.datasource.password") | |
String datasourcePassword; | |
@ConfigProperty(name = "todo.migration.files") | |
List<String> files; | |
public void runFlywayMigration(@Observes StartupEvent event) | |
{ | |
logger.info("Initialising flyway..."); | |
QuarkusPathLocationScanner.setApplicationMigrationFiles(files.stream() | |
.map(file -> "db/migration/" + file) | |
.collect(Collectors.toList())); | |
DataSource datasource = Flyway.configure() | |
.dataSource(getDatasourceUrl(), datasourceUsername, datasourcePassword) | |
.getDataSource(); | |
try (InstanceHandle<FlywayContainerProducer> instance = Arc.container().instance(FlywayContainerProducer.class)) | |
{ | |
FlywayContainerProducer flywayProducer = instance.get(); | |
FlywayContainer flywayContainer = flywayProducer.createFlyway(datasource, "<default>", true, true); | |
Flyway flyway = flywayContainer.getFlyway(); | |
flyway.migrate(); | |
} | |
} | |
private String getDatasourceUrl() | |
{ | |
if (datasourceUrl.startsWith("vertx-reactive:")) | |
return "jdbc:" + datasourceUrl.substring("vertx-reactive:".length()); | |
else | |
return "jdbc:" + datasourceUrl; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Yeah, Quarkus is a pain when you need to do something that is not scripted. What I just learned is that it's not possible to do the above before Hibernate or e.g. Quartz starts. Maybe on a custom connection provider, if you create a new flyway-init thread; but I didn't want to go there. I somehow managed to achieve what I needed with existing options; not the best solution, but it worked.