Liquibase Command tagExists

Tags allows Liquibase to rollback database to the tag status. Liquibase provides tag command in CLI to tag current status without creating ChangeSet. To validate if a tag exists in DATABASECHANGELOG table, Liquibase provides the tagExists command.

<?xml version="1.1" encoding="UTF-8" standalone="no"?>
<databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog" xmlns:ext="http://www.liquibase.org/xml/ns/dbchangelog-ext" xmlns:pro="http://www.liquibase.org/xml/ns/pro" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog-ext http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-ext.xsd http://www.liquibase.org/xml/ns/pro http://www.liquibase.org/xml/ns/pro/liquibase-pro-latest.xsd 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">
		<tagDatabase tag="version_1.1" />
	</changeSet>
	<changeSet author="wesome" id="1692701200879-3">
		<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-4">
		<tagDatabase tag="version_1.2" />
	</changeSet>
	<changeSet author="wesome" id="1692701200879-5">
		<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>

liquibase.properties (update changeLog File file format with SQL, XML, YAML, JSON as per changelog.mysql.)

changeLogFile=changelog.mysql.xml
liquibase.command.url:jdbc:mysql://localhost:3306/appleDb?createDatabaseIfNotExist=true&autoReconnect=true&useSSL=false&allowPublicKeyRetrieval=true
liquibase.command.username : root
liquibase.command.password : rootroot

Run command liquibase update

The table is created with all the records, the same can be verified by running the query

SELECT * FROM appleDb.apple;

The tagDatabase inserted a new record in DATABASECHANGELOG table for each tag.

SELECT * FROM appleDb.databasechangelog;

To validate the presence of a specific tag, run the command liquibase tagExists --tag=version_1.2

follow us on