【问题标题】:How to compare two database structures?如何比较两个数据库结构?
【发布时间】:2017-10-12 10:11:44
【问题描述】:

我正在使用 SQLite/Hibernate。想法是每次应用启动时检查数据库结构是否是最新的。我在“DB”文件夹中有我现有的数据库,每次应用程序启动时,我都会在“DB/structure”文件夹中创建最新的数据库。

我想比较它们,如果我现有的数据库是旧的,请将数据复制到最新的数据库。摆脱旧数据库并移动新的数据库。

到目前为止,我已经尝试过 SchemaCrawler,但我遇到了错误,无法弄清楚。

更新:

我用 SchemaCrawler 连接到两个数据库:

public SchemaConroller() {

        SchemaCrawlerOptions options = new SchemaCrawlerOptions();
        options.setSchemaInfoLevel(SchemaInfoLevelBuilder.standard());

        Catalog catalog1=null;
        try {
            Connection conn = getConnection();
            catalog1 = SchemaCrawlerUtility.getCatalog(conn, options);
            for (Schema schema : catalog1.getSchemas()) {
                System.out.println(schema);
                for (Table table : catalog1.getTables(schema)) {
                    System.out.println("o--> " + table + "    size: "+table.getColumns().size());
                    /*for (Column column : table.getColumns()) {
                        System.out.println("     o--> " + column);
                    }*/
                }
            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        Catalog catalog2=null;
        try {
            Connection conn = getConnection2();
            catalog2 = SchemaCrawlerUtility.getCatalog(conn, options);
            for (Schema schema : catalog2.getSchemas()) {
                System.out.println(schema);
                for (Table table : catalog2.getTables(schema)) {
                    System.out.println("o--> " + table + "    size: "+table.getColumns().size());
                    /*for (Column column : table.getColumns()) {
                        System.out.println("     o--> " + column);
                    }*/
                }
            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        if(catalog1.equals(catalog2)){
            System.out.println(">>>>>>>>>>>>DATABASE IS UP TO DATE<<<<<<<<<<<<<<");
        }else{
            System.out.println(">>>>>>>>>>>>DATABASE IS OUT OF DATE<<<<<<<<<<<<<<");
        }
    }

但我总是得到肯定的答案,如果我尝试catalog1 == catalog2 - 我总是得到否定的答案。如何正确比较数据结构?

【问题讨论】:

  • Sualeh,不完全是。在那个问题中,你指出,我在问为什么它没有连接。在这篇文章中,我正在询问如何使用 SchemaCrawler 或任何其他插件来完成此操作的示例。不幸的是,可用的例子并不多。
  • 我如何将两个数据库结构与 SchemaCrawler 进行比较,最重要是否可以将数据从一个数据库复制到另一个数据库,如果没有某些列的数据只是将它们留空?

标签: java database hibernate sqlite schemacrawler


【解决方案1】:

阿廖娜,

请查看schemacrawler-diff,了解如何开始以编程方式比较数据库结构。请注意,此代码尚未用于生产,也不支持,因此您将根据自己的需要开发自己的数据库比较方法。

Sualeh Fatehi,SchemaCrawler

【讨论】:

  • 谢谢,按照您提供的示例,我比较了两个数据库,但CHANGED 值的结果有点奇怪。在第二个数据库中,我只向表中添加了一列,但我得到了 1 个ADDED 和 6 个CHANGED 结果,即使我不记得有任何更改,为什么它会这样反应:
  • /tables[flat] column flat was CHANGED to flat /tables[flat]/columns[flat.number] column null was ADDED to flat.number /tables[flat]/columns[flat.owner_name] column flat.owner_name was CHANGED to flat.owner_name /tables[flat]/columns[flat.owner_surname] column flat.owner_surname was CHANGED to flat.owner_surname /tables[flat]/columns[flat.area_size] column flat.area_size was CHANGED to flat.area_size /tables[flat]/columns[flat.people_living] column flat.people_living was CHANGED to flat.people_living 等等...
  • Table Flat 是空的,但添加后的任何列都显示为已更改。
猜你喜欢
  • 1970-01-01
  • 2012-08-24
  • 1970-01-01
  • 2011-02-15
  • 1970-01-01
  • 1970-01-01
  • 2011-11-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多