Liquibase Integration in MuleSoft Application with spring dependency

Shyam Raj Prasad
4 min readApr 24, 2022

--

In last Article, we have learnt liquibase integration in mule application with maven command. Please refer: https://shyamrajprasad.medium.com/liquibase-integration-in-mulesoft-application-3e7d9d6cd20c

In this Article, we will integrate liquibase with spring dependency in mule application. Liquibase script will be executed at mule application startup.

  1. Mule Application: Create a Mule application in Anypoint studio.
  2. Application Properties: Create application.properties file under src/main/resources folder.
spring.datasource.url=jdbc:postgresql://localhost:5432/spring
spring.datasource.username=username
spring.datasource.password=password
spring.liquibase.enabled=true
spring.liquibase.change-log=classpath:liquibase/changelog/dbchangelog.xml
spring.liquibase.check-change-log-location=true
spring.liquibase.contexts = dev

3. Folder: Create a folder liquibase in src/main/resources. Also, create a subfolder script under liquibase.

4. SQL Script: Create a script file 01_create_accounts_table.sql in scripts folder and add the below script.

--liquibase formatted sql
--changeset shyam:1
CREATE TABLE accounts (
user_id serial PRIMARY KEY,
username VARCHAR ( 50 ) UNIQUE NOT NULL,
password VARCHAR ( 50 ) NOT NULL,
email VARCHAR ( 255 ) UNIQUE NOT NULL,
created_on TIMESTAMP NOT NULL,
last_login TIMESTAMP
);

5. DbChangeLog : create a dbchangelog.xml file under liquibase folder and add the below contents in the XML file.

<?xml version="1.0" encoding="UTF-8"?>  
<databaseChangeLog
xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.1.xsd">
<include file="scripts/01_create_accounts_table.sql" relativeToChangelogFile="true"/>

</databaseChangeLog>

6. Configuration Properties: Go to mule config and add configuration properties from the mule palette. Add application.properties in the global configuration.

7. Spring Config: Create spring-config.xml in src/main/resources. Add the below contents in the spring config.

<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/jdbc
http://www.springframework.org/schema/jdbc/spring-jdbc-4.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security-4.2.xsd">
</beans>

8. Spring Module: Go to mule palette again, add spring module and choose spring-config.xml.

9. Pom Dependency: Add below dependencies for liquibase and database.

       <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.3.2</version>
</dependency>
<dependency>
<groupId>org.liquibase</groupId>
<artifactId>liquibase-core</artifactId>
<version>4.9.1</version>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.3.4</version>
</dependency>

10. POM Shared Library: Add below library in shared libraries in mule maven plugin.

<sharedLibrary>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
</sharedLibrary>
<sharedLibrary>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
</sharedLibrary>
<sharedLibrary>
<groupId>org.liquibase</groupId>
<artifactId>liquibase-core</artifactId>
</sharedLibrary>

11. Spring Beans: Edit spring-config.xml and add below :

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="org.postgresql.Driver" />
<property name="url" value="${spring.datasource.url}" />
<property name="username" value="${spring.datasource.username}" />
<property name="password" value="${spring.datasource.password}" />
</bean>
<bean id="springLiquibase" class="liquibase.integration.spring.SpringLiquibase">
<property name="dataSource" ref="dataSource" />
<property name="changeLog" value="${spring.liquibase.change-log}" />
</bean>

It will create two beans Datasource and springliquibase.

12. Database Config: Go to mule config mule and add database configuration, with datasource reference connection.

13. Http Listener: Create a http listener and add select database operation.

14. Run Application: Run mule application and you will see the logs for liquibase script execution.

15. Verify Tables: Check the tables created in database. Also you can check the entries in databasechangelog table for one script execution.

16. Verify Endpoint: Now you can hit the endpoint for get accounts table it will return empty results, as there is no entry in the table.

In this article, we have learned how we can integrate the liquibase at mule startup.

GitHub Repo: https://github.com/shyamrajprasad/mulesoft-spring-liquibase-integration

References:

Happy Learning!

--

--

Shyam Raj Prasad
Shyam Raj Prasad

Written by Shyam Raj Prasad

Engineering Leader at Tricon Infotech Private Limited | Mulesoft Certified Developer and Architect

No responses yet