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

旧城冷巷雨未停 提交于 2019-12-04 09:58:09

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.

Take a look at updateSQL command.

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