Search code examples
kubernetesopenshiftliquibase

How to read environment variables in liquibase changelog


i'm executing a sql query in my changeset as follow:

<changeSet author="xxxxx" id="1682329977552-1" context="unittest">
<preConditions onFail="CONTINUE">
 <sqlCheck expectedResult="1"> SELECT COUNT(*) FROM pg_roles WHERE rolname='myuser';</sqlCheck>
</preConditions>
    <sqlFile dbms="!h2, oracle, mysql, postgresql"
             encoding="UTF-8"
             endDelimiter="\nGO"
             path="db_grants.sql"
             relativeToChangelogFile="true"
             splitStatements="true"
             stripComments="true"/>
</changeSet>

As you can see, myuser is hardcoded which is not desirable, i'm looking for a way to read that user from environment variables but was wondering if it's even possible.

I'm also using that user in db_grants.sql which grants some accesses to the user:

grant select on all tables in schema public to myuser;
grant insert on all tables in schema public to myuser;
grant delete on all tables in schema public to myuser;
grant update on all tables in schema public to myuser;

Good to mention that these scripts runs on openshift cluster enviroment.


Solution

  • As written in property-substitution you can pass properties to the liquibase command CLI with -D prefix (or you can use other method), so something like -Dproperty-name=somevalue and then in your changeSet/sql use it as ${property-name}.

    cmd: liquibase update -Dpguser=myuser

    <changeSet author="xxxxx" id="1682329977552-1" context="unittest">
    <preConditions onFail="CONTINUE">
     <sqlCheck expectedResult="1"> SELECT COUNT(*) FROM pg_roles WHERE rolname='${pguser}';</sqlCheck>
    </preConditions>
        <sqlFile dbms="!h2, oracle, mysql, postgresql"
                 encoding="UTF-8"
                 endDelimiter="\nGO"
                 path="db_grants.sql"
                 relativeToChangelogFile="true"
                 splitStatements="true"
                 stripComments="true"/>
    </changeSet>
    
    grant select on all tables in schema public to ${pguser};
    grant insert on all tables in schema public to ${pguser};
    grant delete on all tables in schema public to ${pguser};
    grant update on all tables in schema public to ${pguser};