Applies to:
- Liquibase Pro
- Liquibase Open Source (Community)
Conditions:
- Any version of Liquibase Pro or Open Source
Question:
Can Liquibase use a variable in either the changelog or changeset to reference an external value?
Answer:
Yes, Liquibase performs variable substations in the changelog and changeset files. We document this in our Substituting Properties in Changelogs documentation page.
As long as the variable is defined in the environment, it can be referenced with this syntax ${variable_name}
.
Changelog example
<includeAll path="scripts/${TARGET_ENV}/"/>
In the above example, the TARGET_ENV
variable is defined as "PROD," so Liquibase will replace the variable with "PROD" and will include all of the files from the scripts/PROD/
directory
Changeset example
-- changeset liquibase_support:0
CREATE TABLE ${TARGET_SCHEMA}.employee (
name VARCHAR(100),
environment VARCHAR(100),
url VARCHAR(100)
);
-- rollback drop table ${TARGET_SCHEMA}.employee
In the above example, TARGET_SCHEMA is set to DEMO, so Liquibase will expand the variable to DEMO and will deploy the script to the DEMO schema.
Final SQL after variable substitution
-- changeset liquibase_support:0
CREATE TABLE DEMO.employee (
name VARCHAR(100),
environment VARCHAR(100),
url VARCHAR(100)
);
-- rollback drop table DEMO.employee
Related Article(s):
N/A
Comments
0 comments
Article is closed for comments.