Permalink
Browse files

fixed setting of empty secondary fields during restore (bug 6113)

  • Loading branch information...
1 parent 74b29ac commit e48bc9eb252cd308a23f8024b3208ceab86202dd gbateson committed Jul 15, 2006
Showing with 52 additions and 19 deletions.
  1. +52 −19 mod/hotpot/restorelib.php
@@ -283,6 +283,15 @@ function hotpot_restore_record(&$restore, $status, &$xml, $table, $foreign_keys,
// the name of the secondary key field, if any, in the current $record.
// If this field is specified, then the current record will only be added
// if the $record->$secondarykey value does not already exist in $table
+
+ // maintain a cache of info on table columns
+ static $table_columns = array();
+ if (empty($table_columns[$table])) {
+ global $CFG, $db;
+ $table_columns[$table] = $db->MetaColumns("$CFG->prefix$table");
+ }
+
+ // get values for fields in this record
unset($record);
$TAGS = array_keys($xml);
foreach ($TAGS as $TAG) {
@@ -292,39 +301,63 @@ function hotpot_restore_record(&$restore, $status, &$xml, $table, $foreign_keys,
$record->$tag = backup_todb($value);
}
}
+
+ // update secondary keys, if any
$ok = true;
foreach ($foreign_keys as $key=>$value) {
if (is_numeric($value)) {
$record->$key = $value;
} else {
- if (empty($record->$key)) {
- $record->$key = NULL;
- } else {
- $key_table = $value;
- $new_ids = array();
+ $key_table = $value;
+ $new_ids = array();
+ if (isset($record->$key)) {
$old_ids = explode(',', $record->$key);
foreach ($old_ids as $old_id) {
- $key_record = backup_getid($restore->backup_unique_code, $key_table, $old_id);
- if ($key_record) {
- $new_ids[] = $key_record->new_id;
+ if (empty($old_id)) {
+ // do nothing
} else {
- // foreign key could not be updated
- if (!defined('RESTORE_SILENTLY')) {
- print "<ul><li><b>Warning:</b><br>Foreign key could not be updated:<br>";
- print "'$key_table' record (old id=$old_id) is missing from backup data<br>";
- print "'$table' record ";
- if (isset($record->id)) {
- print "(old id=$record->id) ";
+ $key_record = backup_getid($restore->backup_unique_code, $key_table, $old_id);
+ if ($key_record) {
+ $new_ids[] = $key_record->new_id;
+ } else {
+ // foreign key could not be updated
+ if (!defined('RESTORE_SILENTLY')) {
+ print "<ul><li><b>Warning:</b><br>Foreign key could not be updated:<br>";
+ print "'$key_table' record (old id=$old_id) is missing from backup data<br>";
+ print "'$table' record ";
+ if (isset($record->id)) {
+ print "(old id=$record->id) ";
+ }
+ print "was not restored</li></ul>";
}
- print "was not restored</li></ul>";
+ $ok = false;
}
- $ok = false;
}
}
- $record->$key = implode(',', $new_ids);
+ }
+ $record->$key = implode(',', $new_ids);
+ }
+ }
+
+ // check all "not null" fields have been set
+ foreach ($table_columns[$table] as $column) {
+ if ($column->not_null) {
+ $name = $column->name;
+ if ($name<>'id' && empty($record->$name)) {
+ if (isset($column->default_value)) {
+ $default = $column->default_value;
+ } else {
+ if (preg_match('/int|decimal|double|float|time|year/i', $column->type)) {
+ $default = 0;
+ } else {
+ $default = '';
+ }
+ }
+ $record->$name = $default;
}
}
}
+
// check everything is OK so far
if ($ok && isset($record)) {
// store old record id, if necessary
@@ -343,7 +376,7 @@ function hotpot_restore_record(&$restore, $status, &$xml, $table, $foreign_keys,
}
if (empty($record->id)) {
// add the $record (and get new id)
- $record->id = insert_record ($table, $record);
+ $record->id = insert_record($table, $record);
}
// check $record was added (or found)
if (is_numeric($record->id)) {

0 comments on commit e48bc9e

Please sign in to comment.