liquibase / liquibase-groovy-dsl

The official Groovy DSL for Liquibase
Other
83 stars 34 forks source link

SQLFileChange ChangeLogParameters is null but The xml configuration is not empty #50

Closed tofdragon closed 3 years ago

tofdragon commented 3 years ago

When XML is configured, variables can be replaced correctly. However, when it is Groovy, SQLFileChange fetches changeLogParameters as empty. Debugger code finds that Groovy's Parser does not set changeLogParameters

version: liquibase: 3.8.9 liquibase-groovy-dsl: 2.1.2

driver=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/test?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf-8
username=root
password=123456
changeLogFile=database/db-main-changelog.groovy
parameter.my21=my777777777
parameter.my22=my999999999
delete from s_test  where id in('818c4d04-c949-4096-a9cf-c412fe8c344b');

insert into s_test (id, login_name, login_password, user_name, user_number, phone_number, email, is_valid, remark, created_by, created_date, modified_by, modified_date)
values ('818c4d04-c949-4096-a9cf-c412fe8c344b', 'admin-test004', '${my21}', '${my22}', null, '123456789', 'admin@test.cn', '1', 'test', 'anonymousUser', 1577691791394, 'anonymousUser', 1577691791394);
SQLFileChange
public String getSql() {
        String sql = super.getSql();
        if (sql == null) {
            InputStream sqlStream;
            try {
                sqlStream = openSqlStream();
                if (sqlStream == null) {
                    return null;
                }
                String content = StreamUtil.getStreamContents(sqlStream, encoding);
                if (getChangeSet() != null) {
                    // important:  groovy is null, xml is not null
                    ChangeLogParameters parameters = getChangeSet().getChangeLogParameters();
                    if (parameters != null) {
                        content = parameters.expandExpressions(content, getChangeSet().getChangeLog());
                    } 
                }
                return content;
            } catch (IOException e) {
                throw new UnexpectedLiquibaseException(e);
            }
        } else {
            return sql;
        }
    }

This method is called when the XML is parsed


DatabaseChangeLog
 protected ChangeSet createChangeSet(ParsedNode node, ResourceAccessor resourceAccessor) throws ParsedNodeException {
        ChangeSet changeSet = new ChangeSet(this);
        // important: This method is called to set variables when the XML is configured
        changeSet.setChangeLogParameters(this.getChangeLogParameters());
        changeSet.load(node, resourceAccessor);
        return changeSet;
  }
stevesaliman commented 3 years ago

Thank you for the detailed analysis of this problem. It made it easy to fix.

Version 3.0.2 of the Groovy DSL has been released and should be showing up on Maven Central shortly. Note that this release is built for Liquibase 4.x, so you'll need to update to a 4.x Liquibase.