DDL command not created by liquibase when trying t

2019-09-02 10:24发布

Update:

I think the problem is this line because it returns an empty list when I called changelog.getChangeSets(), so what is wrong this code?

    static liquibase.changelog.DatabaseChangeLog changelog = new DatabaseChangeLog(
            "src/main/resources/changelog.xml");

Original post:

My goal is to run database DDL commands with existing changelog in Java class. Currently my set up is like this:

public class LiquibaseWithJavaCode {
    //find the location of the existing changelog.xml file
    static liquibase.changelog.DatabaseChangeLog changelog = new DatabaseChangeLog(
            "src/main/resources/changelog.xml");

    static void executeUpdate1() throws SQLException, LiquibaseException {

        java.sql.Connection connection = getConnection();

        Database database = DatabaseFactory.getInstance()
                .findCorrectDatabaseImplementation(
                        new JdbcConnection(connection));

        Liquibase liquibase = new liquibase.Liquibase(changelog,
                new ClassLoaderResourceAccessor(), database);

        liquibase.update(new Contexts(), new LabelExpression());
    }

    public static Connection getConnection() throws SQLException {

        Connection conn = null;
        Properties connectionProps = new Properties();
        connectionProps.put("user", "liquibase");
        connectionProps.put("password", "password");

        conn = DriverManager
                .getConnection(
                        "jdbc:sqlserver://111.11.1.1;databaseName=liquibase;SelectMethod=cursor",
                        connectionProps);
        return conn;
    }
}

    public static void main(String[] args) throws SQLException,
        LiquibaseException {
            executeUpdate1();
    }

When I run it here is the output: enter image description here

I created the changelog.xml and it contains a lot of changeset that handles a lot of DDL command. But it seems the changeLogFile does not contain any changeset, it is empty. I double checked with the database, and it was indeed no actually table created, only two tables DATABASECHANGELOG and DATABASECHANGELOGLOCK being created.

Did I do anything wrong? How do I correct it? To be honest I am not familiar about it so please advice.

1条回答
祖国的老花朵
2楼-- · 2019-09-02 10:53

new DatabaseChangeLog just creates an new empty changelog file. To parse an existing file, use:

ChangeLogParser parser = ChangeLogParserFactory.getInstance().getParser("src/main/resources/changelog.xml", resourceAccessor);
DatabaseChangeLog databaseChangeLog = parser.parse("src/main/resources/changelog.xml", new ChangeLogParameters(), resourceAccessor);

For a resourceAccessor, you probably want new ClassLoaderResourceAccessor()

查看更多
登录 后发表回答