Updated March 15, 2023
Introduction to Spring Boot Flyway
Spring boot flyway is nothing but the application of version control, which was easily involved in our database schema in all instances of the database. Spring boot flyway is application software of database migration which was used to migrate, validate, undo, clean, repair, and baseline commands of SQL. Flyway framework is basically used to update the database version using migration tools. Spring boot flyway is the tool that allows incremental changes of version control in our database, so we can easily remodel our database into the new version.
What is a spring boot flyway?
- Using the spring boot flyway framework, we can easily remodel our database schema into a new version of the database.
- We can write an SQL migration script by using the syntax of database-specific. Also, we can write the code in java language by using database transformation of advanced java.
- We can say that database migration can be repeatable or it is versioned. The former contains the one unique version, and it is applied once.
- At the time single migration run, repeated migration is always applied at last after executing the pending migration version.
- Migration of repeatable is always applied at the time of their order description. To migrate using single migration, all database statement of database migration is run in a single database transaction.
- To use the spring boot flyway in our project, we need to add the flyway maven plugin in the pom.xml file of our project.
- The latest version of the flyway maven plugin is 8.0.0. Therefore, we can configure the flyway maven plugin in different ways.
- We can configure the flyway plugin are as follows.
- Configure the flyway plugin using the configuration tag.
- Configure the flyway plugin by using the configuration properties of maven.
- Configure the flyway plugin by using an external configuration file.
- Configure the flyway plugin by using system properties.
- We can configure the flyway plugin in our project by using the configuration tag in our definition file as pom.xml.
- We can also configure the flyway plugin in our application by specifying the configuration properties in the file name as pom.xml.
- Another way to configure the flyway plugin in our project is to create another configuration file and give a “.conf” extension to that file.
Database Spring Boot Flyway Project
- To setup a database flyway project, first, we need to create a project template using spring initializer and need to add the following dependency to it.
- Spring boot starter web
- JDBC API
- PostgreSQL driver
- Spring web
- In the below step, we provide the project group name as com. Examples include artifact name as spring-boot-flyway, project name as spring-boot-flyway, package as a jar file, and selecting java version as 11.
- We also select the dependencies as spring web, flyway migration, PostgreSQL driver, and JDBC API.
- After creating the project, we are opening the same using the spring tool suite are as follows.
- After opening the project in the spring tool suite, we have already added the dependency at the time of creating the template, so we do not need to add the same one again.
- After adding a dependency, we need to create an application.properties and application.yml file for the project; in this file, we have defined the database configuration.
- Then we have created the SQL script in the migration directory. After creating a script, we have to create the main class file of our project.
- After creating the main class file, we need to run our application. After the successful run, we can see all the statement is executed in a database which was we have created in an SQL file.
Spring Boot Flyway Examples
- To configure the flyway project example, we also need to add dependency in our project; below are the steps to create a flyway project example.
- Create project template using spring initializer –
Group – com.example
Artifact name – spring-boot- flyway
Name – spring-boot- flyway
Description - Project of spring-boot- flyway
Package name - com.example.spring-boot- flyway
Packaging – Jar
Java – 11
Dependencies – spring web, flyway migration, PostgreSQL driver, JDBC API
- After generating project extract files and open this project by using the spring tool suite
- After opening the project using the spring tool suite, check the project and its files.
- Add dependency
Code –
<dependency> -- Start of dependency tag.
<groupId>org.springframework.boot</groupId> -- Start and end of groupId tag.
<artifactId>spring-boot-starter-web</artifactId> -- Start and end of artifactId tag.
</dependency> -- End of dependency tag.
<dependency> -- Start of dependency tag.
<groupId>org.flywaydb</groupId> -- Start and end of groupId tag.
<artifactId>flyway-core</artifactId> -- Start and end of artifactId tag.
</dependency> -- End of dependency tag.
<dependency> -- Start of dependency tag.
<groupId>org.postgresql</groupId> -- Start and end of groupId tag.
<artifactId>postgresql </artifactId> -- Start and end of artifactId tag.
</dependency> -- End of dependency tag.
- Configure the application.properties file
Code –
spring.datasource.url = jdbc:postgresql://localhost:5432/springbootdb?createDatabaseIfNotExist=true
spring.datasource.username = postgres
spring.datasource.password = postgres
spring.datasource.platform = postgresql
spring.jpa.hibernate.ddl-auto = update
- Configure the application.yml file
Code –
spring:
application:
name: spring-boot-flyway
datasource:
driverClassName : com.postgresql.jdbc.Driver
url: "jdbc:postgresql://localhost:5432/springbootdb"
password: "postgres"
username: "postgres"
- Create an SQL file in the migration directory
Code –
CREATE TABLE springbootflyway (stud_id int, stud_name varchar(10), address varchar(10));
INSERT INTO springbootflyway (stud_id, stud_name, address) VALUES (1, 'ABC', ‘Pune’);
- Create the main class for the flyway application
Code –
@SpringBootTest
class SpringBootFlywayApplicationTests {
public static void main /* main method of spring boot flyway application */ (String[] args) {
SpringApplication.run (SpringBootFlywayApplicationTests.class, args);
}
}
- Run the application –
- Check the table is created, and data was inserted into the database
After successfully running the application check, the springbootflyway table is created into the database, and data is inserted into the same table.
Code:
# select * from springbootflyway;
Conclusion
Spring boot flyway framework is basically used to update database versions using migration tools. We can write a flyway database migration script using SQL or a java programming language. Flyway is the tool which was allowing incremental changes of version control in our database.
Recommended Articles
This is a guide to the spring boot flyway. Here we discuss What is a spring boot flyway along with the examples and codes. You may also have a look at the following articles to learn more –