mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 12:05:37 +00:00
MINOR: use ENUM hack for dealing with SET legacy values
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/branches/2.4@98042 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
parent
9b3fe76269
commit
bcd9e763ec
@ -475,35 +475,36 @@ abstract class SS_Database {
|
|||||||
Profiler::unmark('createField');
|
Profiler::unmark('createField');
|
||||||
$this->alterationMessage("Field $table.$field: created as $spec_orig","created");
|
$this->alterationMessage("Field $table.$field: created as $spec_orig","created");
|
||||||
} else if($fieldValue != $specValue) {
|
} 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.
|
// 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.
|
// One hard-coded exception is SiteTree - the default for this is Page.
|
||||||
|
foreach(array('enum','set') as $enumtype) {
|
||||||
if(substr($specValue, 0, 4) == "enum") {
|
if(preg_match("/^$enumtype/i",$specValue)) {
|
||||||
$newStr = preg_replace("/(^enum\s*\(')|('$\).*)/i","",$spec_orig);
|
$newStr = preg_replace("/(^$enumtype\s*\(')|('$\).*)/i","",$spec_orig);
|
||||||
$new = preg_split("/'\s*,\s*'/", $newStr);
|
$new = preg_split("/'\s*,\s*'/", $newStr);
|
||||||
|
|
||||||
$oldStr = preg_replace("/(^enum\s*\(')|('$\).*)/i","", $fieldValue);
|
$oldStr = preg_replace("/(^$enumtype\s*\(')|('$\).*)/i","", $fieldValue);
|
||||||
$old = preg_split("/'\s*,\s*'/", $newStr);
|
$old = preg_split("/'\s*,\s*'/", $newStr);
|
||||||
|
|
||||||
$holder = array();
|
$holder = array();
|
||||||
foreach($old as $check) {
|
foreach($old as $check) {
|
||||||
if(!in_array($check, $new)) {
|
if(!in_array($check, $new)) {
|
||||||
$holder[] = $check;
|
$holder[] = $check;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
if(count($holder)) {
|
||||||
if(count($holder)) {
|
$default = explode('default ', $spec_orig);
|
||||||
$default = explode('default ', $spec_orig);
|
$default = $default[1];
|
||||||
$default = $default[1];
|
if($default == "'SiteTree'") $default = "'Page'";
|
||||||
if($default == "'SiteTree'") $default = "'Page'";
|
$query = "UPDATE \"$table\" SET $field=$default WHERE $field IN (";
|
||||||
$query = "UPDATE \"$table\" SET $field=$default WHERE $field IN (";
|
for($i=0;$i+1<count($holder);$i++) {
|
||||||
for($i=0;$i+1<count($holder);$i++) {
|
$query .= "'{$holder[$i]}', ";
|
||||||
$query .= "'{$holder[$i]}', ";
|
}
|
||||||
|
$query .= "'{$holder[$i]}')";
|
||||||
|
DB::query($query);
|
||||||
|
$amount = DB::affectedRows();
|
||||||
|
$this->alterationMessage("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');
|
Profiler::mark('alterField');
|
||||||
|
Loading…
x
Reference in New Issue
Block a user