From ebd3fb6526eb3ee9359111e548d9f6b6e0323e97 Mon Sep 17 00:00:00 2001 From: Loz Calver Date: Tue, 20 Mar 2018 16:49:36 +0000 Subject: [PATCH] FIX: Don't auto-generate indexes for Text field types (fixes #7900) --- model/DataObject.php | 14 +++++++++++--- model/fieldtypes/DBField.php | 8 ++++++++ model/fieldtypes/Text.php | 2 ++ tests/model/DataObjectSchemaGenerationTest.php | 17 ++++++++++++++++- 4 files changed, 37 insertions(+), 4 deletions(-) diff --git a/model/DataObject.php b/model/DataObject.php index f2946e98f..965d4c5ad 100644 --- a/model/DataObject.php +++ b/model/DataObject.php @@ -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)) { diff --git a/model/fieldtypes/DBField.php b/model/fieldtypes/DBField.php index 8e0616857..acc2ea7ec 100644 --- a/model/fieldtypes/DBField.php +++ b/model/fieldtypes/DBField.php @@ -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 diff --git a/model/fieldtypes/Text.php b/model/fieldtypes/Text.php index 7dbc2ed61..c0313d7f7 100644 --- a/model/fieldtypes/Text.php +++ b/model/fieldtypes/Text.php @@ -32,6 +32,8 @@ class Text extends StringField { 'LimitWordCountXML' => 'HTMLText', ); + private static $auto_indexable = false; + /** * (non-PHPdoc) * @see DBField::requireField() diff --git a/tests/model/DataObjectSchemaGenerationTest.php b/tests/model/DataObjectSchemaGenerationTest.php index 2641c2268..4ba8e719a 100644 --- a/tests/model/DataObjectSchemaGenerationTest.php +++ b/tests/model/DataObjectSchemaGenerationTest.php @@ -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"; -} \ No newline at end of file +} + +class DataObjectSchemaGenerationTest_SortedByText extends DataObject implements TestOnly { + private static $db = array( + 'Title' => 'Varchar', + 'Description' => 'Text', + 'Sort' => 'Int', + ); + + private static $default_sort = "Description"; +}