From 345693a6e069aa4c9098a36d16e008929f7b1912 Mon Sep 17 00:00:00 2001 From: Geoff Munn Date: Tue, 17 Feb 2009 02:49:39 +0000 Subject: [PATCH] API CHANGE: RequireField now no longer returns *all* fields as needing to be changed in Postgres. git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@71927 467b73ca-7a2a-4603-9d3b-597d59a354a9 --- core/model/Database.php | 44 +++++++++++++++++++++++++++++------------ 1 file changed, 31 insertions(+), 13 deletions(-) diff --git a/core/model/Database.php b/core/model/Database.php index 9bf40e304..6084b2071 100755 --- a/core/model/Database.php +++ b/core/model/Database.php @@ -295,6 +295,7 @@ abstract class Database extends Object { if(!$newTable && !isset($this->indexList[$table])) { $this->indexList[$table] = $this->indexList($table); } + if($newTable || !isset($this->indexList[$table][$index])) { $this->transCreateIndex($table, $index, $spec); Database::alteration_message("Index $table.$index: created as $spec","created"); @@ -315,18 +316,23 @@ abstract class Database extends Object { * need to take care of database abstraction in your DBField subclass. */ function requireField($table, $field, $spec) { + //TODO: this is starting to get extremely fragmented. + //There are two different versions of $spec floating around, and their content changes depending + //on how they are structured. This needs to be tidied up. + $newTable = false; Profiler::mark('requireField'); // backwards compatibility patch for pre 2.4 requireField() calls + $spec_orig=$spec; if(!is_string($spec)) { // TODO: This is tempororary $spec['parts']['name'] = $field; + $spec_orig['parts']['name'] = $field; //Convert the $spec array into a database-specific string - $spec=DB::getConn()->$spec['type']($spec['parts']); + $spec=DB::getConn()->$spec['type']($spec['parts'], true); } - // Collations didn't come in until MySQL 4.1. Anything earlier will throw a syntax error if you try and use // collations. if(!$this->supportsCollations()) { @@ -338,20 +344,33 @@ abstract class Database extends Object { $this->fieldList[$table] = $this->fieldList($table); } - if($newTable || !isset($this->fieldList[$table][$field])) { + if(is_array($spec)) + $specValue=$spec['data_type']; + else $specValue=$spec; + + if(is_array($this->fieldList[$table][$field])) + $fieldValue=$this->fieldList[$table][$field]['data_type']; + else $fieldValue=$this->fieldList[$table][$field]; + + if(is_array($spec_orig)) + $spec_orig=DB::getConn()->$spec_orig['type']($spec_orig['parts']); + + if($newTable || $fieldValue=='') { Profiler::mark('createField'); - $this->transCreateField($table, $field, $spec); + + $this->transCreateField($table, $field, $spec_orig); Profiler::unmark('createField'); - Database::alteration_message("Field $table.$field: created as $spec","created"); - } else if($this->fieldList[$table][$field] != DB::getConn()->convertIndexSpec($spec)) { + Database::alteration_message("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. // 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($spec, 0, 4) == "enum") { - $newStr = preg_replace("/(^enum\s*\(')|('$\).*)/i","",$spec); + + if(substr($specValue, 0, 4) == "enum") { + $newStr = preg_replace("/(^enum\s*\(')|('$\).*)/i","",$spec_orig); $new = preg_split("/'\s*,\s*'/", $newStr); - $oldStr = preg_replace("/(^enum\s*\(')|('$\).*)/i","",$this->fieldList[$table][$field]); + $oldStr = preg_replace("/(^enum\s*\(')|('$\).*)/i","", $fieldValue); $old = preg_split("/'\s*,\s*'/", $newStr); $holder = array(); @@ -361,7 +380,7 @@ abstract class Database extends Object { } } if(count($holder)) { - $default = explode('default ', $spec); + $default = explode('default ', $spec_orig); $default = $default[1]; if($default == "'SiteTree'") $default = "'Page'"; $query = "UPDATE \"$table\" SET $field=$default WHERE $field IN ("; @@ -375,10 +394,9 @@ abstract class Database extends Object { } } Profiler::mark('alterField'); - $this->transAlterField($table, $field, $spec); + $this->transAlterField($table, $field, $spec_orig); Profiler::unmark('alterField'); - $spec_msg=DB::getConn()->convertIndexSpec($spec); - Database::alteration_message("Field $table.$field: changed to $spec_msg (from {$this->fieldList[$table][$field]})","changed"); + Database::alteration_message("Field $table.$field: changed to $spec_orig (from {$fieldValue})","changed"); } Profiler::unmark('requireField'); }