Liquibase 中 CustomSqlChange 的实现不起作用

Implementation of CustomSqlChange in Liquibase is not working

我需要在我的 liquibase 脚本运行时实现 CustomSqlChange 接口。以下是我的 yaml changeSet 条目:

changeSet: 
 customChange: 
  param: 
   _name: file
   _value: "/com/example/data/user.csv"
  _class: "com.example.MyClass"
  id: id
  author: abj
  failOnError: true

MyClass 的实现如下:

package com.example;

import liquibase.change.custom.CustomSqlChange;
import liquibase.change.custom.CustomSqlRollback;
import liquibase.database.Database;
import liquibase.exception.CustomChangeException;
import liquibase.exception.RollbackImpossibleException;
import liquibase.exception.SetupException;
import liquibase.exception.ValidationErrors;
import liquibase.resource.ResourceAccessor;
import liquibase.statement.SqlStatement;

public class MyClass implements CustomSqlChange, CustomSqlRollback  {


     //to hold the parameter value
    private String file;

    private ResourceAccessor resourceAccessor;


    @Override
    public String getConfirmationMessage() {
        return "Confirmation Message from Custom SQL Change";
    }

    @Override
    public void setUp() throws SetupException {

    }

    @Override
    public void setFileOpener(ResourceAccessor resourceAccessor) {
        this.resourceAccessor = resourceAccessor;
    }

    public String getFile() {
        return file;
    }

    public void setFile(String file) {
        this.file = file;
    }

    @Override
    public ValidationErrors validate(Database database) {
        return null;
    }

    @Override
    public SqlStatement[] generateStatements(Database database) throws CustomChangeException {
        return null;
    }

    @Override
    public SqlStatement[] generateRollbackStatements(Database database)
            throws CustomChangeException, RollbackImpossibleException {
        return null;
    }}

我的 Normal liquibase 脚本运行成功。我在 generateStatements 方法中有一个断点,但流程永远不会到达断点。在应用程序启动时,也不会为特定的变更集打印任何记录器。我是不是漏掉了什么?

稍作改动即可使用。 实施了 CustomTaskChange 而不是 CustomSqlChange。 另外还需要一个名为 changeId 的参数用于某些内部业务逻辑。 代码如下:

yaml ChangeSet 条目:

- changeSet:
    id: id
    author: abj
    changes:
    - customChange:
        class: com.example.MyClass
        params:
        - param:
            name: changeId
            value: id

MyClass 的代码是:

package com.example;

import liquibase.change.custom.CustomTaskChange;
import liquibase.database.Database;
import liquibase.exception.CustomChangeException;
import liquibase.exception.SetupException;
import liquibase.exception.ValidationErrors;
import liquibase.resource.ResourceAccessor;

public class MyClass implements CustomTaskChange  {

    private String changeId;

    public String getChangeId() {
        return changeId;
    }

    public void setChangeId(String changeId) {
        this.changeId = changeId;
    }

    @Override
    public String getConfirmationMessage() {
        return "Custom Task Change for "+this.getClass();
    }

    @Override
    public void setUp() throws SetupException {
    }

    @Override
    public void setFileOpener(ResourceAccessor resourceAccessor) {
    }

    @Override
    public ValidationErrors validate(Database database) {
        return null;
    }

    @Override
    public void execute(Database database) throws CustomChangeException {
        System.out.println("Code Flow comes Here");
    }}