Liquibase Gradle Plugin

Liquibase provides a plugin for Gradle which is located in the central Maven repository. In the below code, for simplicity, for now are passing the database connection string and credentials in build.gradle file.

build.gradle

plugins {
    id 'org.liquibase.gradle' version '2.2.0'
}
group = 'com.example'
version = '0.0.1-SNAPSHOT'
repositories {
    mavenCentral()
}
dependencies {
    liquibaseRuntime 'info.picocli:picocli:4.6.3'
    liquibaseRuntime 'org.liquibase:liquibase-core:4.24.0'
    liquibaseRuntime 'com.mysql:mysql-connector-j:8.1.0'

}

def changeLog = "changelog.mysql.xml"
liquibase {
    activities {
        main {
            changelogFile changeLog
            url 'jdbc:mysql://localhost:3306/appledb?createDatabaseIfNotExist=true'
            username 'root'
            password 'rootroot'
        }
    }
}

changelog.mysql.xml

<?xml version="1.1" encoding="UTF-8" standalone="no"?>
<databaseChangeLog xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.liquibase.org/xml/ns/dbchangelog" xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-latest.xsd">
	<changeSet author="wesome" id="1692446190573-1">
		<createTable catalogName="appledb" tableName="apple">
			<column autoIncrement="true" name="apple_id" type="BIGINT">
				<constraints nullable="false" primaryKey="true" />
			</column>
			<column name="apple_name" type="VARCHAR(255)" />
			<column defaultValueComputed="CURRENT_TIMESTAMP" name="current_date_time" type="datetime" />
		</createTable>
		<rollback>
			<dropTable tableName="apple" />
		</rollback>
	</changeSet>
	<changeSet author="wesome" id="1692701200879-2">
		<insert catalogName="appledb" tableName="apple">
			<column name="apple_name" value="Fuji" />
			<column name="current_date_time" valueDate="now()" />
		</insert>
		<rollback>
			<delete tableName="apple">
				<where>
					apple_name = 'Fuji'
				</where>
			</delete>
		</rollback>
	</changeSet>
	<changeSet author="wesome" id="1692701200879-3">
		<insert catalogName="appledb" tableName="apple">
			<column name="apple_name" value="Gala" />
			<column name="current_date_time" valueDate="now()" />
		</insert>
		<rollback>
			<delete tableName="apple">
				<where>
					apple_name = 'Gala'
				</where>
			</delete>
		</rollback>
	</changeSet>
</databaseChangeLog>

run command

gradle update

The apple table and corresponding data will be created and inserted into the Database, the same can be verified by running SQL query

SELECT * FROM appledb.apple;

follow us on