title: Spring Boot Flyway applying database migration to wrong database
body: <p>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.</p>
  <p>The primary datasource:</p>
  <pre><code>@Configuration
  @EnableTransactionManagement
  @EnableJpaRepositories(basePackages = {&quot;com.gobsmack.*.repository.primary&quot;},
                         entityManagerFactoryRef = &quot;primaryEntityManagerFactory&quot;,
                         transactionManagerRef = &quot;primaryTransactionManager&quot;
  )
  public class PrimaryDatasourceConfiguration {
  
      @Bean
      @Primary
      @ConfigurationProperties(prefix = &quot;spring.primary-datasource&quot;)
      public DataSource primaryDataSource() {
          return DataSourceBuilder.create().build();
      }
  
      @Primary
      @Bean(name = &quot;primaryEntityManagerFactory&quot;)
      public LocalContainerEntityManagerFactoryBean primaryEntityManagerFactory(EntityManagerFactoryBuilder builder) {
          return builder.dataSource(primaryDataSource())
                  .packages(&quot;com.gobsmack.*.model&quot;)
                  .build();
      }
  
      @Primary
      @Bean
      public PlatformTransactionManager primaryTransactionManager(
              final @Qualifier(&quot;primaryEntityManagerFactory&quot;) LocalContainerEntityManagerFactoryBean primaryEntityManagerFactory) {
          return new JpaTransactionManager(primaryEntityManagerFactory.getObject());
      }
  }
  </code></pre>
  <p>The second datasource:</p>
  <pre><code>@Configuration
  @EnableTransactionManagement
  @EnableJpaRepositories(basePackages = {&quot;com.gobsmack.*.repository.secondary&quot;},
                         entityManagerFactoryRef = &quot;secondaryEntityManagerFactory&quot;,
                         transactionManagerRef = &quot;secondaryTransactionManager&quot;
  )
  public class SecondaryDatasourceConfiguration {
  
      @Bean
      @ConfigurationProperties(prefix = &quot;spring.secondary-datasource&quot;)
      public DataSource secondaryDataSource() {
          return DataSourceBuilder.create().build();
      }
  
      @Bean(name = &quot;secondaryEntityManagerFactory&quot;)
      public LocalContainerEntityManagerFactoryBean secondaryEntityManagerFactory(EntityManagerFactoryBuilder builder) {
          return builder.dataSource(secondaryDataSource())
                  .packages(&quot;com.gobsmack.*.model&quot;)
                  .build();
      }
  
      @Bean
      public PlatformTransactionManager secondaryTransactionManager(
              final @NonNull @Qualifier(&quot;secondaryEntityManagerFactory&quot;) LocalContainerEntityManagerFactoryBean secondaryEntityManagerFactory) {
          return new JpaTransactionManager(secondaryEntityManagerFactory.getObject());
      }
  }
  </code></pre>
  <p>The datasource connection properties:</p>
  <pre><code>spring:
    primary-datasource:
      jdbc-url: jdbc:mysql://localhost:3306/primary?useSSL=false&amp;primary
      password: password
      driver-class-name: com.mysql.cj.jdbc.Driver
      username: username
    secondary-datasource:
      jdbc-url: jdbc:mysql://localhost:3306/secondary?useSSL=false&amp;secondary
      password: password
      driver-class-name: com.mysql.cj.jdbc.Driver
      username: username
  </code></pre>
  <p>The Flyway migration:</p>
  <pre><code>@Slf4j
  @Configuration
  public class FlywayConfiguration {
  
      public static final String DB_MIGRATION_BASE = &quot;db/migration/&quot;;
  
      private DataSource primaryDataSource;
      private DataSource secondaryDataSource;
  
      @Value(&quot;${spring.primary-datasource.jdbc-url:localhost}&quot;)
      private String primaryDatasourceUrl;
  
      @Value(&quot;${spring.secondary-datasource.jdbc-url:localhost}&quot;)
      private String secondaryDatasourceUrl;
  
      @Autowired
      public FlywayConfiguration(@Qualifier(&quot;primaryDataSource&quot;) DataSource primaryDataSource,
                                 @Qualifier(&quot;secondaryDataSource&quot;) DataSource secondaryDataSource) {
          this.primaryDataSource = primaryDataSource;
          this.secondaryDataSource = secondaryDataSource;
      }
  
      @PostConstruct
      public void migrate() {
          log.info(&quot;Preparing Flyway migration for database with url: &quot; + primaryDatasourceUrl);
          migrateDatabase(primaryDataSource, &quot;primary&quot;);
  
          log.info(&quot;Preparing Flyway migration for database with url: &quot; + secondaryDatasourceUrl);
          migrateDatabase(secondaryDataSource, &quot;secondary&quot;);
      }
  
      /**
       * 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(&quot;Migration scripts source: &quot; + directory);
              new Flyway(Flyway.configure()
                               .dataSource(datasource)
                               .locations(DB_MIGRATION_BASE + directory))
                               .migrate();
          }
      }
  }
  </code></pre>
  <p>The directory structure:</p>
  <p><a href="https://i.stack.imgur.com/T0GWR.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/T0GWR.png" alt="enter image description here" /></a></p>
  <p>Migration script <code>V1.0.0_1__secondary__init.sql</code> is correctly only applied to the <code>secondary</code> database.</p>
  <p>The issue is that the scripts inside both <code>db/migration/primary</code> and <code>db/migration/secondary</code> are being applied to the <code>primary</code> database, when only the scripts in the primary directory should be being applied.</p>
  <p>How do I configure Flyway to only apply the migration scripts in the directory  <code>db/migration/primary</code> to the <code>primary</code> database?</p>