尝试从Java代码运行时,liquibase未创建DDL命令 [英] DDL command not created by liquibase when trying to run it from Java code

查看:111
本文介绍了尝试从Java代码运行时,liquibase未创建DDL命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更新:

我认为问题是这行,因为当我调用changelog.getChangeSets()时它返回一个空列表,那么这段代码有什么问题呢?

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");

原始帖子:

我的目标是在Java类中使用现有的changelog运行数据库DDL命令. 目前,我的设置是这样的:

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:

我创建了changelog.xml,它包含许多处理很多DDL命令的变更集.但是看来changeLogFile不包含任何变更集,它是空的.我仔细检查了数据库,实际上并没有实际创建表,只创建了两个表DATABASECHANGELOGDATABASECHANGELOGLOCK.

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.

推荐答案

new DatabaseChangeLog仅创建一个新的空变更日志文件.要解析现有文件,请使用:

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);

对于resourceAccessor,您可能需要new ClassLoaderResourceAccessor()

For a resourceAccessor, you probably want new ClassLoaderResourceAccessor()

这篇关于尝试从Java代码运行时,liquibase未创建DDL命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