Liquibase Changelog Dynamic Properties Substitution from Environment

We have already seen how Liquibase allows the user to define all the properties at one location in the ChangeLog file and then replace those with a placeholder dynamically at run time. Liquibase also allows the dynamic property substitution from Operating System Environment Variables as well.

Setting environment variables for Liquibase

Different Operating Systems have their own ways of setting the environment variables as below

Windows

Windows allow to setting of environment variable via GUI and CMD as well

To set an Environment variable from GUI, follow these steps

  1. In your Windows search box, type env and select the Edit the System Environment option in the Control Panel.
  2. In the Advanced tab, select Environment Variables.
  3. In the Edit environment variable window, select New, and then add the variable name and value you need.
  4. Select OK on all windows to close them.

To set the Environment variable from CMD, follow these steps

use the Windows set command to set a temporary environment variable or the setx command to set a permanent environment variable from your command line.

macOS/Linux/Unix

mac Os, Linux, and Unix Operating System calls Environment Variables a shell variable. to set a temporary environment variable, in the terminal, write USERNAME=wesome

this will not persist in the system and will lose values once the terminal is closed.
To store permanent environment variables, use the export variable_name="value" command, ie export USERNAME=wesome.

The export variable_name=value the command will not permanently update the environment variable once terminal close. So to permanently update the value, edit the ~/.profile, ~/.bash_profile, or ~/.bashrc file and add the export command with an environment variable, to refresh the system with new changes, use the source command.

To add a permanent environment variable for all users of the system, create a file user.sh in the directory /etc/profile.d and add the export command for each variable.

<?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="1692599548266-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 name="owner_name" type="VARCHAR(255)" />
			<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_name" value="Gala" />
			<column name="owner_name" value="${USERNAME}" />
			<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.)

liquibase.command.url=jdbc:mysql://localhost:3306/appleDb?createDatabaseIfNotExist=true&autoReconnect=true&useSSL=false&allowPublicKeyRetrieval=true
liquibase.command.username= root
liquibase.command.password= rootroot

execute the command

liquibase update

follow us on