3

我的要求是我想比较两个数据库然后我想生成一个 diff sql。注意我只想通过 java 来做。我是 Liquibase 的新手。我找到了一个名为 CommandLineUtils 的 java api,使用它我可以生成新数据库的 changeLog。当我执行方法 doDiffToChangeLog 时,我的 changeLog xml 为空。你能告诉我哪里做错了吗。

这是我的java代码

Database database=liqui.createDatabaseObject(OracleDriver.class.getClassLoader(), "jdbc:oracle:thin:@localhost:1521:XE", "archive_pmd", "archive_pmd", "oracle.jdbc.driver.OracleDriver", "archive_pmd", null, null);
       liqui.doGenerateChangeLog("test.xml", database, "archive_pmd", null, null, null, null);
       Database database2=liqui.createDatabaseObject(OracleDriver.class.getClassLoader(), "jdbc:oracle:thin:@localhost:1521:XE", "archive_dbs", "archive_dbs", "oracle.jdbc.driver.OracleDriver", "archive_pmd", null, null);
       liqui.doDiffToChangeLog("chageLog.xml", database2, database);

这是我生成的 test.xml

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-2.0.xsd">
    <changeSet author="DELL (generated)" id="1331066786490-1">
        <createTable schemaName="archive_pmd" tableName="PMD00_SEQUENCE">
            <column name="PMD00_ID" type="NUMBER">
                <constraints nullable="false"/>
            </column>
            <column name="TABLE_IDENTIFIER" type="VARCHAR2(50 BYTE)"/>
            <column defaultValueNumeric="0" name="CURRENT_VALUE" type="NUMBER">
                <constraints nullable="false"/>
            </column>
            <column name="LAST_ACCESSED_TS" type="DATE"/>
        </createTable>
    </changeSet>
    <changeSet author="DELL (generated)" id="1331066786490-2">
        <createTable schemaName="archive_pmd" tableName="PMD01_META_DATA">
            <column name="PMD01_ID" type="NUMBER">
                <constraints nullable="false"/>
            </column>
            <column name="CATEGORY" type="VARCHAR2(30 BYTE)">
                <constraints nullable="false"/>
            </column>
            <column name="NAME" type="VARCHAR2(50 BYTE)">
                <constraints nullable="false"/>
            </column>
            <column name="CODE" type="VARCHAR2(100 BYTE)">
                <constraints nullable="false"/>
            </column>
            <column name="STATUS" type="VARCHAR2(1 BYTE)">
                <constraints nullable="false"/>
            </column>
            <column name="PMD01_METADATAVER" type="NUMBER(4,0)">
                <constraints nullable="false"/>
            </column>
            <column name="DMLASTUPDATEDATE" type="DATE">
                <constraints nullable="false"/>
            </column>
        </createTable>
    </changeSet>
    <changeSet author="DELL (generated)" id="1331066786490-3">
        <createTable schemaName="archive_pmd" tableName="PMD13_DB_APPLICATION">
            <column name="ID" type="NUMBER(17,0)">
                <constraints nullable="false"/>
            </column>
            <column name="APPLICATIONNAME" type="VARCHAR2(200 BYTE)">
                <constraints nullable="false"/>
            </column>
            <column name="ENVIRONMENT" type="NUMBER(17,0)">
                <constraints nullable="false"/>
            </column>
            <column name="SERVERIP" type="VARCHAR2(400 BYTE)">
                <constraints nullable="false"/>
            </column>
            <column name="USERID" type="VARCHAR2(200 BYTE)">
                <constraints nullable="false"/>
            </column>
            <column name="PASSWORD" type="VARCHAR2(200 BYTE)">
                <constraints nullable="false"/>
            </column>
            <column name="DBTYPE" type="NUMBER(17,0)">
                <constraints nullable="false"/>
            </column>
            <column name="DMLASTUPDATE" type="DATE">
                <constraints nullable="false"/>
            </column>
            <column name="SERVERPORT" type="NUMBER(17,0)">
                <constraints nullable="false"/>
            </column>
            <column name="SERVICE" type="VARCHAR2(400 BYTE)">
                <constraints nullable="false"/>
            </column>
        </createTable>
    </changeSet>
    <changeSet author="DELL (generated)" id="1331066786490-4">
        <createTable schemaName="archive_pmd" tableName="PMD14_DB_VERSION">
            <column name="ID" type="NUMBER(17,0)">
                <constraints nullable="false"/>
            </column>
            <column name="MAJORVERSION" type="NUMBER(17,0)">
                <constraints nullable="false"/>
            </column>
            <column name="MINORVERSION" type="NUMBER(17,0)">
                <constraints nullable="false"/>
            </column>
            <column name="PATCHVERSION" type="NUMBER(17,0)">
                <constraints nullable="false"/>
            </column>
            <column name="DATEEXECUTED" type="DATE">
                <constraints nullable="false"/>
            </column>
            <column name="DML_SCRIPT" type="BLOB">
                <constraints nullable="false"/>
            </column>
            <column name="STATUS" type="VARCHAR2(20 BYTE)">
                <constraints nullable="false"/>
            </column>
            <column name="REQUESTOR" type="VARCHAR2(200 BYTE)">
                <constraints nullable="false"/>
            </column>
            <column name="DMLASTUPDATE" type="DATE">
                <constraints nullable="false"/>
            </column>
            <column name="DML_SCRIPTNAME" type="VARCHAR2(200 BYTE)">
                <constraints nullable="false"/>
            </column>
            <column name="DB_APPLICATION_FK" type="NUMBER">
                <constraints nullable="false"/>
            </column>
        </createTable>
    </changeSet>
    <changeSet author="DELL (generated)" id="1331066786490-5">
        <createTable schemaName="archive_pmd" tableName="PMD15_DBCOMMIT_LOG">
            <column name="ID" type="NUMBER(17,0)">
                <constraints nullable="false"/>
            </column>
            <column name="MAJORVERSION" type="NUMBER(17,0)">
                <constraints nullable="false"/>
            </column>
            <column name="MINORVERSION" type="NUMBER(17,0)">
                <constraints nullable="false"/>
            </column>
            <column name="PATCHVERSION" type="NUMBER(17,0)">
                <constraints nullable="false"/>
            </column>
            <column name="DATEEXECUTED" type="DATE">
                <constraints nullable="false"/>
            </column>
            <column name="SCRIPT" type="BLOB">
                <constraints nullable="false"/>
            </column>
            <column name="EXECUTIONSTATUS" type="VARCHAR2(200 BYTE)">
                <constraints nullable="false"/>
            </column>
            <column name="REQUESTOR" type="VARCHAR2(400 BYTE)">
                <constraints nullable="false"/>
            </column>
            <column name="LOGREPORT" type="BLOB">
                <constraints nullable="false"/>
            </column>
            <column name="DMLASTUPDATE" type="DATE">
                <constraints nullable="false"/>
            </column>
            <column name="DBAPPLICATIONFK" type="NUMBER(17,0)">
                <constraints nullable="false"/>
            </column>
            <column name="LOGREPORTNAME" type="VARCHAR2(400 BYTE)">
                <constraints nullable="false"/>
            </column>
            <column name="SCRIPTNAME" type="VARCHAR2(400 BYTE)">
                <constraints nullable="false"/>
            </column>
        </createTable>
    </changeSet>
    <changeSet author="DELL (generated)" id="1331066786490-6">
        <createIndex indexName="SYS_C004087" schemaName="archive_pmd" tableName="PMD00_SEQUENCE" tablespace="SYSTEM" unique="true">
            <column name="PMD00_ID"/>
        </createIndex>
    </changeSet>
    <changeSet author="DELL (generated)" id="1331066786490-7">
        <createIndex indexName="SYS_C004084" schemaName="archive_pmd" tableName="PMD01_META_DATA" tablespace="SYSTEM" unique="true">
            <column name="PMD01_ID"/>
        </createIndex>
    </changeSet>
    <changeSet author="DELL (generated)" id="1331066786490-8">
        <createIndex indexName="SYS_C004050" schemaName="archive_pmd" tableName="PMD13_DB_APPLICATION" tablespace="SYSTEM" unique="true">
            <column name="ID"/>
        </createIndex>
    </changeSet>
    <changeSet author="DELL (generated)" id="1331066786490-9">
        <createIndex indexName="SYS_C004062" schemaName="archive_pmd" tableName="PMD14_DB_VERSION" tablespace="SYSTEM" unique="true">
            <column name="ID"/>
        </createIndex>
    </changeSet>
    <changeSet author="DELL (generated)" id="1331066786490-10">
        <createIndex indexName="SYS_C004076" schemaName="archive_pmd" tableName="PMD15_DBCOMMIT_LOG" tablespace="SYSTEM" unique="true">
            <column name="ID"/>
        </createIndex>
    </changeSet>
    <changeSet author="DELL (generated)" id="1331066972401-1">
        <createTable schemaName="archive_pmd" tableName="PMD00_SEQUENCE">
            <column name="PMD00_ID" type="NUMBER">
                <constraints nullable="false"/>
            </column>
            <column name="TABLE_IDENTIFIER" type="VARCHAR2(50 BYTE)"/>
            <column defaultValueNumeric="0" name="CURRENT_VALUE" type="NUMBER">
                <constraints nullable="false"/>
            </column>
            <column name="LAST_ACCESSED_TS" type="DATE"/>
        </createTable>
    </changeSet>
    <changeSet author="DELL (generated)" id="1331066972401-2">
        <createTable schemaName="archive_pmd" tableName="PMD01_META_DATA">
            <column name="PMD01_ID" type="NUMBER">
                <constraints nullable="false"/>
            </column>
            <column name="CATEGORY" type="VARCHAR2(30 BYTE)">
                <constraints nullable="false"/>
            </column>
            <column name="NAME" type="VARCHAR2(50 BYTE)">
                <constraints nullable="false"/>
            </column>
            <column name="CODE" type="VARCHAR2(100 BYTE)">
                <constraints nullable="false"/>
            </column>
            <column name="STATUS" type="VARCHAR2(1 BYTE)">
                <constraints nullable="false"/>
            </column>
            <column name="PMD01_METADATAVER" type="NUMBER(4,0)">
                <constraints nullable="false"/>
            </column>
            <column name="DMLASTUPDATEDATE" type="DATE">
                <constraints nullable="false"/>
            </column>
        </createTable>
    </changeSet>
    <changeSet author="DELL (generated)" id="1331066972401-3">
        <createTable schemaName="archive_pmd" tableName="PMD13_DB_APPLICATION">
            <column name="ID" type="NUMBER(17,0)">
                <constraints nullable="false"/>
            </column>
            <column name="APPLICATIONNAME" type="VARCHAR2(200 BYTE)">
                <constraints nullable="false"/>
            </column>
            <column name="ENVIRONMENT" type="NUMBER(17,0)">
                <constraints nullable="false"/>
            </column>
            <column name="SERVERIP" type="VARCHAR2(400 BYTE)">
                <constraints nullable="false"/>
            </column>
            <column name="USERID" type="VARCHAR2(200 BYTE)">
                <constraints nullable="false"/>
            </column>
            <column name="PASSWORD" type="VARCHAR2(200 BYTE)">
                <constraints nullable="false"/>
            </column>
            <column name="DBTYPE" type="NUMBER(17,0)">
                <constraints nullable="false"/>
            </column>
            <column name="DMLASTUPDATE" type="DATE">
                <constraints nullable="false"/>
            </column>
            <column name="SERVERPORT" type="NUMBER(17,0)">
                <constraints nullable="false"/>
            </column>
            <column name="SERVICE" type="VARCHAR2(400 BYTE)">
                <constraints nullable="false"/>
            </column>
        </createTable>
    </changeSet>
    <changeSet author="DELL (generated)" id="1331066972401-4">
        <createTable schemaName="archive_pmd" tableName="PMD14_DB_VERSION">
            <column name="ID" type="NUMBER(17,0)">
                <constraints nullable="false"/>
            </column>
            <column name="MAJORVERSION" type="NUMBER(17,0)">
                <constraints nullable="false"/>
            </column>
            <column name="MINORVERSION" type="NUMBER(17,0)">
                <constraints nullable="false"/>
            </column>
            <column name="PATCHVERSION" type="NUMBER(17,0)">
                <constraints nullable="false"/>
            </column>
            <column name="DATEEXECUTED" type="DATE">
                <constraints nullable="false"/>
            </column>
            <column name="DML_SCRIPT" type="BLOB">
                <constraints nullable="false"/>
            </column>
            <column name="STATUS" type="VARCHAR2(20 BYTE)">
                <constraints nullable="false"/>
            </column>
            <column name="REQUESTOR" type="VARCHAR2(200 BYTE)">
                <constraints nullable="false"/>
            </column>
            <column name="DMLASTUPDATE" type="DATE">
                <constraints nullable="false"/>
            </column>
            <column name="DML_SCRIPTNAME" type="VARCHAR2(200 BYTE)">
                <constraints nullable="false"/>
            </column>
            <column name="DB_APPLICATION_FK" type="NUMBER">
                <constraints nullable="false"/>
            </column>
        </createTable>
    </changeSet>
    <changeSet author="DELL (generated)" id="1331066972401-5">
        <createTable schemaName="archive_pmd" tableName="PMD15_DBCOMMIT_LOG">
            <column name="ID" type="NUMBER(17,0)">
                <constraints nullable="false"/>
            </column>
            <column name="MAJORVERSION" type="NUMBER(17,0)">
                <constraints nullable="false"/>
            </column>
            <column name="MINORVERSION" type="NUMBER(17,0)">
                <constraints nullable="false"/>
            </column>
            <column name="PATCHVERSION" type="NUMBER(17,0)">
                <constraints nullable="false"/>
            </column>
            <column name="DATEEXECUTED" type="DATE">
                <constraints nullable="false"/>
            </column>
            <column name="SCRIPT" type="BLOB">
                <constraints nullable="false"/>
            </column>
            <column name="EXECUTIONSTATUS" type="VARCHAR2(200 BYTE)">
                <constraints nullable="false"/>
            </column>
            <column name="REQUESTOR" type="VARCHAR2(400 BYTE)">
                <constraints nullable="false"/>
            </column>
            <column name="LOGREPORT" type="BLOB">
                <constraints nullable="false"/>
            </column>
            <column name="DMLASTUPDATE" type="DATE">
                <constraints nullable="false"/>
            </column>
            <column name="DBAPPLICATIONFK" type="NUMBER(17,0)">
                <constraints nullable="false"/>
            </column>
            <column name="LOGREPORTNAME" type="VARCHAR2(400 BYTE)">
                <constraints nullable="false"/>
            </column>
            <column name="SCRIPTNAME" type="VARCHAR2(400 BYTE)">
                <constraints nullable="false"/>
            </column>
        </createTable>
    </changeSet>
    <changeSet author="DELL (generated)" id="1331066972401-6">
        <createIndex indexName="SYS_C004087" schemaName="archive_pmd" tableName="PMD00_SEQUENCE" tablespace="SYSTEM" unique="true">
            <column name="PMD00_ID"/>
        </createIndex>
    </changeSet>
    <changeSet author="DELL (generated)" id="1331066972401-7">
        <createIndex indexName="SYS_C004084" schemaName="archive_pmd" tableName="PMD01_META_DATA" tablespace="SYSTEM" unique="true">
            <column name="PMD01_ID"/>
        </createIndex>
    </changeSet>
    <changeSet author="DELL (generated)" id="1331066972401-8">
        <createIndex indexName="SYS_C004050" schemaName="archive_pmd" tableName="PMD13_DB_APPLICATION" tablespace="SYSTEM" unique="true">
            <column name="ID"/>
        </createIndex>
    </changeSet>
    <changeSet author="DELL (generated)" id="1331066972401-9">
        <createIndex indexName="SYS_C004062" schemaName="archive_pmd" tableName="PMD14_DB_VERSION" tablespace="SYSTEM" unique="true">
            <column name="ID"/>
        </createIndex>
    </changeSet>
    <changeSet author="DELL (generated)" id="1331066972401-10">
        <createIndex indexName="SYS_C004076" schemaName="archive_pmd" tableName="PMD15_DBCOMMIT_LOG" tablespace="SYSTEM" unique="true">
            <column name="ID"/>
        </createIndex>
    </changeSet>
