BUGFIX: Add a unique index to SiteTree_versions.RecordID+Version. Fix saving methods to support this.

git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/branches/2.4@106079 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
Sam Minnee 2010-06-02 06:37:54 +00:00
parent e1753a35e5
commit 3f6005fd6a

View File

@ -287,13 +287,28 @@ class Versioned extends DataObjectDecorator {
$versionIndexes = array_merge( $versionIndexes = array_merge(
array( array(
'RecordID_Version' => '(RecordID, Version)', 'RecordID_Version' => 'unique (RecordID, Version)',
'RecordID' => true, 'RecordID' => true,
'Version' => true, 'Version' => true,
), ),
(array)$indexes (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); DB::requireTable("{$table}_versions", $versionFields, $versionIndexes);
} else { } else {
@ -334,7 +349,6 @@ class Versioned extends DataObjectDecorator {
if($this->migratingVersion) { if($this->migratingVersion) {
$manipulation[$table]['fields']['Version'] = $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. // 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]); 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 // Add the new version # back into the data object, for accessing after this write
if(isset($thisVersion)) $this->owner->Version = str_replace("'","",$thisVersion); 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->where[] = "\"{$baseTable}_versions\".\"RecordID\" = '{$this->owner->ID}'";
$query->orderby = ($sort) ? $sort : "\"{$baseTable}_versions\".\"LastEdited\" DESC, \"{$baseTable}_versions\".\"Version\" DESC"; $query->orderby = ($sort) ? $sort : "\"{$baseTable}_versions\".\"LastEdited\" DESC, \"{$baseTable}_versions\".\"Version\" DESC";
$records = $query->execute(); $records = $query->execute();
$versions = new DataObjectSet(); $versions = new DataObjectSet();