From adf914e156d1de0828bfc0110bcfe4ebe1824e80 Mon Sep 17 00:00:00 2001 From: Michael Bayne Date: Fri, 18 Feb 2011 00:35:58 +0000 Subject: [PATCH] Wired up handling of primary key changes (with some reorganization of a patch from Nathan). If we need to add our primary key, we do so, if we need to remove it, we do so, and only then do we consider whether we need to migrate it (in which case we drop and re-add it). --- .../depot/impl/DepotMarshaller.java | 53 ++++++++++--------- 1 file changed, 28 insertions(+), 25 deletions(-) diff --git a/src/main/java/com/samskivert/depot/impl/DepotMarshaller.java b/src/main/java/com/samskivert/depot/impl/DepotMarshaller.java index 62382e2..c3df91d 100644 --- a/src/main/java/com/samskivert/depot/impl/DepotMarshaller.java +++ b/src/main/java/com/samskivert/depot/impl/DepotMarshaller.java @@ -795,30 +795,7 @@ public class DepotMarshaller implements QueryMarshal } } - // if the primary key has changed size or signature, we have to drop and readd it - boolean keyMatch; - if (metaData.pkColumns.size() == _pkColumns.size()) { - keyMatch = true; - for (FieldMarshaller column : _pkColumns) { - keyMatch &= metaData.pkColumns.contains(column.getColumnName()); - } - } else { - keyMatch = false; - } - - if (!keyMatch) { - log.info("Primary key has changed: dropping."); - ctx.invoke(new Modifier() { - @Override protected int invoke (Connection conn, DatabaseLiaison liaison) - throws SQLException { - // TODO - return 0; - } - }); - - } - - // add or remove the primary key as needed + // add, remove or change the primary key as needed if (hasPrimaryKey() && metaData.pkName == null) { log.info("Adding primary key."); ctx.invoke(new Modifier() { @@ -831,8 +808,8 @@ public class DepotMarshaller implements QueryMarshal }); } else if (!hasPrimaryKey() && metaData.pkName != null) { - final String pkName = metaData.pkName; log.info("Dropping primary key: " + pkName); + final String pkName = metaData.pkName; ctx.invoke(new Modifier() { @Override protected int invoke (Connection conn, DatabaseLiaison liaison) throws SQLException { @@ -840,6 +817,19 @@ public class DepotMarshaller implements QueryMarshal return 0; } }); + + } else if (!metaData.pkMatches(_pkColumns)) { + log.info("Primary key has changed: dropping and readding."); + final String pkName = metaData.pkName; + ctx.invoke(new Modifier() { + @Override protected int invoke (Connection conn, DatabaseLiaison liaison) + throws SQLException { + liaison.dropPrimaryKey(conn, getTableName(), pkName); + liaison.addPrimaryKey( + conn, getTableName(), fieldsToColumns(getPrimaryKeyFields())); + return 0; + } + }); } // add any named indices that exist on the record but not yet on the table @@ -1095,6 +1085,19 @@ public class DepotMarshaller implements QueryMarshal } } + public boolean pkMatches (List> declaredPkColumns) + { + if (pkColumns.size() != declaredPkColumns.size()) { + return false; + } + for (FieldMarshaller column : declaredPkColumns) { + if (!pkColumns.contains(column.getColumnName())) { + return false; + } + } + return true; + } + @Override public String toString () {