Merge pull request #7945 from kinglozzer/indexual-healing

FIX: Don't auto-generate indexes for Text field types (fixes #7900)
This commit is contained in:
Robbie Averill 2018-04-10 08:23:18 +12:00 committed by GitHub
commit cd7cb225a6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 37 additions and 4 deletions

View File

@ -1789,7 +1789,7 @@ class DataObject extends ViewableData implements DataObjectInterface, i18nEntity
$result = $result->alterDataQuery(function($query) use ($extraFields) {
$query->setQueryParam('Component.ExtraFields', $extraFields);
});
if($this->model) $result->setDataModel($this->model);
$this->extend('updateManyManyComponents', $result);
@ -3421,7 +3421,15 @@ class DataObject extends ViewableData implements DataObjectInterface, i18nEntity
continue;
}
if ($this->hasOwnTableDatabaseField($column) && !array_key_exists($column, $indexes)) {
list($fieldType) = SS_Object::parse_class_spec($this->db($column));
$isAutoIndexable = (Config::inst()->get($fieldType, 'auto_indexable')
|| Config::inst()->get("DB{$fieldType}", 'auto_indexable'));
if (
$this->hasOwnTableDatabaseField($column)
&& !array_key_exists($column, $indexes)
&& $isAutoIndexable
) {
$indexes[$column] = true;
}
} catch (InvalidArgumentException $e) { }
@ -3754,7 +3762,7 @@ class DataObject extends ViewableData implements DataObjectInterface, i18nEntity
*/
public function summaryFields() {
$rawFields = $this->stat('summary_fields');
$fields = array();
// Merge associative / numeric keys
if (is_array($rawFields)) {

View File

@ -60,6 +60,14 @@ abstract class DBField extends ViewableData {
*/
private static $default_search_filter_class = 'PartialMatchFilter';
/**
* Flag to indicate whether this data type is safe to automatically generate an index for
*
* @var bool
* @config
*/
private static $auto_indexable = true;
/**
* @var $default mixed Default-value in the database.
* Might be overridden on DataObject-level, but still useful for setting defaults on

View File

@ -32,6 +32,8 @@ class Text extends StringField {
'LimitWordCountXML' => 'HTMLText',
);
private static $auto_indexable = false;
/**
* (non-PHPdoc)
* @see DBField::requireField()

View File

@ -216,6 +216,11 @@ class DataObjectSchemaGenerationTest extends SapphireTest {
$indexes = $object->databaseIndexes();
$this->assertArrayHasKey('Sort', $indexes);
$this->assertEquals('unique', $indexes['Sort']);
// make sure Text fields don't get indexes automatically - these can be db-driver dependent
$object = new DataObjectSchemaGenerationTest_SortedByText();
$indexes = $object->databaseIndexes();
$this->assertArrayNotHasKey('Description', $indexes);
}
}
@ -265,4 +270,14 @@ class DataObjectSchemaGenerationTest_Sorted extends DataObject implements TestOn
);
private static $default_sort = "Sort";
}
}
class DataObjectSchemaGenerationTest_SortedByText extends DataObject implements TestOnly {
private static $db = array(
'Title' => 'Varchar',
'Description' => 'Text',
'Sort' => 'Int',
);
private static $default_sort = "Description";
}