I am attempting to configure Spring Boot and Flyway to apply separate migrations to two different datasources using Spring Boot 2.2.6 and Flyway 5.2.3.
The primary datasource:
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(basePackages = {"com.gobsmack.*.repository.primary"},
entityManagerFactoryRef = "primaryEntityManagerFactory",
transactionManagerRef = "primaryTransactionManager"
)
public class PrimaryDatasourceConfiguration {
@Bean
@Primary
@ConfigurationProperties(prefix = "spring.primary-datasource")
public DataSource primaryDataSource() {
return DataSourceBuilder.create().build();
}
@Primary
@Bean(name = "primaryEntityManagerFactory")
public LocalContainerEntityManagerFactoryBean primaryEntityManagerFactory(EntityManagerFactoryBuilder builder) {
return builder.dataSource(primaryDataSource())
.packages("com.gobsmack.*.model")
.build();
}
@Primary
@Bean
public PlatformTransactionManager primaryTransactionManager(
final @Qualifier("primaryEntityManagerFactory") LocalContainerEntityManagerFactoryBean primaryEntityManagerFactory) {
return new JpaTransactionManager(primaryEntityManagerFactory.getObject());
}
}
The second datasource:
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(basePackages = {"com.gobsmack.*.repository.secondary"},
entityManagerFactoryRef = "secondaryEntityManagerFactory",
transactionManagerRef = "secondaryTransactionManager"
)
public class SecondaryDatasourceConfiguration {
@Bean
@ConfigurationProperties(prefix = "spring.secondary-datasource")
public DataSource secondaryDataSource() {
return DataSourceBuilder.create().build();
}
@Bean(name = "secondaryEntityManagerFactory")
public LocalContainerEntityManagerFactoryBean secondaryEntityManagerFactory(EntityManagerFactoryBuilder builder) {
return builder.dataSource(secondaryDataSource())
.packages("com.gobsmack.*.model")
.build();
}
@Bean
public PlatformTransactionManager secondaryTransactionManager(
final @NonNull @Qualifier("secondaryEntityManagerFactory") LocalContainerEntityManagerFactoryBean secondaryEntityManagerFactory) {
return new JpaTransactionManager(secondaryEntityManagerFactory.getObject());
}
}
The datasource connection properties:
spring:
primary-datasource:
jdbc-url: jdbc:mysql://localhost:3306/primary?useSSL=false&primary
password: password
driver-class-name: com.mysql.cj.jdbc.Driver
username: username
secondary-datasource:
jdbc-url: jdbc:mysql://localhost:3306/secondary?useSSL=false&secondary
password: password
driver-class-name: com.mysql.cj.jdbc.Driver
username: username
The Flyway migration:
@Slf4j
@Configuration
public class FlywayConfiguration {
public static final String DB_MIGRATION_BASE = "db/migration/";
private DataSource primaryDataSource;
private DataSource secondaryDataSource;
@Value("${spring.primary-datasource.jdbc-url:localhost}")
private String primaryDatasourceUrl;
@Value("${spring.secondary-datasource.jdbc-url:localhost}")
private String secondaryDatasourceUrl;
@Autowired
public FlywayConfiguration(@Qualifier("primaryDataSource") DataSource primaryDataSource,
@Qualifier("secondaryDataSource") DataSource secondaryDataSource) {
this.primaryDataSource = primaryDataSource;
this.secondaryDataSource = secondaryDataSource;
}
@PostConstruct
public void migrate() {
log.info("Preparing Flyway migration for database with url: " + primaryDatasourceUrl);
migrateDatabase(primaryDataSource, "primary");
log.info("Preparing Flyway migration for database with url: " + secondaryDatasourceUrl);
migrateDatabase(secondaryDataSource, "secondary");
}
/**
* Migrate specific database.
* @param datasource The datasource.
* @param directories The directories containing the migration scripts.
*/
private void migrateDatabase(@NonNull DataSource datasource, @NonNull String... directories) {
for (val directory : directories) {
log.info("Migration scripts source: " + directory);
new Flyway(Flyway.configure()
.dataSource(datasource)
.locations(DB_MIGRATION_BASE + directory))
.migrate();
}
}
}
The directory structure:
Migration script V1.0.0_1__secondary__init.sql
is correctly only applied to the secondary
database.
The issue is that the scripts inside both db/migration/primary
and db/migration/secondary
are being applied to the primary
database, when only the scripts in the primary directory should be being applied.
How do I configure Flyway to only apply the migration scripts in the directory db/migration/primary
to the primary
database?