Flyway in Spring Boot and Gradle: flyway-core vs Flyway Gradle Plugin

 Have you ever wondered about the best way to integrate Flyway into your Gradle project? Let me break it down into two main approaches: using Flyway as a dependency and applying the Flyway plugin.

The Dependency Approach: implementation 'org.flywaydb:flyway-core'

Adding Flyway as a dependency essentially embeds it within your application. Think of it as equipping your application with a built-in toolbox to handle database changes during runtime. This approach is ideal when you want your application to take charge of migrations.

When to Use It

  • Your application needs to manage database changes while it’s running.
  • You’re working with a Spring Boot application that integrates seamlessly with Flyway.

Key Features

Spring Boot Auto-Configuration: Spring Boot automatically detects the Flyway dependency and configures it. You can customize Flyway’s behavior using the application.properties file, like this:


spring.flyway.url=jdbc:mysql://localhost:3306/mydb
spring.flyway.user=root
spring.flyway.password=secret
spring.flyway.locations=classpath:/db/migration

            

The Plugin Approach: apply plugin: 'org.flywaydb.flyway'

Applying the Flyway plugin shifts the responsibility for database management to Gradle, enabling you to run migrations independently of your application. It’s like delegating the task to a dedicated tool that operates as part of your build process.

When to Use It

  • You want to perform database migrations during the build process.
  • You’re setting up databases across multiple environments before deployment.
  • Your CI/CD pipeline needs to manage schema changes without starting the application.

Key Features

Gradle Tasks: The plugin adds powerful tasks such as flywayMigrate, flywayInfo, flywayValidate, and more. You can configure it in your build.gradle file like this:


plugins {
    id 'org.flywaydb.flyway' version '9.20.1'
}

flyway {
    url = 'jdbc:mysql://localhost:3306/mydb'
    user = 'root'
    password = 'secret'
    locations = ['filesystem:src/main/resources/db/migration']
}

            

Sample Commands

  • ./gradlew flywayMigrate — Run migrations to update the database.
  • ./gradlew flywayInfo — View the migration status.
  • ./gradlew flywayValidate — Validate applied migrations against available ones.

A Helpful Tip

While the Flyway plugin includes the flyway-core library, it’s only available during the build process. If your application needs to perform runtime migrations, you’ll need to explicitly declare the flyway-core dependency in your dependencies block.

Choosing the Right Approach

Dependency Approach:

  • Use when your application manages database changes during runtime.
  • Perfect for Spring Boot applications requiring embedded migration capabilities.

Plugin Approach:

  • Use when migrations are part of your build or deployment pipeline.
  • Ideal for managing databases across multiple environments.

By understanding the differences, you can pick the approach that best fits your project. Whether your goal is runtime migrations or managing schemas as part of the build process, Flyway provides the tools to streamline your workflow.

Comments

Popular posts from this blog

MDK Hospital opened in Horana

Disable single quote Within Forms

My Docker learning short notes