I want to use this Spring properties file for database configuration.
spring.jmx.enabled=false
spring.datasource.jndi-name=java:/global/production
spring.datasource.driver-class-name=org.mariadb.jdbc.Driver
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MariaDBDialect
spring.jpa.show-sql = true
spring.jpa.hibernate.ddl-auto = update
request.limit=300000
spring.flyway.baselineOnMigrate = true
spring.flyway.locations=classpath:/db/migration
The code works well but unfortunately when database is empty after all tables are deleted I get SQL errors because migration script is executed first.
How I can configure JPA first to create tables and then flyway to execute migration script?
Flyway migration run before hibernate execution. You can do that by seeting boot order. For this you need to add this configuration class:
import org.flywaydb.core.Flyway;
import
org.springframework.boot.autoconfigure.flyway.FlywayMigrationInitializer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;
@Configuration
public class MigrationConfiguration {
/**
* Override default flyway initializer to do nothing
*/
@Bean
FlywayMigrationInitializer flywayInitializer(Flyway flyway) {
return new FlywayMigrationInitializer(flyway, (f) ->{} );
}
/**
* Create a second flyway initializer to run after jpa has created the schema
*/
@Bean
@DependsOn("entityManagerFactory")
FlywayMigrationInitializer delayedFlywayInitializer(Flyway flyway) {
return new FlywayMigrationInitializer(flyway, null);
}
}
Look at this answer you will get details.