Flyway and Spring Boot integration
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Flyway is a powerful tool for managing database schema migrations and version control, and its integration with Spring Boot streamlines database handling for Java applications. This article delves into the integration of Flyway with Spring Boot, ensuring your project's database evolves correctly with application updates.
What is Flyway?
Flyway is an open-source database version control tool that provides a structured and repeatable process for migrating databases. It supports multiple databases and is language-agnostic, thus supporting complex database structures across different environments.
Spring Boot and Flyway Integration
Spring Boot automatically integrates with Flyway as long as it detects the flyway-core
dependency in your pom.xml
or build.gradle
file. Let’s go over the steps to set up Flyway with a Spring Boot project:
Adding Flyway Dependency
To enable Flyway in a Spring Boot application, you need to include the flyway-core
dependency. Here's how you can add it to your project:
Maven
Add the following dependency:
- Datasource Configuration: It's important that your datasource settings point to the correct database.
- Location Configuration: By default, Flyway looks for migration scripts in the
classpath:db/migrationdirectory. - Baseline on Migrate: This property is critical for projects with a pre-existing database environment to manage initial baseline versioning.
V1__initial_setup.sqlV2__add_user_table.sql- Callbacks: Hooks for pre or post-processing actions (e.g.,
beforeMigrate,afterMigrate). - Schemas: Specify multiple schemas for database targeting.
- Placeholders: Template values within scripts, e.g.,
$\{tablePrefix\}. - Seamless Integration: Direct integration with Spring Boot's lifecycle without additional configuration.
- Automation: Flyway automatically applies database changes, reducing manual effort.
- Version Control: Each migration is versioned, ensuring you can trace changes.
- Reproducibility: Enables consistent database setups across various environments, from development to production.
- Learning Curve: For teams new to database migrations, adopting Flyway might require initial training.
- Complexity in Large Projects: Managing numerous scripts can become cumbersome over time without structured practices.
Related reading
- Flyway Found non-empty schemas public without schema history table Use baseline - on Empty database
- Flyway repair with Spring Boot
- flyway Unsupported Database MySQL 8.0
- FMDBBlockSQLiteCallBackFunction Crash in app that's not using makeFunctionNamed
- For a boolean field, what is the naming convention for its getter/setter?
- For Spring Boot 1.2.3, how to set ignore null value in JSON serialization?
- For homebrew mysql installs, where''s my.cnf?
- For loop example in MySQL

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.