gpt4 book ai didi

java - 如何使用 java 在 liquibase 中区分两个新模式?

转载 作者:塔克拉玛干 更新时间:2023-11-02 18:58:53 25 4
gpt4 key购买 nike

我的要求是我想比较两个数据库,然后我想生成一个差异 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 中丢失?

最佳答案

这是一个适用于版本 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();
}
}
}

关于java - 如何使用 java 在 liquibase 中区分两个新模式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9592153/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com