How To Auto-Create And Migrate Two Databases In MySQL Using Flyway
Note: For production, don't rely on hibernate.ddl-auto
(or counterparts) to export schema DDL to the database. Simply remove (disable) hibernate.ddl-auto
or set it to validate
. Rely on Flyway or Liquibase.
Description: This application is an example of auto-creating and migrating two databases in MySQL using Flyway. In addition, each data source uses its own HikariCP connection pool. In case of MySQL, where a database is the same thing with schema, we create two databases, authorsdb
and booksdb
.
Key points:
- for Maven, in
pom.xml
, add the Flyway dependency - remove (disable)
spring.jpa.hibernate.ddl-auto
or set it tovalidate
- in
application.properties
, configure the JDBC URL forbooksdb
asjdbc:mysql://localhost:3306/booksdb?createDatabaseIfNotExist=true
and forauthorsdb
asjdbc:mysql://localhost:3306/authorsdb?createDatabaseIfNotExist=true
- in
application.properties
, setspring.flyway.enabled=false
to disable default behavior - programmatically create two
DataSource
, one forbooksdb
and one forauthorsdb
- programmatically create two
FlywayDataSource
, one forbooksdb
and one forauthorsdb
- programmatically create two
EntityManagerFactory
, one forbooksdb
and one forauthorsdb
- for
booksdb
, place the migration SQLs files indb\migration\booksdb
- for
authorsdb
, place the migration SQLs files indb\migration\authorsdb