Don't let database migration headaches slow you down. Keep your database changes organized and under control with Flyway and Spring Boot.
Size: 141.69 KB
Language: en
Added: Apr 17, 2023
Slides: 4 pages
Slide Content
FlywayDB Migration with Spring Boot
What is Flyway DB?
Flyway is an open-source database migration tool that helps you manage and version
your database schema changes across all your instances. It supports various
databases such as MySQL, Oracle, SQL Server, PostgreSQL, and many more. To learn
more about Flyway, you can use the flywaydb. Many software projects use relational
databases. This requires the handling of database migrations, also often called schema
migrations.
The first and most important practice is not to use “spring.jpa.hibernate.ddl-
auto=create/update/create-drop” in production. With these properties, you could update
and migrate your database schema with Hibernate directly. This might be a valid option
for pet projects, but not for enterprise applications in production as you can’t control the
automatic migration process in detail. You also won’t get information about the current
database schema version of an environment (e.g. staging, test, dev …), so thats why we
can use Flyway to resolve the above mentioned problems. In this blog, we will explore
how to use Flyway with Spring Boot in detail.
Configuring Flyway Database :
• Add below flyway-core dependancy to enable FlywayDB in springboot application
:
<dependency>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-core</artifactId>
</dependency>
• NOTE : For the spring-boot version 3.0.2 and greater, we have to use version 17
for JDK and for JDK17 flyway is not supported so we have to add one extra
dependency based on your database, Which is given below :
• It is based on just 7 basic commands:Migrate,Clean,Info,Validate,Undo,Baseline
andRepair (This is not supported in community version).
• Add below properties into the “application.properties” file :
Creating Database Migrations :
Flyway requires you to create database migration scripts, under the path db/migration
folder specified as per in properties file, with filename in a specific format that includes
a version number & descriptive name, for e.g. “V[VERSION_NUMBER]__[NAME].sql” with
SQL statements to be executed/validated before starting the server.
For example, let’s say we want to create a table called users in our database. We can
create a migration script with the following name “V1__Create_User_Table.sql” with
below sql statements :
CREATE TABLE USERS (
ID INT AUTO_INCREMENT PRIMARY KEY,
USERID int,
Name varchar(20)
);
Running Database Migrations :
Now run the spring-boot application and check into your database, user table is created.
The ‘flyway_schema_history’ table is also created, with 1 record
“V1__Create_User_Table” executed successfully. This table tracks the changes in DB as
per the different versions are created.
As each migration gets applied, the schema history table is updated accordingly. When
the new file is added in flyway it will once again scan the filesystem or the classpath of
the application for migrations. The migrations are checked against the schema history
table. If their version number is lower or equal to the one of the version marked as
current, they are ignored.
Conclusion :
In conclusion, Flyway is a powerful tool that enables easy database migration
management in Spring Boot applications. With its seamless integration with Spring
Boot, developers can easily manage database schema changes and updates
throughout the application development lifecycle. The use of Flyway simplifies the
process of database migration, reduces the risk of data loss, and makes it easy to
maintain database versions in a structured manner. By adopting Flyway with Spring
Boot, developers can focus on building the core functionality of their application, while
Flyway takes care of the database migration process in a simple and efficient way.
Originally published by: FlywayDB Migration with Spring Boot