Flyway for Java: Complete Guide with Examples

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Introduction to Flyway: What It Is and Why You Need It

\n\n

Flyway is a library for managing database versioning and migrations (Database Migration Tool). In modern software development, especially in team work and when using CI/CD methodologies, there is a critical need to synchronize the database structure between developers, test, and production environments. Flyway solves this problem by allowing you to describe database schema changes (creating tables, adding columns, indexes, triggers, etc.) as sequential, versioned SQL scripts or Java classes.

\n\n

The core concept of Flyway is simple: each migration has a unique version number (e.g., V1, V2, V3) and is executed exactly once. The library tracks which migrations have already been applied to a specific database using a special table (by default flyway_schema_history). This guarantees idempotency and reproducibility of the database state.

\n\n

Why use Flyway?

\n
    \n
  • Schema versioning: Each database change is a separate version that can be rolled back or applied.
  • \n
  • Automation: Integration with Maven, Gradle, Spring Boot, and CI/CD pipelines.
  • \n
  • Integrity control: Flyway checks checksums of migrations, preventing accidental changes to already executed scripts.
  • \n
  • Simplicity: For most tasks, writing a simple SQL script is sufficient.
  • \n
\n\n

Installing Flyway for Java

\n\n

Flyway is not installed via pip or npm, as it is a Java library. There are several ways to add it to a project:

\n\n

1. Maven (pom.xml)

\n
<dependency>\n    <groupId>org.flywaydb</groupId>\n    <artifactId>flyway-core</artifactId>\n    <version>9.22.3</version> <!-- Current version at the time of writing -->\n</dependency>\n\n<!-- Driver for your database, e.g., PostgreSQL -->\n<dependency>\n    <groupId>org.postgresql</groupId>\n    <artifactId>postgresql</artifactId>\n    <version>42.7.1</version>\n</dependency>
\n\n

2. Gradle (build.gradle)

\n
implementation 'org.flywaydb:flyway-core:9.22.3'\nimplementation 'org.postgresql:postgresql:42.7.1'
\n\n

3. Maven Plugin (for command-line execution)

\n
<plugin>\n    <groupId>org.flywaydb</groupId>\n    <artifactId>flyway-maven-plugin</artifactId>\n    <version>9.22.3</version>\n    <configuration>\n        <url>jdbc:postgresql://localhost:5432/mydb</url>\n        <user>myuser</user>\n        <password>mypassword</password>\n    </configuration>\n</plugin>
\n\n

4. Spring Boot (auto-configuration)

\n

If you are using Spring Boot, simply add the dependency and configure parameters in application.properties:

\n
spring.flyway.enabled=true\nspring.flyway.locations=classpath:db/migration\nspring.datasource.url=jdbc:postgresql://localhost:5432/mydb\nspring.datasource.username=myuser\nspring.datasource.password=mypassword
\n\n

Quick Start with Code Example

\n\n

Let's look at a minimal example of using Flyway in a Java application to create a users table.

\n\n

Step 1: Create the migration directory structure

\n

By default, Flyway looks for SQL scripts in the db/migration folder in the classpath (e.g., src/main/resources/db/migration).

\n\n

Step 2: Create a migration file

\n

Create a file V1__Create_users_table.sql in this folder:

\n
CREATE TABLE users (\n    id SERIAL PRIMARY KEY,\n    username VARCHAR(50) NOT NULL UNIQUE,\n    email VARCHAR(10                    

Recommendations