Skip to main content

Flyway Integration with Spring Boot

This post talks about how we integrated Flyway with Spring Boot and enjoyed the power of database migration seamlessly. If you have worked on Enterprise applications, data migration is really liability for your project. One wrong move, your data get lost during your production patch fixes or release updates. To mitigate such issues happening over manual migrations or even in-house programmatic data migration, we adopted Flyway, an open-source database migration tool that favors simplicity and convention over configuration. Luckily, Spring Boot naturally gels with Flyway.

Flyway               : 4.2.0
Spring Boot      : 1.5.6.RELEASE
Assumption      : Java, MySQL & Maven Used Projects

Flyway Maven Dependency
Flyway has Maven dependency that we need to specify in the pom.xml while we configure spring-boot-starter dependencies. This makes sure Spring Boot look for database migration scripts in the classpath. The default lookout path is src/main/resources/db/migration and the scripts are named as per Flyway convention. You can find more about Flyway from this and this.

Spring Boot in Action
Now that Flyway is in the classpath along with the migration scripts, Spring Boot really wants to see if the migration is necessary while starting to boot the application. So its important that we configure our datasource for connecting, and migrating the scripts. The datasource details can be configured pro-grammatically or in the application.properties file. We can choose any approach as it deems fit. You can find more about Spring Boot Flyway migration from this.

Issue on Database Creation
One of the issues or convention we used to follow in Application Development is that we create database schema even before Flyway data migration. This is just to ensure data source holds the schema to connect and apply the Flyway migrations. But this seemed to cause annoying application failures while booting since we may have missed to create the database in any development environment. As a result, it took quite a lot of time to understand the cause. Flyway, out of the box, provides support to create schema while migration itself.

Spring Boot Flyway Configuration
Flyway uses Spring Boot primary datasource by default unless it has its own datasource defined. Flyway also does not need any schema information in the datasource url. Hence we decided to create two different datasources, one for Flyway that does not contain schema information in the url, another one for Spring Boot primary datasource that contains the schema name. If you want to try this in programmatic way, you can define a @Bean with @FlywayDataSource that Flyway can use for data migration. Also you must need to define another @Bean with @Primary. Makinus applications used to configure through application.properties, hence we did the following.

#Flyway
flyway.url = jdbc:mysql://localhost:3306/
flyway.user = ENC(pcs/pbUbvfWuVCPcFr2QaA==)
flyway.password = ENC(dS+A9wuvtE1KkgVv1nyjUg==)
flyway.schemas = TEST
flyway.locations = classpath:db/migration

#Primary Datasource props
spring.datasource.url = jdbc:mysql://localhost:3306/TEST
spring.datasource.username = ENC(pcs/pbUbvfWuVCPcFr2QaA==)
spring.datasource.password = ENC(dS+A9wuvtE1KkgVv1nyjUg==)
spring.datasource.driver-class-name = com.mysql.jdbc.Driver
spring.datasource.type = com.zaxxer.hikari.HikariDataSource

This solved the problem of creating database schema even before deploying the Spring Boot application.

Comments

Post a Comment

Popular posts from this blog

CSRF enabled Ajax requests using Spring Security

Many of you who have worked on Spring Security might be aware of the fact that Spring Security protects applications from Cross Site Request Forgery using _csrf tokens in the request sent to the web server. You can find a detailed understanding in the Spring documentation page . The objective of this post is to explain how to send _csrf tokens in the Ajax requests when we protect our application URL and application access using spring security. How to get CSRF tokens While we submit a form using an application that is protected with Spring Security, the form gets a default hidden parameter in the form body when using <form:form> element. The param contains the _csrf tokens to authenticate the requests in the server. In case we use other ways to create forms, we have to manually include a hidden parameter that contains name as ${_csrf.parameterName} and  value as ${_csrf.token} . For example, <input type= "hidden" name= "${_csrf.parameterName}"

A wonderful technique to reduce website development cost

Websites - Good way to get online presence Websites are very vital to get online presence of any business nowadays. Websites are categorized into two different types. First one is Static Website and second one is Dynamic Website, normally known as web applications. Static websites are most widely used for any business since they help to bring up the online presence more easily and quickly. Depending on the content and features, static websites cost around $300-$700 . It includes web design and development. Apart from that, the business has to spend for hosting space and domain name for the website. Cloud based development is now more prevalent. Building a website and running it will be very easy and cheap using these cloud infrastructure. But the difficulty facing the development of static websites still looms high as it does not matter who provides the infrastructure. The development cost is still same. Technology - LAMP Static websites are developed using HTML and PHP mostly

In-Place editing with X-Editable using Datatable plugin

Introduction In-place editing is a trending feature that can be seen in many latest web applications, a popular example would be trello.com where the editing data happens on the web page without any explicit forms or popups. Another such example I could point out is, thoughtplan.com. The in-place editing is very nice in such a way that editing data seems so natural and user friendly. To enable in-place editing, there are many free JQuery plugins available on the internet. We are discussing a very popular plugin called x-editable . Most of the time we use html tables to display data where in-place editing is enabled. Hence we need another plugin to elegantly display tables with enormous data. We use a famous JQuery plugin called datatable . Both of these plugins are used widely and free to use. Assumption      : Bootstrap 3, JQuery used Projects Integration In order to enable datatable features on any ordinary table found on web page, we should initialise datatable plugin for