尝试从Java代码运行时,liquibase未创建DDL命令

编程入门 行业动态 更新时间:2024-10-28 13:13:43
本文介绍了尝试从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不包含任何变更集,它是空的.我仔细检查了数据库,实际上并没有实际创建表,只创建了两个表DATABASECHANGELOG和DATABASECHANGELOGLOCK.

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命令

本文发布于:2023-10-28 23:57:42,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1538141.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:命令   代码   Java   DDL   liquibase

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!