Liquibase Command changelogSyncToTag

Liquibase provides a ChangelogSync command that makes some ChangeSet as executed in DATABASECHANGELOG table, without actually executing it, it will mark them as executed. but sometimes the requirement is to mark only some of the ChangeSets as executed or all ChangeSets from the start up to the tag.

Liquibase provides changelogSyncToTag which will mark all ChangeSets as executed, up to the Tag passed as parameters

execute the below query in the database

create database appleDb;
use appleDb;
create table apple (apple_id bigint not null AUTO_INCREMENT , apple_name varchar(255), available CHAR(1), current_date_time datetime default now(), primary key (apple_id), CONSTRAINT uqniue_apple_name UNIQUE (apple_name)) engine=InnoDB;
insert into  apple (apple_name, available, current_date_time) values ("Macintosh", 'Y',  now());
insert into  apple (apple_name, available, current_date_time) values ("Fuji", 'Y',  now());
commit;

The above script will create a database, and a table and insert some data into it.

now create a changelog.mysql.xml file and add some insert ChangeSets

<?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="1692701200879-1">
		<tagDatabase tag="version_1.1" />
	</changeSet>
	<changeSet author="wesome" id="1697954384174-2">
		<insert catalogName="appledb" tableName="apple">
			<column name="apple_name" value="Gala" />
			<column name="avialable" value="Y" />
			<column name="current_date_time" valueDate="now()" />
		</insert>
	</changeSet>
	<changeSet author="wesome" id="1692701200879-3">
		<tagDatabase tag="version_1.2" />
	</changeSet>
	<changeSet author="wesome" id="1697954384174-4">
		<insert catalogName="appledb" tableName="apple">
			<column name="apple_name" value="Jonagold" />
			<column name="avialable" value="Y" />
			<column name="current_date_time" valueDate="now()" />
		</insert>
	</changeSet>
	<changeSet author="wesome" id="1692701200879-5">
		<tagDatabase tag="version_1.3" />
	</changeSet>
	<changeSet author="wesome" id="1697954384174-6">
		<insert catalogName="appledb" tableName="apple">
			<column name="apple_name" value="GrannySmith" />
			<column name="avialable" value="Y" />
			<column name="current_date_time" valueDate="now()" />
		</insert>
	</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

execute the command

liquibase changelogSyncToTag --tag=verison_1.2

The changelogSyncToTag command will not execute the ChangeSets, but mark them all as executed upto tag=verison_1.2 provided in parameters. The same can be validated by executing the command

select * from appleDb.apple;

but will mark them as executed in DATABASECHANGELOG table, same can be verified by executing the command

select * from appleDb.DATABASECHANGELOG;

follow us on