Liquibase Attributes Object Quoting Strategy

By default, the Database treats unquoted object names in SQL as case insensitive. hence most DBA prefers case-sensitive object names or if using some reserved words such as table or column as column name of a table, then those should be quoted.

Liquibase provides objectQuotingStrategyattribute to control the object names in the generated SQL files or while executing them on the Database. objectQuotingStrategy can have three possible values.

  • LEGACY - this is the default value, it doesn't quote objects unless the database specifies that they must be quoted.
  • QUOTE_ALL_OBJECTS - it quotes each and every object.
  • QUOTE_ONLY_RESERVED_WORDS - it is the same LEGACY but only reserved keywords will be quoted.

Object Quoting Strategy on ChangeLog

Each ChangeLog contains multiple ChangeSet, ChangeLog supports objectQuotingStrategyattribute tag and the Strategy will be applicable to all the ChangeSets unless provided otherwise.

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
outputFile=update-sql.txt
overwriteOutputFile=true

liquibase.properties (update ChangeLog File format with SQL, XML, YAML, JSON as per changelog.mysql.<format>)

<?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" objectQuotingStrategy="QUOTE_ALL_OBJECTS">
	<changeSet author="wesome" id="1692446190573-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)" />
			<column defaultValueComputed="CURRENT_TIMESTAMP" name="current_date_time" type="datetime" />
		</createTable>
	</changeSet>
	<changeSet author="wesome" id="1692701200879-2">
		<insert catalogName="appledb" tableName="apple">
			<column name="apple_name" value="Fuji" />
			<column name="current_date_time" valueDate="now()" />
		</insert>
	</changeSet>
	<changeSet author="wesome" id="1692701200879-3">
		<insert catalogName="appledb" tableName="apple">
			<column name="apple_name" value="Gala" />
			<column name="current_date_time" valueDate="now()" />
		</insert>
	</changeSet>
	<changeSet author="wesome" id="1692701200879-4">
		<insert catalogName="appledb" tableName="apple">
			<column name="apple_name" value="Macintosh" />
			<column name="current_date_time" valueDate="now()" />
		</insert>
	</changeSet>
</databaseChangeLog>

Internally when Liquibase generates SQL, all the objects will be quoted. in order to see what the underlying generated SQL will be, execute the command liquibase updateSQL, it will create a update-sql.txt file, which will contain all the SQL query Liquibase will run against the database, After that execute the command liquibase update and it will update the corresponding database. The same can be verified by executing the query

SELECT * FROM appleDb.apple;

Object Quoting Strategy on ChangeSet

The Quoting Strategy tag on ChangeLog is applicable to add underlying ChangeSet, but if required, individual ChangeSet can have its own Quoting Strategy, and the priority will be given to ChangeSet.

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
outputFile=update-sql.txt
overwriteOutputFile=true

liquibase.properties (update ChangeLog File format with SQL, XML, YAML, JSON as per changelog.mysql.<format>)

<?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" objectQuotingStrategy="QUOTE_ALL_OBJECTS">
	<changeSet author="wesome" id="1692446190573-1" objectQuotingStrategy="LEGACY">
		<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)" />
			<column defaultValueComputed="CURRENT_TIMESTAMP" name="current_date_time" type="datetime" />
		</createTable>
	</changeSet>
	<changeSet author="wesome" id="1692701200879-2" objectQuotingStrategy="QUOTE_ALL_OBJECTS">
		<insert catalogName="appledb" tableName="apple">
			<column name="apple_name" value="Fuji" />
			<column name="current_date_time" valueDate="now()" />
		</insert>
	</changeSet>
	<changeSet author="wesome" id="1692701200879-3" objectQuotingStrategy="QUOTE_ONLY_RESERVED_WORDS">
		<insert catalogName="appledb" tableName="apple">
			<column name="apple_name" value="Gala" />
			<column name="current_date_time" valueDate="now()" />
		</insert>
	</changeSet>
	<changeSet author="wesome" id="1692701200879-4">
		<insert catalogName="appledb" tableName="apple">
			<column name="apple_name" value="Macintosh" />
			<column name="current_date_time" valueDate="now()" />
		</insert>
	</changeSet>
</databaseChangeLog>

to see what underlying generated SQL will be, execute the command liquibase updateSQL, it will create a update-sql.txt file, which will contain all the SQL query Liquibase will run against database, After that execute the command liquibase update and it will update the corresponding database. The same can be verified by executing the query

SELECT * FROM appleDb.apple;

follow us on