Liquibase Java,变更集不适用

时间:2015-08-04 16:21:46

标签: java liquibase

我目前正在为我的实习建立一个项目,因为我在两天内试图在java中使用Liquibase而陷入困境。一切似乎都正确:找到更改日志文件,正确的URI,用户名和密码;但是当我运行它时,我的变更集不会被处理。

我使用此类来管理程序中的liquibase操作,例如 rollback update updateSQL futureRollbackSQL ,给出了更改日志,最终是目标文件。如果源或目标来自远程服务器,我使用一些SSH交互(scp to,scp from)与 JSch 和临时文件(但它不是主题)。

这是我现在的java代码,给定 db 用户 passwd realAction 之前设置的 changelogpath dest 是一些数据存储类。

Connection c = null;
Database database = null;
PrintWriter pw = null;
File file = null;
liquibase.Liquibase liquibase = null;
contexts = db+"."+user;
try {
    pw = new PrintWriter(new FileWriter(file));
    // Get connection
    c = SQLManager.getInstance().getConnection(db, user, passwd);
    // Get liquibase connection
    database = DatabaseFactory.getInstance().findCorrectDatabaseImplementation(new JdbcConnection(c));
    liquibase = new liquibase.Liquibase(new DatabaseChangeLog(fsource), new FileSystemResourceAccessor(),
            database);
    // Run liquibase action
    switch (realAction) {
        case Constants.LIQUIBASE_ACTION_FUTUREROLLBACKSQL:
            liquibase.futureRollbackSQL(pw);
            break;
        case Constants.LIQUIBASE_ACTION_UPDATESQL:
            liquibase.update(contexts, pw);
            break;
        case Constants.LIQUIBASE_ACTION_UPDATE:
            liquibase.update(contexts);
            if (!c.getAutoCommit())
                c.commit();
            break;
        default:
            throw new OdewipElementRuntimeException(this, "Action not implemented");
    }
    pw.close();
    database.close();
    c.close();
} catch (IOException | SQLException | LiquibaseException e) {
    throw new Exception(e.getMessage());
} finally {
    if (c != null) {
        try {
            c.close();
        } catch (SQLException e) {
            // nothing to do
            throw new RuntimeException(e.getClass() + ": " + e.getMessage());
        }
    }
}

这是我的更新日志:

<?xml version="1.0" encoding="UTF-8"?>

<databaseChangeLog
  xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:ora="http://www.liquibase.org/xml/ns/dbchangelog-ext"
  xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-2.0.xsd    http://www.liquibase.org/xml/ns/dbchangelog-ext http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-ext.xsd">    

    <changeSet id="mychangeset" author="testy">
        <preConditions onSqlOutput="TEST" onFail="MARK_RAN">
            <not>
                <tableExists tableName="abcd"/>
            </not>
        </preConditions>
        <createTable tableName="abcd">
            <column name="id" type="number">
                <constraints primaryKey="true"/>
            </column>
        </createTable>
    </changeSet>

</databaseChangeLog>

Liquibase似乎做某事,除了解析我的变更集。当我启动我的操作时,sql生成的文件只包含liquibase(databasechangelog和databasechangeloglock)这两个表的创建,这就是全部。 更新操作根本不会修改任何内容(甚至不创建前面提到的两个表)。我(100%)确定在执行之前数据库中不存在表 abcd

所以我觉得我现在需要一些帮助,找出什么不起作用。我试着看一下liquibase论坛的一些例子,但没有任何帮助。 我目前正在使用Maven的liquibase 3.4.0:

<dependency>
    <groupId>org.liquibase</groupId>
    <artifactId>liquibase-core</artifactId>
    <version>3.4.0</version>
</dependency>

还有其他依赖包括我错过了吗? 另一个小问题是我如何包含一个特殊的oracle数据库驱动程序(ojdbc6.jar)?

感谢您的回答。

编辑06/08/2015: 我在调试模式下记录了日志(故意更改模式名称):

