In this tutorial, you will learn using Flyway to version control database migration/evolution. Let's start building an example with Spring Boot, JPA/Hibernate, MySQL, and Docker.
What you will need
- Docker CE 18+
Project structure
Project dependencies
Define JPA Entity and Repository
Prepare SQL script for database migration
Application Properties
Run the application
Prepare Dockerfile for Java/Spring Boot application
Start the application and infrastructure via Docker Compose. Make sure the local Docker engine is running and terminal console is at the springboot-examples project root directory
docker-compose -f docker-compose-jpa.yaml up --renew-anon-volumes db-migration-flyway
Access to MySQL Server docker container by issuing below bash command and key in hellokoding
on Enter password:
docker exec -it hk-mysql mysql -p
Check the created schema and data by Flyway based on the prepared SQL scripts
Flyway also produced an additional table flyway_schema_history
to manage schema version history
Keys to take away
- Using Flyway to version control database migration/evolution with SQL scripts.
- Hibernate DDL Auto should be disable when using with Flyway or on production environment, on application.properties,
spring.jpa.hibernate.ddl-auto
value should bevalidate
ornone
(default). - By default, Spring Boot and Flyway will find the migration script in
resources/db/migration
, you can change it on application.properties withspring.flyway.locations
property. - SQL script file name should follow Flyway convention https://flywaydb.org/documentation/migrations#naming
That's it folks! Thanks for joining and have a happy coding. Full source code of this example is available as below
Source code
https://github.com/hellokoding/springboot-examples/tree/master/jpa-hibernate/db-migration-flyway