From 60a17522f4bf1323af54c32b115289a475afcaa6 Mon Sep 17 00:00:00 2001 From: Nathaniel Catchpole Date: Thu, 23 Jun 2016 23:24:36 +0100 Subject: [PATCH] Issue #2742103 by ekes, dawehner, amateescu: SqlContentEntityStorageSchema::getDedicatedTableSchema() ignores 'unique keys' from the schema definition --- .../Sql/SqlContentEntityStorageSchema.php | 18 ++++++++++ .../Sql/SqlContentEntityStorageSchemaTest.php | 36 +++++++++++++++++-- 2 files changed, 52 insertions(+), 2 deletions(-) diff --git a/core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorageSchema.php b/core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorageSchema.php index d76c1329e16..ec3ebaf002c 100644 --- a/core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorageSchema.php +++ b/core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorageSchema.php @@ -1837,6 +1837,24 @@ class SqlContentEntityStorageSchema implements DynamicallyFieldableEntityStorage } } + // Add unique keys. + foreach ($schema['unique keys'] as $index_name => $columns) { + $real_name = $this->getFieldIndexName($storage_definition, $index_name); + foreach ($columns as $column_name) { + // Unique keys can be specified as either a column name or an array with + // column name and length. Allow for either case. + if (is_array($column_name)) { + $data_schema['unique keys'][$real_name][] = array( + $table_mapping->getFieldColumnName($storage_definition, $column_name[0]), + $column_name[1], + ); + } + else { + $data_schema['unique keys'][$real_name][] = $table_mapping->getFieldColumnName($storage_definition, $column_name); + } + } + } + // Add foreign keys. foreach ($schema['foreign keys'] as $specifier => $specification) { $real_name = $this->getFieldIndexName($storage_definition, $specifier); diff --git a/core/tests/Drupal/Tests/Core/Entity/Sql/SqlContentEntityStorageSchemaTest.php b/core/tests/Drupal/Tests/Core/Entity/Sql/SqlContentEntityStorageSchemaTest.php index e38988e26b5..1ce87c54021 100644 --- a/core/tests/Drupal/Tests/Core/Entity/Sql/SqlContentEntityStorageSchemaTest.php +++ b/core/tests/Drupal/Tests/Core/Entity/Sql/SqlContentEntityStorageSchemaTest.php @@ -820,6 +820,16 @@ class SqlContentEntityStorageSchemaTest extends UnitTestCase { 'length' => 32, 'not null' => FALSE, ), + 'area' => array( + 'type' => 'int', + 'unsigned' => TRUE, + 'not null' => TRUE, + ), + 'depth' => array( + 'type' => 'int', + 'unsigned' => TRUE, + 'not null' => TRUE, + ), ), 'foreign keys' => array( 'color' => array( @@ -829,8 +839,14 @@ class SqlContentEntityStorageSchemaTest extends UnitTestCase { ), ), ), - 'unique keys' => array(), - 'indexes' => array(), + 'unique keys' => array( + 'area' => array('area'), + 'shape' => array(array('shape', 10)), + ), + 'indexes' => array( + 'depth' => array('depth'), + 'color' => array(array('color', 3)), + ), )); $field_storage = $this->storageDefinitions[$field_name]; @@ -905,11 +921,27 @@ class SqlContentEntityStorageSchemaTest extends UnitTestCase { 'length' => 32, 'not null' => FALSE, ), + $field_name . '_area' => array( + 'type' => 'int', + 'unsigned' => TRUE, + 'not null' => TRUE, + ), + $field_name . '_depth' => array( + 'type' => 'int', + 'unsigned' => TRUE, + 'not null' => TRUE, + ), ), 'primary key' => array('entity_id', 'deleted', 'delta', 'langcode'), 'indexes' => array( 'bundle' => array('bundle'), 'revision_id' => array('revision_id'), + $field_name . '_depth' => array($field_name . '_depth'), + $field_name . '_color' => array(array($field_name . '_color', 3)), + ), + 'unique keys' => array( + $field_name . '_area' => array($field_name . '_area'), + $field_name . '_shape' => array(array($field_name . '_shape', 10)), ), 'foreign keys' => array( $field_name . '_color' => array(