Liquibase Command unexpectedChangesets

Liquibase updates the database by executing the ChangeSet present in ChangeLog file, for reference purpose it also maintains the record of executed ChangeSets in DATABASECHANGELOG table. In order to validate that the ChangeLog file is in sync with database, Liquibase provides unexpectedChangesets command. The unexpectedChangesets command will compare the current ChangeLog file with the DATABASECHANGELOG table and will list down all the ChangeSet executed on the database but are not present in ChangeLog file.

<?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="1697962298531-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)">
				<constraints unique="true" />
			</column>
			<column name="available" type="CHAR(1)" />
			<column defaultValueComputed="CURRENT_TIMESTAMP" name="current_date_time" type="datetime" />
		</createTable>
	</changeSet>
	<changeSet author="wesome" id="1697962298531-2">
		<insert catalogName="appledb" tableName="apple">
			<column name="apple_id" valueNumeric="1" />
			<column name="apple_name" value="Macintosh" />
			<column name="available" value="Y" />
			<column name="current_date_time" valueDate="now()" />
		</insert>
	</changeSet>
	<changeSet author="wesome" id="1697962298531-3">
		<insert catalogName="appledb" tableName="apple">
			<column name="apple_id" valueNumeric="2" />
			<column name="apple_name" value="Fuji" />
			<column name="available" 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

Run command liquibase update

Liquibase will create a table with all the records, the same can be verified by running the query

SELECT * FROM appleDb.apple;

with each ChangeSet executed a new record in DATABASECHANGELOG table.

SELECT * FROM appleDb.databasechangelog;

now update the changelog.mysql.xml file by updating any of the ChangeSet like below

<?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="1697962298531-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)">
				<constraints unique="true" />
			</column>
			<column name="available" type="CHAR(1)" />
			<column defaultValueComputed="CURRENT_TIMESTAMP" name="current_date_time" type="datetime" />
		</createTable>
	</changeSet>
	<changeSet author="wesome" id="1697962298531-2">
		<insert catalogName="appledb" tableName="apple">
			<column name="apple_id" valueNumeric="1" />
			<column name="apple_name" value="Macintosh" />
			<column name="available" value="Y" />
			<column name="current_date_time" valueDate="now()" />
		</insert>
	</changeSet>
	<!--
	<changeSet author="wesome" id="1697962298531-3">
	<insert catalogName="appledb" tableName="apple">
	<column name="apple_id" valueNumeric="2" />
	<column name="apple_name" value="Fuji" />
	<column name="available" value="Y" />
	<column name="current_date_time" valueDate="now()" />
	</insert>
	</changeSet>
	-->
</databaseChangeLog>

now to validate the changelog.mysql.xml file against the DATABASECHANGELOG table, execute the command

liquibase unexpectedChangesets

follow us on