Flyway is an open-source database migration tool that helps manage and automate the process of applying changes to a database schema. It provides a structured way to track, version, and execute database migrations, ensuring that any changes applied to the database are consistent across different environments.

Flyway is not specific to Spring Boot. It can be integrated into various Java applications to manage and automate database changes.However, Spring Boot offers easy integration with Flyway by automatically executing migration scripts at application startup when Flyway is included in the project configuration. This makes it particularly convenient for developers using Spring Boot.

Use of Flyway

Flyway is primarily used for:

  • Version Control: Keeping track of database schema changes over time.
  • Automation: Automatically applying database changes as part of your deployment process.
  • Consistency: Ensuring that all environments (development, testing, production) have the same schema.

When to Use Flyway

We should use Flyway when:

  • We need to manage complex database schemas that are frequently updated.
  • Our team collaborates on a project, and we need to ensure everyone is using the same database version.
  • We want to automate the deployment of database changes as part of a continuous integration/continuous deployment (CI/CD) pipeline.

How to Use Flyway in a Spring Boot Project

Maven Configuration

To integrate Flyway into a Spring Boot project using Maven, we need to add the Flyway Maven plugin to our pom.xml file:

<project>
    ...
    <build>
        <plugins>
            <plugin>
                <groupId>org.flywaydb</groupId>
                <artifactId>flyway-maven-plugin</artifactId>
                <version>11.2.0</version>
            </plugin>
        </plugins>
    </build>
</project>
  • groupId and artifactId: Identify the Flyway Maven plugin.
  • version: Specifies the version of Flyway to use.

Spring Boot Configuration

Spring Boot automatically integrates with Flyway if it’s on the classpath. For doing that, we could/should configure it in the application.properties file:

spring.datasource.url=jdbc:mysql://localhost:3306/mydb
spring.datasource.username=myuser
spring.datasource.password=mypassword
spring.flyway.enabled=true
  • spring.datasource.url: Specifies the database connection URL.
  • spring.datasource.username and password: Database credentials.
  • spring.flyway.enabled: Enables Flyway for the application.

Creating Migration Scripts

Migration scripts should be placed in the src/main/resources/db/migration directory. Each script should be named according to the Flyway naming convention, such as V1__Initial_setup.sql.

Example of a migration script (V1__Initial_setup.sql):

CREATE TABLE example_table (
    id INT PRIMARY KEY,
    name VARCHAR(100)
);
  • V1__Initial_setup.sql: The file name indicates it is the first version of the migration.
  • CREATE TABLE: SQL statement to create a new table.

Running Migrations

To apply migrations, let us run the Flyway Maven plugin:

mvn flyway:migrate
  • mvn flyway:migrate: Executes all pending migrations against the database.

Example Project Structure

Real Case Example with Flyway

Let’s consider a scenario where we need to manage versioning for a database schema in a Spring Boot application using Flyway. We will also explore how to perform a rollback.

Adding Versioned Migrations

Let us create a directory src/main/resources/db/migration and add migration scripts. Each script should be prefixed with a version number.

Version 1: Initial Setup

Create V1__Initial_setup.sql:

CREATE TABLE employee (
    id INT PRIMARY KEY,
    name VARCHAR(100),
    department VARCHAR(50)
);
Version 2: Add Column

Create V2__Add_email_to_employee.sql:

ALTER TABLE employee ADD email VARCHAR(100);
Applying Migration
mvn flyway:migrate

This command will apply all pending migrations to the database.

Rollback Strategy

Flyway does not support automatic rollbacks. However, we can manually create a rollback script.

Rollback Version 2

Create U2__Remove_email_from_employee.sql:

ALTER TABLE employee DROP COLUMN email;

this script removes the email column from the employee table. This action effectively reverses the change made in the migration that added the email column.

To rollback, we could manually run the rollback script using the database client or create a custom Maven command.

Example Rollback Execution

To simulate a rollback, we can manually execute the rollback script:

mvn flyway:migrate -Dflyway.sqlMigrationPrefix=U

This command will apply rollback scripts prefixed with U.

The prefix U in the Flyway migration command is used to specify rollback scripts. In Flyway, migration scripts typically have a prefix like V for versioned migrations. By using the prefix U, we indicate that these scripts are meant to undo or rollback changes made by corresponding V scripts.

PS: It is not obligatory to use the prefix U for undo scripts in Flyway. The prefix is configurable, and we can choose any prefix that suits our naming convention. The default prefix for versioned migrations is V, but for undo scripts, we can define a different prefix (like U) using the flyway.sqlMigrationPrefix property when executing the command. This allows flexibility in organizing and identifying our scripts.

Conclusion

Flyway is a powerful tool for managing database migrations in Spring Boot applications. By integrating Flyway, we can ensure that our database schema is versioned, consistent, and easily manageable across different environments.

By Shabazz

Software Engineer, MCSD, Web developer & Angular specialist

Leave a Reply

Your email address will not be published. Required fields are marked *