簡體   English   中英

liquibase-在使用Java比較兩個數據庫之后如何從changelog生成sql腳本?

[英]liquibase - how to generate the sql script from the changelog after comparing two databases using java?

我將兩個數據庫與liquibase java API 3.3.2進行了比較,然后生成了changelog.xml文件。 現在,我想生成將更新目標數據庫的sql腳本。 這是我的代碼:

Database database;
Database database2;
try {
    database = CommandLineUtils.createDatabaseObject(org.postgresql.Driver.class.getClassLoader(), "jdbc:postgresql://localhost:5432/loan", "postgres", "", "org.postgresql.Driver", null, null, false, false, null, null, null, null, null);
    database2=CommandLineUtils.createDatabaseObject(org.postgresql.Driver.class.getClassLoader(), "jdbc:postgresql://localhost:5432/loan2", "postgres", "", "org.postgresql.Driver", null, null, false, false, null, null, null, null, null);
    Liquibase liquibase = new Liquibase("", new FileSystemResourceAccessor(), database);
    DiffResult result = liquibase.diff(database, database2, new CompareControl());
    DiffToChangeLog diffLog = new DiffToChangeLog(result, new DiffOutputControl());
    diffLog.print(System.out);
    //new DiffToReport(result, System.out).print();

} catch (Exception e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

編輯這是我的工作方式,這要感謝Steve donie:

        Database database;
    Database database2;
    try {
        database = CommandLineUtils.createDatabaseObject(org.postgresql.Driver.class.getClassLoader(), "jdbc:postgresql://localhost:5432/loan", "postgres", "", "org.postgresql.Driver", null, null, false, false, null, null, null, null, null);
        database2= CommandLineUtils.createDatabaseObject(org.postgresql.Driver.class.getClassLoader(), "jdbc:postgresql://localhost:5432/loan2", "postgres", "", "org.postgresql.Driver", null, null, false, false, null, null, null, null, null);

        // generate the changelog that contains the differences
        CommandLineUtils.doDiffToChangeLog("changelog.xml", database, database2, new DiffOutputControl(), null, null);

        // generate the sql script from the changeLog (without executing it)
        File sql = new File("output.sql");
        Liquibase liquibase = new Liquibase("changelog.xml", new FileSystemResourceAccessor(), database);
        liquibase.update("Update", new FileWriter(sql));
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

解決此問題的一種方法是查看從命令行運行時Liquibase本身的功能。 在這種情況下,通常將使用diffChangeLog命令創建更改日志,然后運行updateSQL生成更新數據庫所需的SQL。 看着為Liquibase源代碼liquibase.integration.commandline.Main ,我看到它做什么是使用Liquibase.update()方法與參數指定一個非空輸出作家 (這是SQL將文件保存到)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM