diff --git a/core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorageSchema.php b/core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorageSchema.php index 2196cfe..033d898 100644 --- a/core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorageSchema.php +++ b/core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorageSchema.php @@ -592,8 +592,6 @@ protected function getEntitySchemaTables() { */ protected function getEntitySchemaData(ContentEntityTypeInterface $entity_type, array $schema) { $entity_type_id = $entity_type->id(); - $keys = array('indexes', 'unique keys'); - $unused_keys = array_flip(array('description', 'fields', 'foreign keys')); // Collect all possible field schema identifiers for shared table fields. // These will be used to detect entity schema data in the subsequent loop. @@ -612,7 +610,10 @@ protected function getEntitySchemaData(ContentEntityTypeInterface $entity_type, } } + // Extract entity schema data from the Schema API definition. $schema_data = []; + $keys = ['indexes', 'unique keys']; + $unused_keys = array_flip(['description', 'fields', 'foreign keys']); foreach ($schema as $table_name => $table_schema) { $table_schema = array_diff_key($table_schema, $unused_keys); foreach ($keys as $key) { diff --git a/core/modules/system/system.install b/core/modules/system/system.install index 69044a1..34ef59d 100644 --- a/core/modules/system/system.install +++ b/core/modules/system/system.install @@ -1847,3 +1847,60 @@ function system_update_8013() { /** * @} End of "addtogroup updates-8.0.0-beta". */ + +/** + * @addtogroup updates-8.0.0-rc + * @{ + */ + +/** + * Make sure SQL entity schema data has no incorrect value. + */ +function system_update_8014() { + $key_value = \Drupal::keyValue('entity.storage_schema.sql'); + $schema_data = $key_value->getAll(); + ksort($schema_data); + + // Collect entity and field schema data, they will be used in the subsequent + // loop. + $all_entity_schema_data = []; + $all_field_schema_data = []; + foreach ($schema_data as $key => $value) { + list($entity_type_id, $sub_key) = explode('.', $key, 2); + if ($sub_key == 'entity_schema_data') { + $all_entity_schema_data[$entity_type_id] = $value; + } + else { + list($prefix, $field_name) = explode('.', $sub_key, 2); + if ($prefix == 'field_schema_data') { + $all_field_schema_data[$entity_type_id][$field_name] = $value; + } + else { + // This is not supposed to happen. + \Drupal::logger('update')->error(t('Unexpected SQL storage schema key: @key', ['@key' => $sub_key])); + } + } + } + + // Make sure no field schema data item is also stored in the entity schema + // data. + $schema_keys = ['indexes', 'unique keys']; + foreach ($all_entity_schema_data as $entity_type_id => $entity_schema_data) { + foreach ($all_field_schema_data[$entity_type_id] as $field_name => $field_schema_data) { + foreach ($field_schema_data as $table_name => $table_field_schema_data) { + foreach ($schema_keys as $schema_key) { + if (!empty($entity_schema_data[$table_name][$schema_key]) && !empty($table_field_schema_data[$schema_key])) { + $entity_schema_data[$table_name][$schema_key] = array_diff_key($entity_schema_data[$table_name][$schema_key], $table_field_schema_data[$schema_key]); + } + } + } + $key_value->set($entity_type_id . '.entity_schema_data', $entity_schema_data); + } + } +} + +/** + * @} End of "addtogroup updates-8.0.0-rc". + */ + +