Wednesday, December 20, 2017

Mule:Flyway DB migration tool Integration

Most of the people have understood importance of Flyway/Liquibase DB migration tool. I worked on Flyway integration with spring framework application in 2014 and I loved it. It made my life easy and simple for next 2 years.  Flyway DB- (https://flywaydb.org)

Similar experience I had with liquibase just an advantage which we got is that it support xml format along with sql scripts.
DB migration is very much important when you starting your mule project and you are going to deal with database for longer term with lot of db schema changes.
I will continue with my previous project where we will pick up properties from specific environment wise properties (Blog link-Mule: Load Properties as per the Environment (With Default Properties File) ).

Code is available on Github - (https://github.com/aghorpade/MuleSoft/tree/master/MuleFlywayIntegration
1. Download project from github location
2.open project into anypoint studio -> import->anypoint studio->pom base project
Project structure will look like as below -
 


1. In this if you observer carefully then you will see that DB migration scripts are kept under
resources/db/migration with specific format as V1__initial_Schema.sql
if you want to have another schema or update on exisiting schema then add new schema file as
V1.1__update_Schema.sql or V2__update_schema.sql
File naming convention can be found on https://flywaydb.org/ also we can change default file naming convention b overriding property of flyway class
 2. Also properties are picked up from dev.properties file if we provide environment argument -Dmule.env=dev



Now properties files you can add as many as per environment available for you. While running just provide env name in argument value.
We will be using dbcp basicdDataSource as it has more advantages over mysql or oracle datasource. (Why DBCP daasource -ref
1. https://stackoverflow.com/questions/17945137/spring-drivermanagerdatasource-vs-apache-basicdatasource
2. https://www.journaldev.com/2509/java-datasource-jdbc-datasource-example
3https://mytechrepo.wordpress.com/2015/02/17/drivermanagerdatasource-vs-basicdatasource/)



Also flyway configuration is provided in this code.
Assumption - mysql database is installed on local machine with root username and root password

Once application imported the  run as mule application-> provide  env argument as -Dmule.env so it will pick dev.properties file values.
Once you application ran successfully then go to mysql workbench and you will see that demo table has been created with one record inserted automatically. Magic of Flyway DB migration tool.
Also Flyway DB migration tool maintains schema_version file to validation newly added db migration files to code. and to show status of each db migration file



If mysql workbench is not available then once application is built successfully  then go to browser and run-
http://localhost:8081/hello
Result -
Hello World from Mule DB. [{id=100}]

Any questions or feedback - please let me know