如何比较两个数据库结构?
How to compare two database structures?
我正在使用 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
- 我总是得到否定的答案。如何正确比较数据结构?
阿莲娜,
请查看 schemacrawler-diff,了解有关如何开始以编程方式比较数据库结构的想法。请注意,此代码未准备好生产,也不受支持,因此您将根据自己的需要开发自己的比较数据库的方法。
Sualeh Fatehi,SchemaCrawler
我正在使用 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
- 我总是得到否定的答案。如何正确比较数据结构?
阿莲娜,
请查看 schemacrawler-diff,了解有关如何开始以编程方式比较数据库结构的想法。请注意,此代码未准备好生产,也不受支持,因此您将根据自己的需要开发自己的比较数据库的方法。
Sualeh Fatehi,SchemaCrawler