![]() |
Flyway is a powerful database migration tool. It simplifies and automates the process of managing database schema changes. Flyway follows the simple principle of versioning the database schema using SQL scripts. These scripts are organized in a specific folder structure and executed in a sequential order. In this article, we will learn how to use Flyway with JDBC for effective database migration. Prerequisites:
Step-by-Step Implementation of Flyway Database Migration ToolBelow are the steps to use Flyway with JDBC for database migration. Step 1: Directory StructureCreate a directory named db/migration in the project. This is where Flyway will look for migration scripts. project
└── db
└── migration
└── V1__Initial_Script.sql
Step 2: Migration Script NamingMigration scripts follow a naming convention. They start with a prefix V, followed by the version number, double underscores, and then a description. For example, V1__Initial_Script.sql. Step 3: Configuration of FlywayCreate a Flyway configuration file named flyway.conf to specify database connection details and other configurations. flyway.url=jdbc:mysql://localhost:3306/your_database
flyway.user=your_username
flyway.password=your_password
Now, let us configure Flyway with JDBC in the application. This can be configured by the file named flyway.conf. Java
In this, it uses Flyway and a database migration tool to perform migrations on a MySQL database. It connects to the database using a specific URL, username, and password, then runs the migrations defined in the Flyway configuration. The Step 4: Migration ScriptNow, create a simple migration script: V1__Create_Table.sql: CREATE TABLE users (
id INT PRIMARY KEY,
username VARCHAR(50) NOT NULL,
email VARCHAR(100) NOT NULL
);
Step 5: Run Flyway MigrationOpen a terminal and run the following command to execute the migration: flyway migrate
Flyway will automatically detect and execute the SQL script, updating the database schema. |
Reffered: https://www.geeksforgeeks.org
Java |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 14 |