FIX: Don't auto-generate indexes for Text field types (fixes #7900)

This commit is contained in:
Loz Calver 2018-03-20 16:49:36 +00:00
parent fcf0796dbd
commit ebd3fb6526
4 changed files with 37 additions and 4 deletions

View File

@ -3421,7 +3421,15 @@ class DataObject extends ViewableData implements DataObjectInterface, i18nEntity
continue; 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; $indexes[$column] = true;
} }
} catch (InvalidArgumentException $e) { } } catch (InvalidArgumentException $e) { }

View File

@ -60,6 +60,14 @@ abstract class DBField extends ViewableData {
*/ */
private static $default_search_filter_class = 'PartialMatchFilter'; 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. * @var $default mixed Default-value in the database.
* Might be overridden on DataObject-level, but still useful for setting defaults on * 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', 'LimitWordCountXML' => 'HTMLText',
); );
private static $auto_indexable = false;
/** /**
* (non-PHPdoc) * (non-PHPdoc)
* @see DBField::requireField() * @see DBField::requireField()

View File

@ -216,6 +216,11 @@ class DataObjectSchemaGenerationTest extends SapphireTest {
$indexes = $object->databaseIndexes(); $indexes = $object->databaseIndexes();
$this->assertArrayHasKey('Sort', $indexes); $this->assertArrayHasKey('Sort', $indexes);
$this->assertEquals('unique', $indexes['Sort']); $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);
} }
} }
@ -266,3 +271,13 @@ class DataObjectSchemaGenerationTest_Sorted extends DataObject implements TestOn
private static $default_sort = "Sort"; 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";
}