DEBUG 06/08/15 09:28: liquibase: Executing QUERY database command: select count(*) from MYSCHEMA.DATABASECHANGELOGLOCK
DEBUG 06/08/15 09:28: liquibase: Create Database Lock Table
DEBUG 06/08/15 09:28: liquibase: Executing EXECUTE database command: CREATE TABLE MYSCHEMA.DATABASECHANGELOGLOCK (ID NUMBER(10) NOT NULL, LOCKED NUMBER(1) NOT NULL, LOCKGRANTED TIMESTAMP, LOCKEDBY VARCHAR2(255), CONSTRAINT PK_DATABASECHANGELOGLOCK PRIMARY KEY (ID))
DEBUG 06/08/15 09:28: liquibase: Created database lock table with name: MYSCHEMA.DATABASECHANGELOGLOCK
DEBUG 06/08/15 09:28: liquibase: Executing QUERY database command: select count(*) from MYSCHEMA.DATABASECHANGELOGLOCK
DEBUG 06/08/15 09:28: liquibase: Initialize Database Lock Table
DEBUG 06/08/15 09:28: liquibase: Executing EXECUTE database command: DELETE FROM MYSCHEMA.DATABASECHANGELOGLOCK
DEBUG 06/08/15 09:28: liquibase: Executing EXECUTE database command: INSERT INTO MYSCHEMA.DATABASECHANGELOGLOCK (ID, LOCKED) VALUES (1, 0)
DEBUG 06/08/15 09:28: liquibase: Executing QUERY database command: SELECT LOCKED FROM MYSCHEMA.DATABASECHANGELOGLOCK WHERE ID=1 FOR UPDATE
DEBUG 06/08/15 09:28: liquibase: Lock Database
DEBUG 06/08/15 09:28: liquibase: Executing UPDATE database command: UPDATE MYSCHEMA.DATABASECHANGELOGLOCK SET LOCKED = 1, LOCKEDBY = 'CRO09177 (xx.xx.xx.xxx)', LOCKGRANTED = to_date('2015-08-06 09:28:28', 'YYYY-MM-DD HH24:MI:SS') WHERE ID = 1 AND LOCKED = 0
INFO 06/08/15 09:28: liquibase: Successfully acquired change log lock
DEBUG 06/08/15 09:28: liquibase: Create Database Change Log Table
INFO 06/08/15 09:28: liquibase: Creating database history table with name: MYSCHEMA.DATABASECHANGELOG
DEBUG 06/08/15 09:28: liquibase: Executing EXECUTE database command: CREATE TABLE MYSCHEMA.DATABASECHANGELOG (ID VARCHAR2(255) NOT NULL, AUTHOR VARCHAR2(255) NOT NULL, FILENAME VARCHAR2(255) NOT NULL, DATEEXECUTED TIMESTAMP NOT NULL, ORDEREXECUTED NUMBER(10) NOT NULL, EXECTYPE VARCHAR2(10) NOT NULL, MD5SUM VARCHAR2(35), DESCRIPTION VARCHAR2(255), COMMENTS VARCHAR2(255), TAG VARCHAR2(255), LIQUIBASE VARCHAR2(20), CONTEXTS VARCHAR2(255), LABELS VARCHAR2(255))
DEBUG 06/08/15 09:28: liquibase: Executing QUERY database command: select count(*) from MYSCHEMA.DATABASECHANGELOG
INFO 06/08/15 09:28: liquibase: Reading from MYSCHEMA.DATABASECHANGELOG
DEBUG 06/08/15 09:28: liquibase: Executing QUERY database command: SELECT FILENAME,AUTHOR,ID,MD5SUM,DATEEXECUTED,ORDEREXECUTED,EXECTYPE,DESCRIPTION,COMMENTS,TAG,LIQUIBASE,LABELS,CONTEXTS FROM MYSCHEMA.DATABASECHANGELOG ORDER BY DATEEXECUTED ASC, ORDEREXECUTED ASC
DEBUG 06/08/15 09:28: liquibase: Executing QUERY database command: select count(*) from MYSCHEMA.DATABASECHANGELOGLOCK
DEBUG 06/08/15 09:28: liquibase: Release Database Lock
DEBUG 06/08/15 09:28: liquibase: Executing UPDATE database command: UPDATE MYSCHEMA.DATABASECHANGELOGLOCK SET LOCKED = 0, LOCKEDBY = NULL, LOCKGRANTED = NULL WHERE ID = 1
INFO 06/08/15 09:28: liquibase: Successfully released change log lock

2 个答案:

答案 0 :(得分:2)

在Datical,我们注意到Liquibase和Java 1.8存在一些问题。您在Liquibase用户论坛上提到您使用Java 1.8的帖子,因此Java 1.8可能成为问题。您可以尝试使用Java 1.7并查看是否得到不同的结果?如果没有,您可以尝试提高日志记录级别 - 在创建Liquibase对象后添加如下所示的行:

app/etc/

其中logLevel是字符串&#34; debug&#34;

答案 1 :(得分:0)