Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a Java API for comparing Database Schemas

I'd like to compare if

  • tables
  • columns including datatypes and length/precision.
  • indexes and their columns
  • constraints

in two database schemas are identical.

Is there anything like this available? Maybe from one of the database migration managing tools?

like image 856
Jens Schauder Avatar asked Jan 22 '23 14:01

Jens Schauder


1 Answers

I don't know a high level API for schema comparison I used DatabaseMetaData it's not to hard to find differences i.g to retieve all tables you can do something like this:

DatabaseMetaData meta = con.getMetaData();
  ResultSet res = meta.getTables(null, null, null, 
     new String[] {"TABLE"});
  System.out.println("List of tables: "); 
  while (res.next()) {
     System.out.println(
        "   "+res.getString("TABLE_CAT") 
       + ", "+res.getString("TABLE_SCHEM")
       + ", "+res.getString("TABLE_NAME")
       + ", "+res.getString("TABLE_TYPE")
       + ", "+res.getString("REMARKS")); 
  }
  res.close();

The following methods are also important for your intention:

getColumns(String catalog, String schemaPattern, String tableNamePattern, String columnNamePattern) 
getExportedKeys(String catalog, String schema, String table)
getIndexInfo(String catalog, String schema, String table, boolean unique, boolean approximate) 
getPrimaryKeys(String catalog, String schema, String table) 
like image 150
stacker Avatar answered Jan 28 '23 17:01

stacker