Using Flyway in Spring Boot

I introduced you to Flyway to implement database migration. For Spring Boot applications that want to use Flyway, this integration is easy because Spring Boot already supports it. How is it in detail? Let’s find out together in this tutorial!

First, I will create a new Spring Boot project with Flyway, Spring Data JDBC, and PostgreSQL dependencies as follows:

Result:

If you check the src/main/resources folder in the newly created project, you will see there is a folder called db/migration. This is the default folder where we will store SQL files for Spring Boot with Flyway to do database migration, guys!

Now, as an example, I will create a new SQL file V2021.10.20.00000__Init.sql located in the src/main/resources/db/migration directory:

to create a new student table as follows:

The next thing we need to do is configure the Datasource in the application.properties file, our example is as follows:

with flyway_example being my example database name.

Now, if you run the application and access the above database, you will see the following results:

The flyway_schema_history table and also the student table that we defined in the SQL file have been created.

As you can see, using Flyway in a Spring Boot application is very simple, and doesn’t take much effort, right?

Add Comment