navig8tr
navig8tr

Reputation: 1854

Liquibase use a variable to set changeset author

Is it possible to use some kind of variable or property to set the changeset author in a liquibase formatted sql file.

I have tried to set a property in a chnagelog.xml and using the property for the author as show below.

changelog.xml

<?xml version="1.0" encoding="UTF-8"?>
<databaseChangeLog
        xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
    http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.8.xsd">

    <property name="author" value="my-author"/>

    <include file="sql/test_schema.sql"/>
</databaseChangeLog>

And test_schemal.sql

--liquibase formatted sql

--changeset ${author}:1
CREATE SCHEMA IF NOT EXISTS test_schema;
--rollback DROP SCHEMA IF EXISTS test_schema;

However, the author is not set to the author property, "my-author".

I think this could also be done with terraform templating, but is there a way to do this using only liquibase functionality?

Upvotes: 1

Views: 5931

Answers (2)

Rakhi Agrawal
Rakhi Agrawal

Reputation: 917

Liquibase allows dynamic substitution of properties in changelog files. We can configure multiple properties inside a file and then use them wherever required. In your case, we can just configure property "authorName" with some value and then use it in changelog file using ${authorName} syntax.

Whatever Jorge has mentioned in his answer is correct. Liquibase assigns or prioritizes value for configured property in below order:

  1. As an attribute passed to your liquibase runner.
  2. As a JVM sytem property
  3. As an environment variable
  4. As a CLI attribute if you are running liquibase through command line
  5. In liquibase.properties file
  6. In the parameters block (property element of the DATABASECHANGELOG table)

You can do it as below example code snippet:

<?xml version="1.0" encoding="UTF-8"?>
<databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
                      http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.8.xsd">

    <changeSet author="${authorName}" id="some-unique-id" dbms="${dbType}" context="some-context">
        **My SQL query/ transactional logic goes here**
    </changeSet>
    
</databaseChangeLog>

In liquibase.properties file, I will configure these properties as follows:

authorName=Rakhi
dbType=PostgreSQL

Note: above example is using 2 properties (authorName and dbType). You can use only authorName or even more than that.

If you need help with creating "liquibase.properties" file, visit this link

Cheers!

Upvotes: 2

Jorge Dieguez Perez
Jorge Dieguez Perez

Reputation: 186

Using property substitution in your changelog

Liquibase allows you to use different ways to can set property values. Liquibase assigns these values in the order specified below:

As an attribute passed to your Liquibase runner.

  1. As a JVM system property.
  2. As an environment variable.
  3. As a CLI attributes if executed from the command line
  4. As a CLI attributes if executed from the command line
  5. command_line parameter if executed from the command line
  6. In the parameters block (property element) of the DATABASECHANGELOG file itself

Check this liquibase doc

Upvotes: 1

Related Questions