Skip to content

Latest commit

 

History

History

HibernateSpringBootFlywayMySQLDatabase

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 

How To Migrate MySQL Database Using Flyway - Database Created Via spring.flyway.schemas

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 migrating a MySQL database when the database is created by Flyway via spring.flyway.schemas. In this case, the entities should be annotated with @Table(schema = "bookstoredb") or @Table(catalog = "bookstoredb"). Here, the database name is bookstoredb.

Key points:

  • for Maven, in pom.xml, add the Flyway dependency
  • remove (disable) spring.jpa.hibernate.ddl-auto or set it to validate
  • in application.properties, set the JDBC URL as follows: jdbc:mysql://localhost:3306/
  • in application.properties, add spring.flyway.schemas=bookstoredb, where bookstoredb is the database that should be created by Flyway (feel free to add your own database name)
  • each entity that should be stored in this database should be annotated with, @Table(schema/catalog = "bookstoredb")
  • each SQL file containing the schema update add it in classpath:db/migration
  • each SQL file name it as V1.1__Description.sql, V1.2__Description.sql, ...

Output of migration history example:


If you need a deep dive into the performance recipes exposed in this repository then I am sure that you will love my book "Spring Boot Persistence Best Practices"If you need a hand of tips and illustrations of 100+ Java persistence performance issues then "Java Persistence Performance Illustrated Guide" is for you.