From 03c3d59ba02104cb26d0fdf6b5ffdf00bfbd3c36 Mon Sep 17 00:00:00 2001 From: Ingo Schommer Date: Fri, 15 Oct 2010 02:55:15 +0000 Subject: [PATCH] BUGFIX: Add a unique index to SiteTree_versions.RecordID+Version. Fix saving methods to support this. (from r106079) git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@112514 467b73ca-7a2a-4603-9d3b-597d59a354a9 --- core/model/Versioned.php | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/core/model/Versioned.php b/core/model/Versioned.php index 7ded12dd5..f0a70150d 100755 --- a/core/model/Versioned.php +++ b/core/model/Versioned.php @@ -287,13 +287,28 @@ class Versioned extends DataObjectDecorator { $versionIndexes = array_merge( array( - 'RecordID_Version' => '(RecordID, Version)', + 'RecordID_Version' => 'unique (RecordID, Version)', 'RecordID' => true, 'Version' => true, ), (array)$indexes ); } + + // Fix data that lacks the uniqueness constraint (since this was added later and + // bugs meant that the constraint was validated) + if(DB::getConn()->hasTable("{$table}_versions")) { + $duplications = DB::query("SELECT MIN(\"ID\") AS \"ID\", \"RecordID\", \"Version\" + FROM \"{$table}_versions\" GROUP BY \"RecordID\", \"Version\" + HAVING COUNT(*) > 1"); + + foreach($duplications as $dup) { + DB::alteration_message("Removing {$table}_versions duplicate data for " + ."{$dup['RecordID']}/{$dup['Version']}" ,"deleted"); + DB::query("DELETE FROM \"{$table}_versions\" WHERE \"RecordID\" = {$dup['RecordID']} + AND \"Version\" = {$dup['Version']} AND \"ID\" != {$dup['ID']}"); + } + } DB::requireTable("{$table}_versions", $versionFields, $versionIndexes); } else { @@ -334,7 +349,6 @@ class Versioned extends DataObjectDecorator { if($this->migratingVersion) { $manipulation[$table]['fields']['Version'] = $this->migratingVersion; - $this->migrateVersion(null); } // If we haven't got a version #, then we're creating a new version. @@ -387,6 +401,9 @@ class Versioned extends DataObjectDecorator { unset($manipulation[$table]); } } + + // Clear the migration flag + if($this->migratingVersion) $this->migrateVersion(null); // Add the new version # back into the data object, for accessing after this write if(isset($thisVersion)) $this->owner->Version = str_replace("'","",$thisVersion); @@ -559,7 +576,6 @@ class Versioned extends DataObjectDecorator { $query->where[] = "\"{$baseTable}_versions\".\"RecordID\" = '{$this->owner->ID}'"; $query->orderby = ($sort) ? $sort : "\"{$baseTable}_versions\".\"LastEdited\" DESC, \"{$baseTable}_versions\".\"Version\" DESC"; - $records = $query->execute(); $versions = new DataObjectSet();