diff --git a/src/java/com/samskivert/jdbc/depot/DepotMarshaller.java b/src/java/com/samskivert/jdbc/depot/DepotMarshaller.java index c38a12d..3c753bf 100644 --- a/src/java/com/samskivert/jdbc/depot/DepotMarshaller.java +++ b/src/java/com/samskivert/jdbc/depot/DepotMarshaller.java @@ -664,6 +664,7 @@ public class DepotMarshaller // if schema versioning is disabled, stop now if (_schemaVersion < 0) { + verifySchemasMatch(ctx); return; } @@ -682,6 +683,7 @@ public class DepotMarshaller } }); if (currentVersion == _schemaVersion) { + verifySchemasMatch(ctx); return; } @@ -699,39 +701,9 @@ public class DepotMarshaller } // enumerate all of the columns now that we've run our pre-migrations - final Set columns = new HashSet(); - final Map> indexColumns = new HashMap>(); - ctx.invoke(new Modifier() { - public int invoke (Connection conn) throws SQLException { - DatabaseMetaData meta = conn.getMetaData(); - ResultSet rs = meta.getColumns(null, null, getTableName(), "%"); - while (rs.next()) { - columns.add(rs.getString("COLUMN_NAME")); - } - - rs = meta.getIndexInfo(null, null, getTableName(), false, false); - while (rs.next()) { - String indexName = rs.getString("INDEX_NAME"); - Set set = indexColumns.get(indexName); - if (rs.getBoolean("NON_UNIQUE")) { - // not a unique index: just make sure there's an entry in the keyset - if (set == null) { - indexColumns.put(indexName, null); - } - - } else { - // for unique indices we collect the column names - if (set == null) { - set = new HashSet(); - indexColumns.put(indexName, set); - } - set.add(rs.getString("COLUMN_NAME")); - } - } - - return 0; - } - }); + Set columns = new HashSet(); + Map> indexColumns = new HashMap>(); + loadMetaData(ctx, columns, indexColumns); // this is a little silly, but we need a copy for name disambiguation later Set indicesCopy = new HashSet(indexColumns.keySet()); @@ -857,6 +829,65 @@ public class DepotMarshaller }); } + /** + * Loads up the metadata for our database table: the names of our columns and indices. + */ + protected void loadMetaData (PersistenceContext ctx, final Set columns, + final Map> indexColumns) + throws PersistenceException + { + ctx.invoke(new Modifier() { + public int invoke (Connection conn) throws SQLException { + DatabaseMetaData meta = conn.getMetaData(); + ResultSet rs = meta.getColumns(null, null, getTableName(), "%"); + while (rs.next()) { + columns.add(rs.getString("COLUMN_NAME")); + } + + if (indexColumns != null) { + rs = meta.getIndexInfo(null, null, getTableName(), false, false); + while (rs.next()) { + String indexName = rs.getString("INDEX_NAME"); + Set set = indexColumns.get(indexName); + if (rs.getBoolean("NON_UNIQUE")) { + // not a unique index: just make sure there's an entry in the keyset + if (set == null) { + indexColumns.put(indexName, null); + } + + } else { + // for unique indices we collect the column names + if (set == null) { + set = new HashSet(); + indexColumns.put(indexName, set); + } + set.add(rs.getString("COLUMN_NAME")); + } + } + } + + return 0; + } + }); + } + + /** + * Checks that there are no database columns for which we no longer have Java fields. + */ + protected void verifySchemasMatch (PersistenceContext ctx) + throws PersistenceException + { + Set columns = new HashSet(); + loadMetaData(ctx, columns, null); + for (String fname : _columnFields) { + FieldMarshaller fmarsh = _fields.get(fname); + columns.remove(fmarsh.getColumnName()); + } + for (String column : columns) { + log.warning(getTableName() + " contains stale column '" + column + "'."); + } + } + protected String getPrimaryKeyColumns () { String[] pkcols = new String[_pkColumns.size()];