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).
This commit is contained in:
@@ -795,30 +795,7 @@ public class DepotMarshaller<T extends PersistentRecord> 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<T extends PersistentRecord> 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<T extends PersistentRecord> 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<T extends PersistentRecord> implements QueryMarshal
|
||||
}
|
||||
}
|
||||
|
||||
public boolean pkMatches (List<FieldMarshaller<?>> 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 ()
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user