</databaseChangeLog>

=== 这是我的changeLog.xml(差异一)

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-2.0.xsd"/>

注意archive_dbs 不包含任何表。为什么liquibase didint 显示archive_pmd 中的表在archive_dbs 中丢失?

4

2 回答 2

3

这是一个适用于版本 3.2.2 的示例:

void diff(Connection referenceConnection, Connection targetConnection) throws LiquibaseException, IOException, ParserConfigurationException {

    Liquibase liquibase = null;

    try {

        Database referenceDatabase = DatabaseFactory.getInstance().findCorrectDatabaseImplementation(new JdbcConnection(referenceConnection));
        Database targetDatabase = DatabaseFactory.getInstance().findCorrectDatabaseImplementation(new JdbcConnection(targetConnection));

        liquibase = new Liquibase("", new FileSystemResourceAccessor(), referenceDatabase);
        DiffResult diffResult = liquibase.diff(referenceDatabase, targetDatabase, new CompareControl());
        new DiffToChangeLog(diffResult, new DiffOutputControl()).print(System.out);

    } finally {
        if (liquibase != null) {
            liquibase.forceReleaseLocks();
        }
    }
}
于 2014-10-24T12:41:02.863 回答
1

无论如何它现在正在工作..这是更新的代码..我现在喜欢 liquibase

