From a2055d1d675d8cc35c4bd3d263ba463e2de84740 Mon Sep 17 00:00:00 2001 From: Ingo Schommer Date: Tue, 13 Apr 2010 01:49:58 +0000 Subject: [PATCH] MINOR: use ENUM hack for dealing with SET legacy values (from r98042) git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@102569 467b73ca-7a2a-4603-9d3b-597d59a354a9 --- core/model/Database.php | 47 +++++++++++++++++++++-------------------- 1 file changed, 24 insertions(+), 23 deletions(-) diff --git a/core/model/Database.php b/core/model/Database.php index 8f7de074b..51b99889c 100755 --- a/core/model/Database.php +++ b/core/model/Database.php @@ -475,35 +475,36 @@ abstract class SS_Database { Profiler::unmark('createField'); $this->alterationMessage("Field $table.$field: created as $spec_orig","created"); } else if($fieldValue != $specValue) { - // If enums are being modified, then we need to fix existing data in the table. + // If enums/sets are being modified, then we need to fix existing data in the table. // Update any records where the enum is set to a legacy value to be set to the default. // One hard-coded exception is SiteTree - the default for this is Page. - - if(substr($specValue, 0, 4) == "enum") { - $newStr = preg_replace("/(^enum\s*\(')|('$\).*)/i","",$spec_orig); - $new = preg_split("/'\s*,\s*'/", $newStr); + foreach(array('enum','set') as $enumtype) { + if(preg_match("/^$enumtype/i",$specValue)) { + $newStr = preg_replace("/(^$enumtype\s*\(')|('$\).*)/i","",$spec_orig); + $new = preg_split("/'\s*,\s*'/", $newStr); - $oldStr = preg_replace("/(^enum\s*\(')|('$\).*)/i","", $fieldValue); - $old = preg_split("/'\s*,\s*'/", $newStr); + $oldStr = preg_replace("/(^$enumtype\s*\(')|('$\).*)/i","", $fieldValue); + $old = preg_split("/'\s*,\s*'/", $newStr); - $holder = array(); - foreach($old as $check) { - if(!in_array($check, $new)) { - $holder[] = $check; + $holder = array(); + foreach($old as $check) { + if(!in_array($check, $new)) { + $holder[] = $check; + } } - } - if(count($holder)) { - $default = explode('default ', $spec_orig); - $default = $default[1]; - if($default == "'SiteTree'") $default = "'Page'"; - $query = "UPDATE \"$table\" SET $field=$default WHERE $field IN ("; - for($i=0;$i+1alterationMessage("Changed $amount rows to default value of field $field (Value: $default)"); } - $query .= "'{$holder[$i]}')"; - DB::query($query); - $amount = DB::affectedRows(); - $this->alterationMessage("Changed $amount rows to default value of field $field (Value: $default)"); } } Profiler::mark('alterField');