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?
答案 0 :(得分:0)
Alyona,
请查看schemacrawler-diff,了解如何以编程方式比较数据库结构。请注意,此代码不是生产就绪的,也不支持,因此您将根据自己的需要开发自己的数据库比较方法。
Sualeh Fatehi,SchemaCrawler