You are currently viewing Effortless Database Migrations in Spring Boot with Flyway

Effortless Database Migrations in Spring Boot with Flyway

Flyway is a database migration tool that helps manage database changes in a structured and version-controlled way. Spring Boot, on the other hand, is a popular framework for building Java-based, production-grade applications.

Prerequisites:

  • Basic knowledge of Spring Boot.
  • Familiarity with relational databases (e.g., MySQL, PostgreSQL).

Step 1: Create a Spring Boot Project

You can create a Spring Boot project using Spring Initializer or your preferred IDE. Include the dependencies for “Spring Web” and the database you plan to use (e.g., “Spring Data JPA,” “H2 Database,” or others).

Step 2: Configure Database Properties

Open the application.properties or application.yml file in the src/main/resources directory and configure your database connection properties. For example:

spring.datasource.url=jdbc:mysql://localhost:3306/your_database
spring.datasource.username=your_username
spring.datasource.password=your_password
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.hibernate.ddl-auto=none

Step 3: Add Flyway Dependency

In your pom.xml (if using Maven) or build.gradle (if using Gradle), add the Flyway dependency:

Maven:

<dependency>
    <groupId>org.flywaydb</groupId>
    <artifactId>flyway-core</artifactId>
</dependency>

Gradle:

implementation 'org.flywaydb:flyway-core'

Step 4: Create Database Migrations

Create a folder named db/migration inside the src/main/resources directory. Flyway will automatically look for SQL scripts in this folder and apply them in order.

For example, create a file named V1__create_table.sql:

CREATE TABLE IF NOT EXISTS user (
    id INT AUTO_INCREMENT PRIMARY KEY,
    username VARCHAR(50) NOT NULL,
    email VARCHAR(100) NOT NULL
);

Step 5: Enable Flyway in Spring Boot

Create a configuration class to enable Flyway. For example:

This configuration ensures that Flyway cleans the database and applies migrations during the application startup.

Step 6: Create a Simple Entity and Repository

Create a simple entity class, for example, User.java:

Create a repository interface, for example, UserRepository.java:

Step 7: Use the Repository in a Service or Controller

Create a service or controller to interact with the database using the UserRepository. For example:

Step 8: Run the Application

Run your Spring Boot application, and Flyway will automatically apply the database migrations during startup. You should see log entries indicating that Flyway is executing your migration scripts.

Now you have a Spring Boot application integrated with Flyway for database migration. As you make changes to your database schema, add new migration scripts following the Flyway naming convention (e.g., V2__alter_table.sql), and Flyway will manage the versioning and apply them in order.

This is a basic example, and you can explore more advanced features of Flyway, such as repeatable migrations and callbacks, based on your application’s requirements.

Leave a Reply