Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
898 views
in Technique[技术] by (71.8m points)

java - How to compare two database structures?

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...