FlywayDB Migration with Spring Boot

What is Flyway DB?

Flyway is an open-source database migration tool that helps you manage and version your database schema changes across all your instances. It supports various databases such as MySQL, Oracle, SQL Server, PostgreSQL, and many more. To learn more about Flyway, you can use the flywaydb. Many software projects use relational databases. This requires the handling of database migrations, also often called schema migrations.

The first and most important practice is not to use “spring.jpa.hibernate.ddl-auto=create/update/create-drop” in production. With these properties, you could update and migrate your database schema with Hibernate directly. This might be a valid option for pet projects, but not for enterprise applications in production as you can’t control the automatic migration process in detail. You also won’t get information about the current database schema version of an environment (e.g. staging, test, dev …), so thats why we can use Flyway to resolve the above mentioned problems. In this blog, we will explore how to use Flyway with Spring Boot in detail.

Configuring Flyway Database :


  • NOTE : For the spring-boot version 3.0.2 and greater, we have to use version 17 for JDK and for JDK17 flyway is not supported so we have to add one extra dependency based on your database, Which is given below :


# Flyway Configuration

Creating Database Migrations :

Flyway requires you to create database migration scripts, under the path db/migration folder specified as per in properties file, with filename in a specific format that includes a version number & descriptive name, for e.g. “V[VERSION_NUMBER]__[NAME].sql” with SQL statements to be executed/validated before starting the server.

For example, let’s say we want to create a table called users in our database. We can create a migration script with the following name “V1__Create_User_Table.sql” with below sql statements :

Name varchar(20)

Running Database Migrations :

Now run the spring-boot application and check into your database, user table is created. The ‘flyway_schema_history’ table is also created, with 1 record “V1__Create_User_Table” executed successfully. This table tracks the changes in DB as per the different versions are created. 

As each migration gets applied, the schema history table is updated accordingly. When the new file is added in flyway it will once again scan the filesystem or the classpath of the application for migrations. The migrations are checked against the schema history table. If their version number is lower or equal to the one of the version marked as current, they are ignored.

NOTE : If you want to change table structure of above table, then you have to create new script file like “V2__Update_User_Table.sql”, we cannot update the “V1__Create_User_Table.sql” script file, it will give error, because it creates hashcode of sql statements and stores in database and compares that hashcode while running spring-boot application, so if you change even one line then it will give error.

Conclusion : 

In conclusion, Flyway is a powerful tool that enables easy database migration management in Spring Boot applications. With its seamless integration with Spring Boot, developers can easily manage database schema changes and updates throughout the application development lifecycle. The use of Flyway simplifies the process of database migration, reduces the risk of data loss, and makes it easy to maintain database versions in a structured manner. By adopting Flyway with Spring Boot, developers can focus on building the core functionality of their application, while Flyway takes care of the database migration process in a simple and efficient way.

References :

GitHub Demo Link :

Reference :


Got a Project Idea? We will make it happen!

We will help you convert your business idea into reality by providing you with the best development services!