Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple instances of Flyway pointing to different Datasources in Java application

I have created two different instances of Flyway in 2 different projects in my eclipse workspace.

They are pointing to different data sources/2 diff databases and also have separate src.main.resources.db.migration packages. Each package contains different sql files for the respective databases.

When I start my application, Flyway is identifying scripts from one project only (project initialized first) and executing the scripts from this project to the other DB also.

Is it possible for Flyway to update 2 databases simultaneously?


Thanks for the reply. But could you please explain how this can be implemented in Java Code. For eg: Curretnly I am using the following way...

            Flyway flyway = new Flyway();               
            InitialContext ictx = new InitialContext();
            DataSource dataSource = (DataSource) ictx.lookup("DS-name");
            flyway.setDataSource(dataSource);
            flyway.setLocations("main.resources.db.migration");//location under first project
                flyway.migrate();

                 Flyway flywaygen =  new Flyway;

            InitialContext ictx = new InitialContext();
            DataSource dataSource = (DataSource) ictx.lookup("DS-name");
            flywaygen.setDataSource(dataSource);
            flywaygen.setLocations("main.resources.emlogis.migration");//location                       under second project
                flywaygen.migrate();

The issue is that flywaygen is also looking at the first project location. So if in the first location 3 sql scripts are added and 2 scripts in the second one, the second flyway instance says Migration 3 completed. So flywaygen is also pointing at main.resources.db.migration instead of main.resources.emlogis.migration.

like image 723
user1862868 Avatar asked Jan 31 '26 09:01

user1862868


1 Answers

Define a second flyway bean. Then add to both flyway beans a different SqlMigrationPrefix property and name your migration scripts accordingly.

For example

<bean class="com.googlecode.flyway.core.Flyway" init-method="migrate">
   <property name="dataSource" ref="dataSource1" />
   <property name="sqlMigrationPrefix" value="DB1_" />
</bean>
<bean class="com.googlecode.flyway.core.Flyway" init-method="migrate">
   <property name="dataSource" ref="dataSource2" />
   <property name="sqlMigrationPrefix" value="DB2_" />
</bean>

Your migration scripts have to follow this naming scheme:

DB1_1.0__initial_setup.sql
DB1_1.1__new_column.sql

DB2_1.0__initial_setup.sql
DB2_1.1__new_column.sql

EDIT - configuration by code:

In your code you are having two Flyway instances. This instances are equivalent to the definition as a spring bean. So you can call the following to achieve the same:

flyway.setSqlMigrationPrefix("DB1_");
flywaygen.setSqlMigrationPrefix("DB2_");
like image 180
Kai Avatar answered Feb 02 '26 22:02

Kai