public function onEntityTypeDelete(EntityTypeInterface $entity_type) {
$this->checkEntityType($entity_type);
$schema_handler = $this->database->schema();
- $actual_definition = $this->entityManager->getDefinition($entity_type->id());
- // @todo Instead of switching the wrapped entity type, we should be able to
- // instantiate a new table mapping for each entity type definition. See
- // https://www.drupal.org/node/2274017.
- $this->storage->setEntityType($entity_type);
-
- // Delete entity tables.
- foreach ($this->getEntitySchemaTables() as $table_name) {
+
+ $field_storage_definitions = $this->entityManager->getLastInstalledFieldStorageDefinitions($entity_type->id());
+ $table_mapping = $this->storage->getCustomTableMapping($entity_type, $field_storage_definitions);
+
+ // Delete entity and field tables.
+ foreach ($table_mapping->getTableNames() as $table_name) {
if ($schema_handler->tableExists($table_name)) {
$schema_handler->dropTable($table_name);
}
}
- // Delete dedicated field tables.
- $field_storage_definitions = $this->entityManager->getLastInstalledFieldStorageDefinitions($entity_type->id());
- $this->originalDefinitions = $field_storage_definitions;
- $table_mapping = $this->storage->getTableMapping($field_storage_definitions);
+ // Delete the field schema data.
foreach ($field_storage_definitions as $field_storage_definition) {
- // If we have a field having dedicated storage we need to drop it,
- // otherwise we just remove the related schema data.
- if ($table_mapping->requiresDedicatedTableStorage($field_storage_definition)) {
- $this->deleteDedicatedTableSchema($field_storage_definition);
- }
- elseif ($table_mapping->allowsSharedTableStorage($field_storage_definition)) {
- $this->deleteFieldSchemaData($field_storage_definition);
- }
+ $this->deleteFieldSchemaData($field_storage_definition);
}
- $this->originalDefinitions = NULL;
-
- $this->storage->setEntityType($actual_definition);
// Delete the entity schema.
$this->deleteEntitySchemaData($entity_type);
$entity_type_id = $entity_type->id();
if (!isset($this->schema[$entity_type_id]) || $reset) {
- // Back up the storage definition and replace it with the passed one.
- // @todo Instead of switching the wrapped entity type, we should be able
- // to instantiate a new table mapping for each entity type definition.
- // See https://www.drupal.org/node/2274017.
- $actual_definition = $this->entityManager->getDefinition($entity_type_id);
- $this->storage->setEntityType($entity_type);
-
// Prepare basic information about the entity type.
$tables = $this->getEntitySchemaTables();
}
// We need to act only on shared entity schema tables.
- $table_mapping = $this->storage->getTableMapping();
+ $table_mapping = $this->storage->getCustomTableMapping($entity_type, $this->fieldStorageDefinitions);
$table_names = array_diff($table_mapping->getTableNames(), $table_mapping->getDedicatedTableNames());
foreach ($table_names as $table_name) {
if (!isset($schema[$table_name])) {
}
$this->schema[$entity_type_id] = $schema;
-
- // Restore the actual definition.
- $this->storage->setEntityType($actual_definition);
}
return $this->schema[$entity_type_id];
$entity_type_id . '__revision' => [
'table' => $this->storage->getRevisionTable(),
'columns' => [$revision_key => $revision_key],
- ]
+ ],
],
];
* The entity type.
* @param array $schema
* The table schema, passed by reference.
- *
- * @return array
- * A partial schema array for the base table.
*/
protected function processBaseTable(ContentEntityTypeInterface $entity_type, array &$schema) {
- $this->processIdentifierSchema($schema, $entity_type->getKey('id'));
+ // Process the schema for the 'id' entity key only if it exists.
+ if ($entity_type->hasKey('id')) {
+ $this->processIdentifierSchema($schema, $entity_type->getKey('id'));
+ }
}
/**
* The entity type.
* @param array $schema
* The table schema, passed by reference.
- *
- * @return array
- * A partial schema array for the base table.
*/
protected function processRevisionTable(ContentEntityTypeInterface $entity_type, array &$schema) {
- $this->processIdentifierSchema($schema, $entity_type->getKey('revision'));
+ // Process the schema for the 'revision' entity key only if it exists.
+ if ($entity_type->hasKey('revision')) {
+ $this->processIdentifierSchema($schema, $entity_type->getKey('revision'));
+ }
}
/**
// Create field columns.
$schema[$table_name] = $this->getSharedTableFieldSchema($storage_definition, $table_name, $column_names);
if (!$only_save) {
+ // The entity schema needs to be checked because the field schema is
+ // potentially incomplete.
+ // @todo Fix this in https://www.drupal.org/node/2929120.
+ $entity_schema = $this->getEntitySchema($this->entityType);
foreach ($schema[$table_name]['fields'] as $name => $specifier) {
+ // Check if the field is part of the primary keys and pass along
+ // this information when adding the field.
+ // @see \Drupal\Core\Database\Schema::addField()
+ $new_keys = [];
+ if (isset($entity_schema[$table_name]['primary key']) && array_intersect($column_names, $entity_schema[$table_name]['primary key'])) {
+ $new_keys = ['primary key' => $entity_schema[$table_name]['primary key']];
+ }
+
// Check if the field exists because it might already have been
// created as part of the earlier entity type update event.
if (!$schema_handler->fieldExists($table_name, $name)) {
- $schema_handler->addField($table_name, $name, $specifier);
+ $schema_handler->addField($table_name, $name, $specifier, $new_keys);
}
}
if (!empty($schema[$table_name]['indexes'])) {
}
$column_schema = $original_schema[$table_name]['fields'][$column_name];
$column_schema['not null'] = $not_null;
- $schema_handler->changeField($table_name, $field_name, $field_name, $column_schema);
+ $schema_handler->changeField($table_name, $column_name, $column_name, $column_schema);
}
}
if ($initial_value && isset($initial_value[$field_column_name])) {
$schema['fields'][$schema_field_name]['initial'] = drupal_schema_get_field_value($column_schema, $initial_value[$field_column_name]);
}
- elseif (!empty($initial_value_from_field)) {
+ if (!empty($initial_value_from_field)) {
$schema['fields'][$schema_field_name]['initial_from_field'] = $initial_value_from_field[$field_column_name];
}
}
'size' => 'tiny',
'not null' => TRUE,
'default' => 0,
- 'description' => 'A boolean indicating whether this data item has been deleted'
+ 'description' => 'A boolean indicating whether this data item has been deleted',
],
'entity_id' => $id_schema,
'revision_id' => $revision_id_schema,