LiquiBase — Any way to output change log sql to a file in 2.0.5?

旧时模样 提交于 2019-12-06 05:09:17

问题


Currently I am integrating Liquibase with my spring application using liquibase.integration.spring.SpringLiquibase bean

From java doc, tt is plausible to know there is an property sqlOutputDir to that bean class so that the sql can output to external file.

However, the feature seems not exist in latest 2.0.5.

So, the question is, what is the current equivalent method or function to output changeLog sql to external file, or the feature just have been totally removed forever?

Please give a hint, thanks a lot.


回答1:


The equivalent Java command for writing the SQL output is: Liquibase#update(String, Writer). You can invoke this method in your Spring app by subclassing SpringLiquibase and overriding afterPropertiesSet. For example:

@Bean
public SpringLiquibase liquibase() {
    SpringLiquibase liquibase = new SpringLiquibaseWriter();
    // ...
    return liquibase;
}

private static class SpringLiquibaseWriter extends SpringLiquibase {

    @Override
    public void afterPropertiesSet() throws LiquibaseException {

        try (Connection connection = getDataSource().getConnection()) {

            Liquibase liquibase = createLiquibase(connection);
            Writer writer; // ... get writer
            liquibase.update(getContexts(), writer);
            // ...

        } catch (LiquibaseException | SQLException e) {
            // handle
        }

        super.afterPropertiesSet();
    }
}

The call to update(String,Writer) will execute your changesets without actually updating the database. The call to super.afterPropertiesSet will actually perform the liquibase updates.

I did notice the javadoc you referred to in SpringLiquibase that mentions the writeSqlFileEnabled and sqlOutputDir properties. Apparently, these were removed but the javadoc was not updated (see CORE-1104). I'm not sure the reason that these options were removed or what the intended replacements are though. I've found that liquibase logging is a bit deficient, so this approach may be useful for logging (debugging) the liquibase SQL output.




回答2:


Take a look at updateSQL command.



来源:https://stackoverflow.com/questions/12147855/liquibase-any-way-to-output-change-log-sql-to-a-file-in-2-0-5

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!