Skip to main content
 首页 » 编程设计

java中CustomTaskChange 在调用 updateSQL 时实际执行

2025年01月19日6lhb25

我在 Liquibase 中有一个 CustomTaskChange(除了其他 ChangeSet)。 我希望我的应用程序在实际执行之前显示所有 ChangeSet 的 SQL。根据我的理解,updateSQL 应该预览 SQL 并且不执行任何操作。但是,CustomTaskChange 会立即执行。我本以为调用 updateSQL 时会忽略 CustomTaskChange

变更集:

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

自定义任务更改:

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

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

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

谢谢!

[已编辑]

请您参考如下方法:

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 运行它时打印到标准输出。