gradlegroovyliquibasedslliquibase-hibernate

How to pass dynamic schema names to Liquibase changeset and run them in a loop using liquibase gradle plugin


We are new to liquibase and trying to create schema's dynamically like this

<property name="schemaName" value="temp2"/>
<changeSet author="abcd" id="1">
    <sql dbms="postgresql" endDelimiter=";">
        CREATE SCHEMA $(schemaName)
    </sql>
</changeSet>

But we are getting this exception when we try to execute

    Unexpected error running Liquibase: ERROR: syntax error at or near "$"
  Position: 15 [Failed SQL: (0) CREATE SCHEMA $(schemaName)]
liquibase.exception.MigrationFailedException: Migration failed for change set src/main/db/changelog2.xml::1::aditya:
     Reason: liquibase.exception.DatabaseException: ERROR: syntax error at or near "$"
  Position: 15 [Failed SQL: (0) CREATE SCHEMA $(schemaName)]
        at liquibase.changelog.ChangeSet.execute(ChangeSet.java:646)
        at liquibase.changelog.visitor.UpdateVisitor.visit(UpdateVisitor.java:53)
        at liquibase.changelog.ChangeLogIterator.run(ChangeLogIterator.java:83)
        at liquibase.Liquibase.update(Liquibase.java:202)
        at liquibase.Liquibase.update(Liquibase.java:179)
        at liquibase.integration.commandline.Main.doMigration(Main.java:1223)
        at liquibase.integration.commandline.Main.run(Main.java:209)
        at liquibase.integration.commandline.Main.main(Main.java:132)

It would be of great help if the community could please point us right direction


Solution

  • You can use replaceable parameters in your changelog - these are called changelog parameters

    The syntax is to use ${parameterName} in the changelog (note curly braces rather than parentheses).