Liquibase Preconditions foreignKeyConstraintExists

foreignKeyConstraintExists is a type of Precondition provided by Liquibase. It checks for the Foreign key should exist in the table.

<?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">
		<createTable catalogName="appledb" tableName="vendor">
			<column name="vendor_id" type="BIGINT">
				<constraints nullable="false" primaryKey="true" />
			</column>
			<column name="vendor_name" type="VARCHAR(20)" />
			<column name="apple_id" type="BIGINT" />
		</createTable>
	</changeSet>
	<changeSet author="wesome" id="1693111591793-3">
		<addForeignKeyConstraint baseColumnNames="apple_id" baseTableName="vendor" baseTableSchemaName="appledb" constraintName="apple_id_fk" referencedColumnNames="apple_id" referencedTableName="apple" referencedTableSchemaName="appleDb" validate="true" />
	</changeSet>
	<changeSet author="wesome" id="1693111591793-4">
		<insert catalogName="appledb" tableName="apple">
			<column name="apple_id" valueNumeric="1" />
			<column name="apple_name" value="Macintosh" />
		</insert>
	</changeSet>
	<changeSet author="wesome" id="1693111591793-5">
		<preConditions>
			<foreignKeyConstraintExists foreignKeyName="apple_id_fk" foreignKeyTableName="vendor" />
		</preConditions>
		<insert catalogName="appledb" tableName="vendor">
			<column name="vendor_id" valueNumeric="1" />
			<column name="vendor_name" value="Apple_Vendor" />
			<column name="apple_id" valueNumeric="1" />
		</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

foreignKeyConstraintExists 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">
				<constraints nullable="false" primaryKey="true" />
			</column>
			<column name="apple_name" type="VARCHAR(20)" />
		</createTable>
	</changeSet>
	<changeSet author="wesome" id="1693111591793-2">
		<createTable catalogName="appledb" tableName="vendor">
			<column name="vendor_id" type="BIGINT">
				<constraints nullable="false" primaryKey="true" />
			</column>
			<column name="vendor_name" type="VARCHAR(20)" />
			<column name="apple_id" type="BIGINT" />
		</createTable>
	</changeSet>
	<changeSet author="wesome" id="1693111591793-3">
		<addForeignKeyConstraint baseColumnNames="apple_id" baseTableName="vendor" baseTableSchemaName="appledb" constraintName="apple_id_fk" referencedColumnNames="apple_id" referencedTableName="apple" referencedTableSchemaName="appleDb" validate="true" />
	</changeSet>
	<changeSet author="wesome" id="1693111591793-4">
		<insert catalogName="appledb" tableName="apple">
			<column name="apple_id" valueNumeric="1" />
			<column name="apple_name" value="Macintosh" />
		</insert>
	</changeSet>
	<changeSet author="wesome" id="1693111591793-5">
		<preConditions onFailMessage="vendor table should have a forign key of apple table by the name of apple_id_fk">
			<foreignKeyConstraintExists foreignKeyName="apple_id_fk" foreignKeyTableName="vendor" />
		</preConditions>
		<insert catalogName="appledb" tableName="vendor">
			<column name="vendor_id" valueNumeric="1" />
			<column name="vendor_name" value="Apple_Vendor" />
			<column name="apple_id" valueNumeric="1" />
		</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