How to compare two database structures?

南楼画角 提交于 2019-12-11 15:17:02

问题


I'm using SQLite/Hibernate. Idea is to check each time app starts whether database structure is up to date. I have my existing database in "DB" folder and each time app start I'm creating up to date database in "DB/structure" folder.

I want to compare them and if my existing database is old, copy data to up to date database. Get rid of old database and move fresh one in it's place.

So far I've tried SchemaCrawler, but I was getting errors with it and couldn't figure it out.

UPDATE:

I connected with SchemaCrawler to both databases:

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<<<<<<<<<<<<<<");
        }
    }

But I always get positive answer, if I try catalog1 == catalog2 - I always get negative. How to properly compare data structure?


回答1:


Alyona,

Please take a look at schemacrawler-diff for ideas on how to get started with programatically comparing database structures. Please note that this code is not production ready, nor is it supported, so you will to develop your own method of comparing databases, based on your needs.

Sualeh Fatehi, SchemaCrawler



来源:https://stackoverflow.com/questions/46706970/how-to-compare-two-database-structures

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!