Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting up multiple database with Flyway

I am trying to setup two different database with Flyway 5.0.7, MySQL for development and H2 for testing. I have configured both databases in respective files.

For Development, src/main/resource/application.properties

spring.datasource.url=jdbc:mysql://localhost:3306/moment
spring.datasource.username=root
spring.datasource.password=root

flyway.locations=db/migration,db/specific/mysql

For Testing, src/test/resource/application.properties

spring.datasource.driver-class-name=org.h2.Driver
spring.datasource.url=jdbc:h2:mem:db;DB_CLOSE_DELAY=-1
spring.datasource.username=sa
spring.datasource.password=sa

flyway.locations=db/migration,db/specific/h2

Below is the folder structure for Flyway migration files

Flyway migration file structure

In this case Flyway is not able to find migration files under specific folder and throws error when applying V1.1__Insert_Records.sql for table not found.

If I move the specific folder inside db/migration, I am getting error for duplicate files of same version.

Any suggestions how should I configure migration files for multiple databases to work with Flyway?

like image 700
Meena Chaudhary Avatar asked Jul 15 '18 08:07

Meena Chaudhary


1 Answers

I suspect you might be using Spring Boot 2.x here? If so, flyway.locations is no longer valid and will be ignored.

Flyway will then just use the default location (db/migration), which will find only the V1.1__Insert_Records.sql script but not the V1__Create_table.sql script.

With Spring Boot 2.x, flyway.locations must be prefixed with spring.:

spring.flyway.locations=db/migration,db/specific/h2

By the way, if you use the {vendor} placeholder in the location, Spring Boot will work out the directory from the lowercase of the database driver id (h2, mysql, oracle, etc), which is nice:

spring.flyway.locations=db/migration,db/specific/{vendor}
like image 134
codemonkey Avatar answered Oct 07 '22 07:10

codemonkey