LiquiBase — Any way to output change log sql to a

2019-04-09 15:43发布

问题:

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.