Liquibase Preconditions indexExists

indexExists is a type of Precondition provided by Liquibase. It checks that index should exist in the table.

indexExists can take multiple parameters to check the index on the table, let's see them. 

<?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="1693111591793-1">
		<createTable catalogName="appledb" tableName="apple">
			<column name="apple_id" type="BIGINT">
				<constraints nullable="false" primaryKey="true" />
			</column>
			<column name="apple_name" type="VARCHAR(20)" />
		</createTable>
	</changeSet>
	<changeSet author="wesome" id="1693111591793-2">
		<preConditions>
			<indexExists tableName="apple" columnNames="apple_id" />
		</preConditions>
		<insert catalogName="appledb" tableName="apple">
			<column name="apple_id" valueNumeric="1" />
			<column name="apple_name" value="Macintosh" />
		</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
 

By default, a primary key itself creates an index on the column, but sometimes it doesn't provide all the information, so the createIndex constraint is used.

indexExist with indexName

Liquibase allows to provide names to index created, indexExists can check if an index of the same name exists or not.

<?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="1693111591793-1">
		<createTable catalogName="appledb" tableName="apple">
			<column name="apple_id" type="BIGINT">
			</column>
			<column name="apple_name" type="VARCHAR(20)" />
		</createTable>
	</changeSet>
	<changeSet author="wesome" id="1693111591793-2">
		<createIndex catalogName="AppleDb" indexName="apple_id_indx" schemaName="public" tableName="Apple" tablespace="AppleDb" unique="true">
			<column name="apple_id" />
		</createIndex>
	</changeSet>
	<changeSet author="wesome" id="1693111591793-3">
		<preConditions>
			<indexExists indexName="apple_id_indx" />
		</preConditions>
		<insert catalogName="appledb" tableName="apple">
			<column name="apple_id" valueNumeric="1" />
			<column name="apple_name" value="Macintosh" />
		</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

indexExist with tableName and columnNames

<?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="1693111591793-1">
		<createTable catalogName="appledb" tableName="apple">
			<column name="apple_id" type="BIGINT">
			</column>
			<column name="apple_name" type="VARCHAR(20)" />
		</createTable>
	</changeSet>
	<changeSet author="wesome" id="1693111591793-2">
		<createIndex catalogName="AppleDb" indexName="apple_id_indx" schemaName="public" tableName="Apple" tablespace="AppleDb" unique="true">
			<column name="apple_id" />
		</createIndex>
	</changeSet>
	<changeSet author="wesome" id="1693111591793-3">
		<preConditions>
			<indexExists tableName="apple" columnNames="apple_id" />
		</preConditions>
		<insert catalogName="appledb" tableName="apple">
			<column name="apple_id" valueNumeric="1" />
			<column name="apple_name" value="Macintosh" />
		</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

indexExist with indexName and tableName

<?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="1693111591793-1">
		<createTable catalogName="appledb" tableName="apple">
			<column name="apple_id" type="BIGINT">
			</column>
			<column name="apple_name" type="VARCHAR(20)" />
		</createTable>
	</changeSet>
	<changeSet author="wesome" id="1693111591793-2">
		<createIndex catalogName="AppleDb" indexName="apple_id_indx" schemaName="public" tableName="Apple" tablespace="AppleDb" unique="true">
			<column name="apple_id" />
		</createIndex>
	</changeSet>
	<changeSet author="wesome" id="1693111591793-3">
		<preConditions>
			<indexExists tableName="apple" indexName="apple_id_indx" />
		</preConditions>
		<insert catalogName="appledb" tableName="apple">
			<column name="apple_id" valueNumeric="1" />
			<column name="apple_name" value="Macintosh" />
		</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

Precondition with a custom message

indexExists fails the ChangeSet if Precondition is not matched, so onFailMessage can be used to provide a custom message.

<?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="1693111591793-1">
		<createTable catalogName="appledb" tableName="apple">
			<column name="apple_id" type="BIGINT">
			</column>
			<column name="apple_name" type="VARCHAR(20)" />
		</createTable>
	</changeSet>
	<changeSet author="wesome" id="1693111591793-2">
		<createIndex catalogName="AppleDb" indexName="apple_id_indx" schemaName="public" tableName="Apple" tablespace="AppleDb" unique="true">
			<column name="apple_id" />
		</createIndex>
	</changeSet>
	<changeSet author="wesome" id="1693111591793-3">
		<preConditions onFailMessage="Index with name apple_id_indx should exist in apple table">
			<indexExists indexName="apple_id_indx" />
		</preConditions>
		<insert catalogName="appledb" tableName="apple">
			<column name="apple_id" valueNumeric="1" />
			<column name="apple_name" value="Macintosh" />
		</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

follow us on