in openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/schema/SchemaTool.java [766:887]
protected void retain(SchemaGroup db, SchemaGroup repos, boolean tables,
boolean sequences)
throws SQLException {
Schema[] schemas = db.getSchemas();
if (_seqs && sequences) {
Sequence[] seqs;
for (Schema schema : schemas) {
seqs = schema.getSequences();
for (Sequence seq : seqs) {
if (!isDroppable(seq))
continue;
if (repos.findSequence(seq) == null) {
if (dropSequence(seq))
schema.removeSequence(seq);
else
_log.warn(_loc.get("drop-seq", seq));
}
}
}
}
// order is important in this method; start with foreign keys
Table[] tabs;
Table reposTable;
if (_fks) {
ForeignKey[] fks;
ForeignKey fk;
for (Schema schema : schemas) {
tabs = schema.getTables();
for (Table tab : tabs) {
if (!isDroppable(tab))
continue;
fks = tab.getForeignKeys();
reposTable = repos.findTable(tab);
if (!tables && reposTable == null)
continue;
for (ForeignKey foreignKey : fks) {
if (foreignKey.isLogical())
continue;
fk = null;
if (reposTable != null)
fk = findForeignKey(reposTable, foreignKey);
if (reposTable == null || fk == null
|| !foreignKey.equalsForeignKey(fk)) {
if (dropForeignKey(foreignKey))
tab.removeForeignKey(foreignKey);
else
_log.warn(_loc.get("drop-fk", foreignKey,
tab));
}
}
}
}
}
// primary keys
if (_pks) {
PrimaryKey pk;
for (Schema schema : schemas) {
tabs = schema.getTables();
for (Table tab : tabs) {
if (!isDroppable(tab))
continue;
pk = tab.getPrimaryKey();
if (pk != null && pk.isLogical())
continue;
reposTable = repos.findTable(tab);
if (pk != null && reposTable != null
&& (reposTable.getPrimaryKey() == null
|| !pk.equalsPrimaryKey(reposTable.getPrimaryKey()))) {
if (dropPrimaryKey(pk))
tab.removePrimaryKey();
else
_log.warn(_loc.get("drop-pk", pk, tab));
}
}
}
}
// columns
Column[] cols;
Column col;
Collection<Table> drops = new LinkedList<>();
for (Schema value : schemas) {
tabs = value.getTables();
for (Table tab : tabs) {
if (!isDroppable(tab))
continue;
cols = tab.getColumns();
reposTable = repos.findTable(tab);
if (reposTable != null) {
for (Column column : cols) {
col = reposTable.getColumn(column.getIdentifier());
if (col == null || !column.equalsColumn(_dict, col)) {
if (tab.getColumns().length == 1)
drops.add(tab);
else if (dropColumn(column))
tab.removeColumn(column);
else
_log.warn(_loc.get("drop-col", column,
tab));
}
}
}
}
}
// now tables
if (tables) {
for (Schema schema : schemas) {
tabs = schema.getTables();
for (Table tab : tabs)
if (isDroppable(tab)
&& repos.findTable(tab) == null)
drops.add(tab);
}
}
dropTables(drops, db);
}