Skip to content

Instantly share code, notes, and snippets.

@seeseemelk
Created September 28, 2022 10:35
Show Gist options
  • Save seeseemelk/27c601ad8ce5a6a89d423a476d6e9237 to your computer and use it in GitHub Desktop.
Save seeseemelk/27c601ad8ce5a6a89d423a476d6e9237 to your computer and use it in GitHub Desktop.
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;
}
}
@seeseemelk
Copy link
Author

Good question, I've got no idea. I had to abandon the project I was building using Quarkus as I was running in to too many growing pains in Quarkus in some of the lesser used modules.

@igr
Copy link

igr commented Nov 13, 2023

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment