Fix constraint checking and only run it if not already constrained
This commit is contained in:
parent
558cbe5b6d
commit
195285ac2f
@ -3087,6 +3087,13 @@ class Notice extends Managed_DataObject
|
|||||||
$schema = Schema::get();
|
$schema = Schema::get();
|
||||||
$schemadef = $schema->getTableDef($table);
|
$schemadef = $schema->getTableDef($table);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Make sure constraints are met before upgrading, if foreign keys
|
||||||
|
* are not already in use.
|
||||||
|
* 2016-03-31
|
||||||
|
*/
|
||||||
|
if (!isset($schemadef['foreign keys'])) {
|
||||||
|
$newschemadef = self::schemaDef();
|
||||||
printfnq("\nConstraint checking Notice table...\n");
|
printfnq("\nConstraint checking Notice table...\n");
|
||||||
/**
|
/**
|
||||||
* Improve typing and make sure no NULL values in any id-related columns are 0
|
* Improve typing and make sure no NULL values in any id-related columns are 0
|
||||||
@ -3100,26 +3107,27 @@ class Notice extends Managed_DataObject
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Make sure constraints are met before upgrading.
|
* This Will find foreign keys which do not fulfill the constraints and fix
|
||||||
* 2016-03-31
|
|
||||||
*
|
|
||||||
* Will find foreign keys which do not fulfill the constraints and fix
|
|
||||||
* where appropriate, such as delete when "repeat_of" ID not found in notice.id
|
* where appropriate, such as delete when "repeat_of" ID not found in notice.id
|
||||||
* or set to NULL for "reply_to" in the same case.
|
* or set to NULL for "reply_to" in the same case.
|
||||||
|
* 2016-03-31
|
||||||
*
|
*
|
||||||
* XXX: How does this work if we would use multicolumn foreign keys?
|
* XXX: How does this work if we would use multicolumn foreign keys?
|
||||||
*/
|
*/
|
||||||
foreach (['reply_to' => 'reset', 'repeat_of' => 'delete', 'profile' => 'delete'] as $field=>$action) {
|
foreach (['reply_to' => 'reset', 'repeat_of' => 'delete', 'profile_id' => 'delete'] as $field=>$action) {
|
||||||
$notice = new Notice();
|
$notice = new Notice();
|
||||||
|
|
||||||
$fkeyname = $notice->tableName().'_'.$field.'_fkey';
|
$fkeyname = $notice->tableName().'_'.$field.'_fkey';
|
||||||
assert(isset($schemadef['foreign keys'][$fkeyname]) && $schemadef['foreign keys'][$fkeyname]);
|
assert(isset($newschemadef['foreign keys'][$fkeyname]));
|
||||||
$foreign_key = $schemadef['foreign keys'][$fkeyname];
|
assert($newschemadef['foreign keys'][$fkeyname]);
|
||||||
printfnq("\n"._ve($schemadef));
|
|
||||||
|
$foreign_key = $newschemadef['foreign keys'][$fkeyname];
|
||||||
$fkeytable = $foreign_key[0];
|
$fkeytable = $foreign_key[0];
|
||||||
assert(isset($foreign_key[1][$field]));
|
assert(isset($foreign_key[1][$field]));
|
||||||
$fkeycol = $foreign_key[1][$field];
|
$fkeycol = $foreign_key[1][$field];
|
||||||
|
|
||||||
|
printfnq("* {$fkeyname} ({$field} => {$fkeytable}.{$fkeycol})\n");
|
||||||
|
|
||||||
// NOTE: Above we set all repeat_of to NULL if they were 0, so this really gets them all.
|
// NOTE: Above we set all repeat_of to NULL if they were 0, so this really gets them all.
|
||||||
$notice->whereAdd(sprintf('%1$s NOT IN (SELECT %2$s FROM %3$s)', $field, $fkeycol, $fkeytable));
|
$notice->whereAdd(sprintf('%1$s NOT IN (SELECT %2$s FROM %3$s)', $field, $fkeycol, $fkeytable));
|
||||||
if ($notice->find()) {
|
if ($notice->find()) {
|
||||||
@ -3127,8 +3135,7 @@ class Notice extends Managed_DataObject
|
|||||||
switch ($action) {
|
switch ($action) {
|
||||||
case 'delete': // since it's a directly dependant notice for an unknown ID we don't want it in our DB
|
case 'delete': // since it's a directly dependant notice for an unknown ID we don't want it in our DB
|
||||||
while ($notice->fetch()) {
|
while ($notice->fetch()) {
|
||||||
// $notice->delete();
|
$notice->delete();
|
||||||
printfnq("\n deleting {$notice->id}");
|
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case 'reset': // just set it to NULL to be compatible with our constraints, if it was related to an unknown ID
|
case 'reset': // just set it to NULL to be compatible with our constraints, if it was related to an unknown ID
|
||||||
@ -3137,6 +3144,7 @@ class Notice extends Managed_DataObject
|
|||||||
settype($id, 'int');
|
settype($id, 'int');
|
||||||
$ids[] = $id;
|
$ids[] = $id;
|
||||||
}
|
}
|
||||||
|
unset($notice);
|
||||||
$notice = new Notice();
|
$notice = new Notice();
|
||||||
$notice->query(sprintf('UPDATE %1$s SET %2$s=NULL WHERE id IN (%3$s)',
|
$notice->query(sprintf('UPDATE %1$s SET %2$s=NULL WHERE id IN (%3$s)',
|
||||||
$notice->escapedTableName(),
|
$notice->escapedTableName(),
|
||||||
@ -3150,6 +3158,7 @@ class Notice extends Managed_DataObject
|
|||||||
}
|
}
|
||||||
unset($notice);
|
unset($notice);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// 2015-09-04 We move Notice location data to Notice_location
|
// 2015-09-04 We move Notice location data to Notice_location
|
||||||
// First we see if we have to do this at all
|
// First we see if we have to do this at all
|
||||||
|
Loading…
Reference in New Issue
Block a user