primaryKeyExists
is a type of Precondition
provided by Liquibase
. It checks for the primaryKeyExists
should exist in the table
. While creating a column, a constraint can be added for primary = true
, it will make the column as primary key
of 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">
<preConditions>
<primaryKeyExists tableName="apple" schemaName="appleDb" />
</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
but sometimes, the default primary key is not sufficient to fulfill all the checks, so addPrimaryKey constrain is used to manually add a primary key 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" />
</column>
<column name="apple_name" type="VARCHAR(20)" />
</createTable>
</changeSet>
<changeSet author="wesome" id="1693111591793-2">
<addPrimaryKey catalogName="appleDb" columnNames="apple_id" constraintName="apple_id_pk" schemaName="appleDb" tableName="apple" tablespace="appleDb" validate="true" />
</changeSet>
<changeSet author="wesome" id="1693111591793-3">
<preConditions>
<primaryKeyExists tableName="apple" schemaName="appleDb" />
</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