Disable Flyway migrate on startup and run only when requested

Asked

Viewed 496 times

5

I’m just getting started with the Flyway in the Java along with the Spring Boot and I currently have an activity that tells me to do the Flyway execute the migrate() only when a certain endpoint is executed and with the database properties (Url, user, password) that is sent.

The problem is, the Flyway is currently running the migrate() when the project gives startup and with the properties of datasource which I specify in the file application.properties, I took a look at the Docs from Spring Boot and saw that to create a class that implements the FlywayMigrationStrategy which gives me greater control over the Flyway but it’s still not what I want, I still can’t, for example, call a method by passing a parameter to specify the datasource of the database and that method migrate() with the datasource that I passed by parameter.

I’ve looked in many places, Spring Boot and Flyway for example, but I couldn’t find the answer, someone can give me a light?

Edit 1:

I managed to execute the migrate() creating a variable of the type ClassicConfiguration and setting the datasource on it and then using the object constructor Flyway passing the ClassicConfiguration as parameter and passing this object Flyway as a parameter for the migrate().

Only I’m having a problem that I can’t run my project Spring Boot without a datasource configured by a class @Configuration of Java or by file application.properties.

The intention is for him to execute the project and do the migrate() only when a specific endpoint is executed (the endpoint in question would execute the migrate() with the parameters for connection to the database of the endpoint itself), and so soon after save the datasource that was used in the migrate() in the session so that the same user can make other requests without problems. It would be an application multi-tenacy.

1 answer

1

To disable Flyway during startup you can include the following parameter in your application.properties:

spring.flyway.enabled=false

This will disable Flyway autoconfiguration


Edit :)

You can create a wrapper and mount Flyway settings in hand:

import org.flywaydb.core.Flyway;

import org.springframework.boot.autoconfigure.flyway.FlywayMigrationStrategy;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.SimpleDriverDataSource;

public class FlywayMigrationWrapper {

    private final Flyway flyway;

    public MyFlywayMigrationStrategy(String url, String username, String password) {           
        this.flyway = Flyway.configure().dataSource(buildDatasource()).load();
    }

    private DataSource buildDatasource(String url, String username, String password) {
        return DataSourceBuilder.create().url(url).username(username).password(password).build();
    }

    public void migrate(Flyway flyway) {
        flyway.clean();
        flyway.migrate();
    }
}

I didn’t get to run this code, or play in the IDE, I took some examples from the Flyway API and the Spring Boot API (I don’t even know if the code compiles).

You can then create a @Bean and inject it where necessary, or customize more :)

I suggest you read the API of Flyway for more information.

  • right buddy, it worked, but now, as I call the method migrate() passing the datasource per parameter?

  • You want to run Migration by passing which parameters?

  • the parameters of an object Datasource -Url -Password -User

  • @Brunoeduardo edited the answer

  • Okay, buddy, I think I got it and I’m going to try to do this tomorrow because I’m running out of time, but so, I was able to do something similar (I even edited the question explaining that upstairs) only that I had some problems, I couldn’t run the project without a configured initial datasource, until I was able to run the migrate() by endpoint put the next request I do that needs to make a connection to the error bank saying that the datasource not configured, if you can help me with that I would appreciate very friend.

  • To move up the project without datasource you need to disable autoconfiguration, but this is already the subject of another question :) I think your original question was answered only with the spring.flyway.enabled=false

  • in fact it was answered in parts, let’s say that it does not matter if I manage to execute the project without flyway executing migrate() if the project does not execute, and also I’ve seen a question like this here in stackoverflow even about disabling autoconfiguration using exclude to not run the datasource Autoconfiguration class, only that doing so gives an error in Jdbctemplate that I use here, but I have to do some more research on that

  • but still it was worth for giving me a north friend

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.