mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
Merge pull request #4261 from tractorcow/pulls/3/fix-orphaned-delete
BUG Fix deletion of orphaned versioned records when a parent _versions has been deleted
This commit is contained in:
commit
65fce1cb07
@ -486,39 +486,39 @@ class Versioned extends DataExtension implements TemplateGlobalProvider {
|
|||||||
if(!$isRootClass && ($versionedTables = ClassInfo::dataClassesFor($table))) {
|
if(!$isRootClass && ($versionedTables = ClassInfo::dataClassesFor($table))) {
|
||||||
|
|
||||||
foreach($versionedTables as $child) {
|
foreach($versionedTables as $child) {
|
||||||
if($table == $child) break; // only need subclasses
|
if($table === $child) break; // only need subclasses
|
||||||
|
|
||||||
$count = DB::query("
|
// Select all orphaned version records
|
||||||
SELECT COUNT(*) FROM \"{$table}_versions\"
|
$orphanedQuery = SQLSelect::create()
|
||||||
LEFT JOIN \"{$child}_versions\"
|
->selectField("\"{$table}_versions\".\"ID\"")
|
||||||
ON \"{$child}_versions\".\"RecordID\" = \"{$table}_versions\".\"RecordID\"
|
->setFrom("\"{$table}_versions\"");
|
||||||
AND \"{$child}_versions\".\"Version\" = \"{$table}_versions\".\"Version\"
|
|
||||||
WHERE \"{$child}_versions\".\"ID\" IS NULL
|
|
||||||
")->value();
|
|
||||||
|
|
||||||
|
// If we have a parent table limit orphaned records
|
||||||
|
// to only those that exist in this
|
||||||
|
if(DB::get_schema()->hasTable("{$child}_versions")) {
|
||||||
|
$orphanedQuery
|
||||||
|
->addLeftJoin(
|
||||||
|
"{$child}_versions",
|
||||||
|
"\"{$child}_versions\".\"RecordID\" = \"{$table}_versions\".\"RecordID\"
|
||||||
|
AND \"{$child}_versions\".\"Version\" = \"{$table}_versions\".\"Version\""
|
||||||
|
)
|
||||||
|
->addWhere("\"{$child}_versions\".\"ID\" IS NULL");
|
||||||
|
}
|
||||||
|
|
||||||
|
$count = $orphanedQuery->count();
|
||||||
if($count > 0) {
|
if($count > 0) {
|
||||||
DB::alteration_message("Removing orphaned versioned records", "deleted");
|
DB::alteration_message("Removing {$count} orphaned versioned records", "deleted");
|
||||||
|
$ids = $orphanedQuery->execute()->column();
|
||||||
$affectedIDs = DB::query("
|
foreach($ids as $id) {
|
||||||
SELECT \"{$table}_versions\".\"ID\" FROM \"{$table}_versions\"
|
|
||||||
LEFT JOIN \"{$child}_versions\"
|
|
||||||
ON \"{$child}_versions\".\"RecordID\" = \"{$table}_versions\".\"RecordID\"
|
|
||||||
AND \"{$child}_versions\".\"Version\" = \"{$table}_versions\".\"Version\"
|
|
||||||
WHERE \"{$child}_versions\".\"ID\" IS NULL
|
|
||||||
")->column();
|
|
||||||
|
|
||||||
if(is_array($affectedIDs)) {
|
|
||||||
foreach($affectedIDs as $key => $value) {
|
|
||||||
DB::prepared_query(
|
DB::prepared_query(
|
||||||
"DELETE FROM \"{$table}_versions\" WHERE \"ID\" = ?",
|
"DELETE FROM \"{$table}_versions\" WHERE \"ID\" = ?",
|
||||||
array($value)
|
array($id)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
DB::require_table("{$table}_versions", $versionFields, $versionIndexes, true, $options);
|
DB::require_table("{$table}_versions", $versionFields, $versionIndexes, true, $options);
|
||||||
} else {
|
} else {
|
||||||
|
Loading…
Reference in New Issue
Block a user