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

The above source code is available under the MIT license.
Feel free to do whatever you want with it!

Copyright (c) 2022 Sebastiaan de Schaetzen

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

@igr
Copy link

igr commented Nov 12, 2023

How do you make sure this run before e.g. Hibernate initialization?

@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