Search code examples
liquibase

Liquibase resolves/interpolates environment variables in SQL files


I have an SQL file like this with a variable:

INSERT INTO ATABLE VALUES ('${MY_VAR}');

I set on my shell

export MY_VAR=ABC

Now I run liquibase.sh to apply my SQL to the DB. I can see that in my database it contains the value 'ABC'.

I can see from the documentation that this mechanism is supported 1 2, but documentation states it should only do this for variables starting with 'LIQUIBASE_'. Why is it still doing it for my variable?

I am asking because we are seeing this replacmenent of environment variable in an inconsistent manor. It happens for some variables but others not and i would like to understand what is expected from liquibase product side. Should it replace random variables that do not start with the LIQUIBASE_ prefix or not?

Using liquibase: org.liquibase:liquibase-core=4.20.0


Solution

  • I can see from the documentation that this mechanism is supported 1 2, but documentation states it should only do this for variables starting with 'LIQUIBASE_'.

    I'm not sure why did you came to this conclusion. These two links you've mentioned are referring to two completely different things:

    1. Substituting Properties in Changelogs tells about how to pass the external parameters to your changelogs and changesets. One of the options is usage of environment variables.
    2. Liquibase Environment Variables tells about the variables used to configure Liquibase itself. They start with LIQUIBASE_ which is a common pattern intended to minimize the possibility of conflicts as it provides some kind of namespace.

    Given that, the inconsistentencies you're talking about could be caused by at least two reasons:

    • an environment variable is overridden by a CLI attribute, or by a changeset parameter (see the first documentation link above for precedence details);
    • an environment variable name that you use in your changeset conflicts with some existing Liquibase environment variable.