Building Warehouse Management Software: A...
November 18, 2024
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.
<dependency>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-core</artifactId>
</dependency>
<dependency>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-mysql</artifactId>
</dependency>
# Flyway Configuration
spring.flyway.baseline-on-migrate=true
spring.flyway.enabled=true
spring.flyway.locations=classpath:db/migration
spring.jpa.hibernate.ddl-auto=validate
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)
);
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.
NOTE : If you want to change table structure of above table, then you have to create new script file like “V2__Update_User_Table.sql”, we cannot update the “V1__Create_User_Table.sql” script file, it will give error, because it creates hashcode of sql statements and stores in database and compares that hashcode while running spring-boot application, so if you change even one line then it will give error.
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.
GitHub Demo Link : https://github.com/Ritish34/flayway
Reference :
https://www.tutorialspoint.com/spring_boot/spring_boot_flyway_database.htm
https://www.educba.com/spring-boot-flyway
https://www.section.io/engineering-education/spring-boot-flyway-migrations