API CHANGE: fulltext and unique indexes are now arrays

API CHANGE: db/build now no longer notifies you of changes which haven't actually happened.

git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@69303 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
Geoff Munn 2008-12-17 00:40:24 +00:00
parent 3d2d79987b
commit fe0a640c39
5 changed files with 56 additions and 17 deletions

View File

@ -278,10 +278,14 @@ abstract class Database extends Object {
function requireIndex($table, $index, $spec) {
$newTable = false;
//DB Abstraction: remove this ===true option as a possibility?
if($spec === true) {
$spec = "($index)";
}
$spec = ereg_replace(" *, *",",",$spec);
//Indexes specified as arrays cannot be checked with this line: (it flattens out the array)
if(!is_array($spec))
$spec = ereg_replace(" *, *",",",$spec);
if(!isset($this->tableList[strtolower($table)])) $newTable = true;
@ -291,9 +295,10 @@ abstract class Database extends Object {
if($newTable || !isset($this->indexList[$table][$index])) {
$this->transCreateIndex($table, $index, $spec);
Database::alteration_message("Index $table.$index: created as $spec","created");
} else if($this->indexList[$table][$index] != $spec) {
} else if($this->indexList[$table][$index] != DB::getConn()->convertIndexSpec($spec)) {
$this->transAlterIndex($table, $index, $spec);
Database::alteration_message("Index $table.$index: changed to $spec <i style=\"color: #AAA\">(from {$this->indexList[$table][$index]})</i>","changed");
$spec_msg=DB::getConn()->convertIndexSpec($spec);
Database::alteration_message("Index $table.$index: changed to $spec_msg <i style=\"color: #AAA\">(from {$this->indexList[$table][$index]})</i>","changed");
}
}
@ -329,7 +334,7 @@ abstract class Database extends Object {
$this->transCreateField($table, $field, $spec);
Profiler::unmark('createField');
Database::alteration_message("Field $table.$field: created as $spec","created");
} else if($this->fieldList[$table][$field] != $spec) {
} else if($this->fieldList[$table][$field] != DB::getConn()->convertIndexSpec($spec)) {
// 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.
@ -363,7 +368,8 @@ abstract class Database extends Object {
Profiler::mark('alterField');
$this->transAlterField($table, $field, $spec);
Profiler::unmark('alterField');
Database::alteration_message("Field $table.$field: changed to $spec <i style=\"color: #AAA\">(from {$this->fieldList[$table][$field]})</i>","changed");
$spec_msg=DB::getConn()->convertIndexSpec($spec);
Database::alteration_message("Field $table.$field: changed to $spec_msg <i style=\"color: #AAA\">(from {$this->fieldList[$table][$field]})</i>","changed");
}
Profiler::unmark('requireField');
}

View File

@ -285,7 +285,10 @@ class MySQLDatabase extends Database {
}
if($field['Default'] || $field['Default'] === "0") {
$fieldSpec .= " default '" . addslashes($field['Default']) . "'";
if(is_numeric($field['Default']))
$fieldSpec .= " default " . addslashes($field['Default']);
else
$fieldSpec .= " default '" . addslashes($field['Default']) . "'";
}
if($field['Extra']) $fieldSpec .= " $field[Extra]";
@ -304,13 +307,39 @@ class MySQLDatabase extends Database {
$this->query("ALTER TABLE \"$tableName\" ADD " . $this->getIndexSqlDefinition($indexName, $indexSpec));
}
/*
* This takes the index spec which has been provided by a class (ie static $indexes = blah blah)
* and turns it into a proper string.
* Some indexes may be arrays, such as fulltext and unique indexes, and this allows database-specific
* arrays to be created.
*/
public function convertIndexSpec($indexSpec){
if(is_array($indexSpec)){
//Here we create a db-specific version of whatever index we need to create.
switch($indexSpec['type']){
case 'fulltext':
$indexSpec='fulltext (' . str_replace(' ', '', $indexSpec['value']) . ')';
break;
case 'unique':
$indexSpec='unique (' . $indexSpec['value'] . ')';
break;
}
}
return $indexSpec;
}
protected function getIndexSqlDefinition($indexName, $indexSpec) {
$indexSpec = trim($indexSpec);
$indexSpec=$this->convertIndexSpec($indexSpec);
$indexSpec = trim($indexSpec);
if($indexSpec[0] != '(') list($indexType, $indexFields) = explode(' ',$indexSpec,2);
else $indexFields = $indexSpec;
if(!isset($indexType)) {
$indexType = "index";
}
return "$indexType \"$indexName\" $indexFields";
}
@ -321,7 +350,10 @@ class MySQLDatabase extends Database {
* @param string $indexSpec The specification of the index, see Database::requireIndex() for more details.
*/
public function alterIndex($tableName, $indexName, $indexSpec) {
$indexSpec = trim($indexSpec);
$indexSpec=$this->convertIndexSpec($indexSpec);
$indexSpec = trim($indexSpec);
if($indexSpec[0] != '(') {
list($indexType, $indexFields) = explode(' ',$indexSpec,2);
} else {
@ -450,7 +482,7 @@ class MySQLDatabase extends Database {
//$parts=Array('datatype'=>'enum', 'enums'=>$this->enum, 'character set'=>'utf8', 'collate'=> 'utf8_general_ci', 'default'=>$this->default);
//DB::requireField($this->tableName, $this->name, "enum('" . implode("','", $this->enum) . "') character set utf8 collate utf8_general_ci default '{$this->default}'");
return 'enum(\'' . implode('\', \'', $values['enums']) . '\') character set utf8 collate utf8_general_ci default \'' . $values['default'] . '\'';
return 'enum(\'' . implode('\',\'', $values['enums']) . '\') character set utf8 collate utf8_general_ci default \'' . $values['default'] . '\'';
}
/**

View File

@ -86,8 +86,8 @@ class SiteTree extends DataObject implements PermissionProvider {
);
static $indexes = array(
"SearchFields" => "fulltext (Title, MenuTitle, Content, MetaTitle, MetaDescription, MetaKeywords)",
"TitleSearchFields" => "fulltext (Title)",
"SearchFields" => Array('type'=>'fulltext', 'value'=>'Title, MenuTitle, Content, MetaTitle, MetaDescription, MetaKeywords'),
"TitleSearchFields" => Array('type'=>'fulltext', 'value'=>'Title'),
"URLSegment" => true,
);
@ -1202,7 +1202,7 @@ class SiteTree extends DataObject implements PermissionProvider {
)
//new NamedLabelField("Status", $message, "pageStatusMessage", true)
);
if(!Permission::check('SITETREE_GRANT_ACCESS')) {
$fields->makeFieldReadonly($viewersOptionsField);
$fields->makeFieldReadonly($viewerGroupsField);
@ -1314,12 +1314,12 @@ class SiteTree extends DataObject implements PermissionProvider {
$actions->push(new FormAction('save',_t('CMSMain.SAVE','Save')));
}
}
if($this->canPublish()) {
// "publish"
$actions->push(new FormAction('publish', _t('SiteTree.BUTTONSAVEPUBLISH', 'Save and Publish')));
}
// getCMSActions() can be extended with updateCMSActions() on a decorator
$this->extend('updateCMSActions', $actions);
@ -1692,7 +1692,7 @@ class SiteTree extends DataObject implements PermissionProvider {
public static function enableCMSFieldsExtensions() {
self::$runCMSFieldsExtensions = true;
}
function providePermissions() {
return array(
'SITETREE_GRANT_ACCESS' => _t(

View File

@ -25,7 +25,7 @@ class File extends DataObject {
);
static $indexes = array(
"SearchFields" => "fulltext (Filename,Title,Content)",
"SearchFields" => Array('type'=>'fulltext', 'value'=>'Filename,Title,Content'),
);
static $has_one = array(

View File

@ -39,7 +39,8 @@ class Member extends DataObject {
static $indexes = array(
'Email' => true,
'AutoLoginHash' => 'unique (AutoLoginHash)'
//'AutoLoginHash' => 'unique (AutoLoginHash)'
'AutoLoginHash' => Array('type'=>'unique', 'value'=>'AutoLoginHash')
);
static $notify_password_change = false;