mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-09-07 10:11:17 +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,34 +486,34 @@ 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\"
|
DB::prepared_query(
|
||||||
LEFT JOIN \"{$child}_versions\"
|
"DELETE FROM \"{$table}_versions\" WHERE \"ID\" = ?",
|
||||||
ON \"{$child}_versions\".\"RecordID\" = \"{$table}_versions\".\"RecordID\"
|
array($id)
|
||||||
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(
|
|
||||||
"DELETE FROM \"{$table}_versions\" WHERE \"ID\" = ?",
|
|
||||||
array($value)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user