-
Notifications
You must be signed in to change notification settings - Fork 1
FLYWAY
Flyway is an open-source database migration tool that simplifies the process of managing changes to database schema and data over time. It allows developers to version control their database schema and easily migrate between different versions of the schema.
With Flyway, developers can define and execute database migrations as a series of SQL scripts, which can be versioned and managed in a source code repository along with the application code. Flyway tracks which migrations have been applied to the database and ensures that they are executed in the correct order, making it easy to apply incremental changes to a database over time.
Flyway supports multiple database types, including Oracle, MySQL, PostgreSQL, SQL Server, and others. It integrates with popular build tools and CI/CD pipelines, and can be easily configured and customized to meet specific requirements.
Overall, Flyway provides an efficient and reliable way to manage database changes and keep track of the schema history, making it a valuable tool for software development teams.
_Spring Boot _ provides easy integration with Flyway which is very good to start the data migration quickly
Flyway supports many databases and some of them are
- Postgres
- MySql
- Oracle
- DB2
- H2
- MariaDB
It also supports cloud-based database providers like
- Amazon RDS
- Google Cloud SQL
- Heroku
Dependencies
Flyway library can be added to the Spring Boot application using the following
implementation group: 'org.flywaydb', name: 'flyway-mysql', version: '9.16.1'
Spring Boot will then automatically auto-wire Flyway with its DataSource and invoke it on startup.
Flyway will look for scripts under the path db/migration folder by default.
The naming convention for all the migration scripts is
V[VERSION_NUMBER__[NAME].sql
### Migration Scripts
Having seen what is Flyway and the dependencies needed to set up, let us see how to write the migration scripts using Flyway
Let us create the following create table script in a file called V1.0__create_table.sql to create employee table. We have to place this file under the path src/main/resources/db/migration/V1.0_create_table.sql
CREATE TABLE employee (
id bigint NOT NULL AUTO_INCREMENT PRIMARY KEY ,
email_address varchar(255) NOT NULL,
first_name varchar(255) NOT NULL,
last_name varchar(255) NOT NULL
);
CREATE TABLE todos (
id varchar(255) NOT NULL PRIMARY KEY,
completed bit(1),
order_number int,
title varchar(255) NOT NULL
);
When we run the spring boot application using java -jar build/libs/Spring-Boot-Starter-0.0.1-SNAPSHOT.jar, flyway creates 3 tables namely
- employee
- todos
- flyway_schema_history
flyway_schema_history table is used by Flyway to keep track of the migration versions, script file name, checksum, etc. If we make any change to the V1.0__create_table.sql after the migration is completed, Flyway will check for the checksum and throw an error
So any changes to the employee table must be done in the new migration script called V1.1_modify_emp_table.sql
ALTER TABLE employee ADD COLUMN mobileNumber int;
Now when we restart our Spring Boot application, the current migration version will be Version 2 in the flyway_schema_history table as shown below
Flyway commands
Flyway provides the following 8 commands to support various manual operations
- Migrate
Migrates the schema to the latest version. Flyway will create the schema history table automatically if it doesn’t exist.
- Clean
Drops all objects (tables, views, procedures, triggers, …) in the configured schemas.
- Info
Prints the details and status information about all the migrations.
- Validate
Validate applied migrations against resolved ones (on the filesystem or classpath) to detect accidental changes that may prevent the schema(s) from being recreated exactly.
- Undo
Undoes the most recently applied versioned migration.
- Baseline
Baselines an existing database, excluding all migrations up to and including baseline version
- Repair
Repairs the Flyway schema history table.
- Check check command produces reports to increase confidence in your migrations. Flyway migrates against a temporary database and compares this against the target database in order to generate a report.
https://javarevisited.blogspot.com/2019/03/top-5-course-to-learn-apache-maven-for.html#axzz7xEnFMpLP