在Java代码中运行liquibase [英] Running liquibase within Java code

查看:122
本文介绍了在Java代码中运行liquibase的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于某种原因,没有关于在Java代码中运行liquibase的文档。我想为单元测试生成表。

For some reason there's no documentation on running liquibase inside Java code. I want to generate tables for Unit tests.

如何直接在Java中运行它?

How would I run it directly in Java?

例如

Liquibase liquibase = new Liquibase()
liquibase.runUpdates() ?


推荐答案

应该是这样的(取自liquibase.integration .spring.SpringLiquibase source):

It should be something like (taken from liquibase.integration.spring.SpringLiquibase source):

java.sql.Connection c = YOUR_CONNECTION;
Liquibase liquibase = null;
try {
    Database database = DatabaseFactory.getInstance().findCorrectDatabaseImplementation(new JdbcConnection(c))
    liquibase = new Liquibase(YOUR_CHANGELOG, new FileSystemResourceAccessor(), database);
    liquibase.update();
} catch (SQLException e) {
    throw new DatabaseException(e);
} finally {
    if (c != null) {
        try {
            c.rollback();
            c.close();
        } catch (SQLException e) {
            //nothing to do
        }
    }
}

ResourceAccessor有多种实现方式,具体取决于应该如何找到更改日志文件。

There are multiple implementation of ResourceAccessor depending on how your changelog files should be found.

这篇关于在Java代码中运行liquibase的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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