Database database=CommandLineUtils.createDatabaseObject(OracleDriver.class.getClassLoader(), "jdbc:oracle:thin:@xxx.xx.xxx.xx:1521:XE", "archive_dbs", "123", "oracle.jdbc.driver.OracleDriver", "archive_dbs", null, null);
    //CommandLineUtils.doGenerateChangeLog("test.xml", database, "archive_dbs", null, null, null, null);uncomment to generate changelog
    Database database2=CommandLineUtils.createDatabaseObject(OracleDriver.class.getClassLoader(), "jdbc:oracle:thin:@yy.yy.yy.yy:1521:XE", "archive_dbs", "123", "oracle.jdbc.driver.OracleDriver", "archive_dbs", null, null);
   /* CommandLineUtils.doDiffToChangeLog("chageLog.xml", database2, database);//This can generate changeLog for diff
        ResourceAccessor resource=new FileSystemResourceAccessor();
        File sql=new File("SQLscript.sql");
        FileWriter writer=new FileWriter(sql);
        Liquibase base=new Liquibase("chageLog.xml",resource,database);
        base.update("Update",writer);//store diff sql to file,but donot execute it
        base.update("Update");//execute diff sql*/
        //CommandLineUtils.doDiff(database2, database);//show diff in console
   /* DatabaseSnapshot dbsnap1=new DatabaseSnapshot(database,"archive_dbs");
    DatabaseSnapshot dbsnap2=new DatabaseSnapshot(database2,"archive_dbs");*/
    Diff res=new Diff(database2,database);
    DiffResult result=res.compare();
    if(result.differencesFound()){
        System.out.println("Different");
    }else{
        System.out.println("Equal");
    }
    ByteArrayOutputStream baos=new ByteArrayOutputStream();
    PrintStream stream=new PrintStream(baos);
    result.printResult(stream);
    System.out.println(baos.toString());//show diff report
    System.out.println("Done");
于 2012-03-07T08:49:28.937 回答