【问题标题】:CustomTaskChange gets actually executed when called updateSQLCustomTaskChange 在调用 updateSQL 时实际执行
【发布时间】:2014-06-06 11:35:12
【问题描述】:

我在 Liquibase 中有一个 CustomTaskChange(除了其他变更集)。 我希望我的应用程序在实际执行之前显示所有 ChangeSet 的 SQL。据我了解,updateSQL 应该预览 SQL 并且什么都不执行。但是,CustomTaskChange 会立即执行。我原以为CustomTaskChange 在调用updateSQL 时会被忽略。

变更集:

<changeSet id="2" author="clu">
    <comment>Print out 'helloworld'.</comment>
    <customChange class="path.to.HelloWorldUpdate">
</customChange>
</changeSet>

CustomTaskChange:

public class HelloWorldUpdate implements CustomTaskChange {
    @Override
public void execute(Database database) throws CustomChangeException {
    System.out.println("HELLO WORLD!!");
}
}

当调用updateSQL 时,控制台会打印“HELLO WORLD!!”

这是一个错误吗?有解决办法吗?

谢谢!

[编辑]

【问题讨论】:

    标签: java liquibase


    【解决方案1】:

    CustomTaskChange的描述说:

    Interface to implement when creating a custom change that does not actually
    generate SQL. If you are updating a database through SQL, 
    implementing CustomSqlChange is preferred because the SQL can either 
    be executed directly or saved to a text file for later use depending 
    on the migration mode used.
    

    所以请改用CustomSqlChange。

    在generateStatements() 方法中,您还可以在返回SqlStatement[] 数组之前打印出您的sql。 println 每次都会运行。但是您创建的语句不会应用于数据库,而是在您选择使用updateSQL 运行时打印到标准输出。

    【讨论】:

    • 感谢您的回答。也许我说得不够清楚...我实际上想要一个Java方法在update上运行,上面的HelloWorldUpdate类只是一个玩具示例。实际的CustomSqlChange 应该根据程序中的计算更新列。但它是数据库重构的一部分。
    • 调用updateSQL 时,我希望 LB 预览 CustomSqlChange 的 SQL,但不为 CustomTaskChange 做任何事情。如果它只是预览 DB 操作,它为什么要执行任务?
    • updateSQL 用于写入 liquibase 将对文件而不是数据库所做的更改。然后您可以预览文件(作为一个用例)并查看 liquibase 会执行什么。
    • 对,我想用它作为预览。但这与 CustomTaskChange 不同。 CustomSqlChange 也无济于事,因为它不知道它是在 update 还是 updateSQL 模式下运行。我认为,CustomXXXChanges 应该接收一个参数,它们以何种模式运行。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-11-17
    • 2016-04-22
    • 2018-09-10
    • 1970-01-01
    • 1970-01-01
    • 2012-10-09
    • 1970-01-01
    相关资源
    最近更新 更多