View source
<?php
namespace Drupal\Core\Entity\Sql;
use Drupal\Core\Database\Connection;
use Drupal\Core\DependencyInjection\DependencySerializationTrait;
use Drupal\Core\DependencyInjection\DeprecatedServicePropertyTrait;
use Drupal\Core\Entity\ContentEntityTypeInterface;
use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Entity\EntityPublishedInterface;
use Drupal\Core\Entity\EntityStorageException;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Entity\Exception\FieldStorageDefinitionUpdateForbiddenException;
use Drupal\Core\Entity\Schema\DynamicallyFieldableEntityStorageSchemaInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Field\FieldException;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\Site\Settings;
class SqlContentEntityStorageSchema implements DynamicallyFieldableEntityStorageSchemaInterface {
use DependencySerializationTrait;
use DeprecatedServicePropertyTrait;
use SqlFieldableEntityTypeListenerTrait {
onFieldableEntityTypeUpdate as traitOnFieldableEntityTypeUpdate;
}
protected $deprecatedProperties = [
'entityManager' => 'entity.manager',
];
protected $entityTypeManager;
protected $entityFieldManager;
protected $entityType;
protected $fieldStorageDefinitions;
protected $storage;
protected $schema;
protected $database;
protected $installedStorageSchema;
protected $updateBackupRepository;
protected $deletedFieldsRepository;
public function __construct(EntityTypeManagerInterface $entity_type_manager, ContentEntityTypeInterface $entity_type, SqlContentEntityStorage $storage, Connection $database, EntityFieldManagerInterface $entity_field_manager = NULL) {
$this->entityTypeManager = $entity_type_manager;
$this->storage = clone $storage;
$this->database = $database;
if (!$entity_field_manager) {
@trigger_error('Calling SqlContentEntityStorageSchema::__construct() with the $entity_field_manager argument is supported in drupal:8.7.0 and will be required before drupal:9.0.0. See https://www.drupal.org/node/2549139.', E_USER_DEPRECATED);
$entity_field_manager = \Drupal::service('entity_field.manager');
}
$this->entityFieldManager = $entity_field_manager;
$this->entityType = $entity_type_manager
->getActiveDefinition($entity_type
->id());
$this->fieldStorageDefinitions = $entity_field_manager
->getActiveFieldStorageDefinitions($entity_type
->id());
}
protected function installedStorageSchema() {
if (!isset($this->installedStorageSchema)) {
$this->installedStorageSchema = \Drupal::keyValue('entity.storage_schema.sql');
}
return $this->installedStorageSchema;
}
protected function deletedFieldsRepository() {
if (!isset($this->deletedFieldsRepository)) {
$this->deletedFieldsRepository = \Drupal::service('entity_field.deleted_fields_repository');
}
return $this->deletedFieldsRepository;
}
protected function updateBackupRepository() {
if (!isset($this->updateBackupRepository)) {
$this->updateBackupRepository = \Drupal::keyValue('entity.update_backup');
}
return $this->updateBackupRepository;
}
protected function getTableMapping(EntityTypeInterface $entity_type, array $storage_definitions = NULL) {
if ($storage_definitions && count($storage_definitions) === 1) {
$storage_definition = reset($storage_definitions);
$field_storage_definitions = [
$storage_definition
->getName() => $storage_definition,
] + $this->fieldStorageDefinitions;
}
else {
$field_storage_definitions = $storage_definitions ?: $this->fieldStorageDefinitions;
}
return $this->storage
->getCustomTableMapping($entity_type, $field_storage_definitions);
}
public function requiresEntityStorageSchemaChanges(EntityTypeInterface $entity_type, EntityTypeInterface $original) {
return $this
->hasSharedTableStructureChange($entity_type, $original) || $this
->getEntitySchemaData($entity_type, $this
->getEntitySchema($entity_type, TRUE)) != $this
->loadEntitySchemaData($original);
}
protected function hasSharedTableStructureChange(EntityTypeInterface $entity_type, EntityTypeInterface $original) {
return $entity_type
->isRevisionable() != $original
->isRevisionable() || $entity_type
->isTranslatable() != $original
->isTranslatable() || $this
->hasSharedTableNameChanges($entity_type, $original);
}
protected function hasSharedTableNameChanges(EntityTypeInterface $entity_type, EntityTypeInterface $original) {
$base_table = $this->database
->schema()
->tableExists($entity_type
->getBaseTable());
$data_table = $this->database
->schema()
->tableExists($entity_type
->getDataTable());
$revision_table = $this->database
->schema()
->tableExists($entity_type
->getRevisionTable());
$revision_data_table = $this->database
->schema()
->tableExists($entity_type
->getRevisionDataTable());
return !$base_table && $entity_type
->getBaseTable() != $original
->getBaseTable() || !$data_table && $entity_type
->getDataTable() != $original
->getDataTable() || !$revision_table && $entity_type
->getRevisionTable() != $original
->getRevisionTable() || !$revision_data_table && $entity_type
->getRevisionDataTable() != $original
->getRevisionDataTable();
}
public function requiresFieldStorageSchemaChanges(FieldStorageDefinitionInterface $storage_definition, FieldStorageDefinitionInterface $original) {
$table_mapping = $this
->getTableMapping($this->entityType);
if ($storage_definition
->hasCustomStorage() != $original
->hasCustomStorage() || $storage_definition
->getSchema() != $original
->getSchema() || $storage_definition
->isRevisionable() != $original
->isRevisionable() || $table_mapping
->allowsSharedTableStorage($storage_definition) != $table_mapping
->allowsSharedTableStorage($original) || $table_mapping
->requiresDedicatedTableStorage($storage_definition) != $table_mapping
->requiresDedicatedTableStorage($original)) {
return TRUE;
}
if ($storage_definition
->hasCustomStorage()) {
return FALSE;
}
$current_schema = $this
->getSchemaFromStorageDefinition($storage_definition);
$this
->processFieldStorageSchema($current_schema);
$installed_schema = $this
->loadFieldSchemaData($original);
$this
->processFieldStorageSchema($installed_schema);
return $current_schema != $installed_schema;
}
protected function getSchemaFromStorageDefinition(FieldStorageDefinitionInterface $storage_definition) {
assert(!$storage_definition
->hasCustomStorage());
$table_mapping = $this
->getTableMapping($this->entityType, [
$storage_definition,
]);
$schema = [];
if ($table_mapping
->requiresDedicatedTableStorage($storage_definition)) {
$schema = $this
->getDedicatedTableSchema($storage_definition);
}
elseif ($table_mapping
->allowsSharedTableStorage($storage_definition)) {
$field_name = $storage_definition
->getName();
foreach (array_diff($table_mapping
->getTableNames(), $table_mapping
->getDedicatedTableNames()) as $table_name) {
if (in_array($field_name, $table_mapping
->getFieldNames($table_name))) {
$column_names = $table_mapping
->getColumnNames($storage_definition
->getName());
$schema[$table_name] = $this
->getSharedTableFieldSchema($storage_definition, $table_name, $column_names);
}
}
}
return $schema;
}
public function requiresEntityDataMigration(EntityTypeInterface $entity_type, EntityTypeInterface $original) {
$original_storage_class = $original
->getStorageClass();
if (!class_exists($original_storage_class)) {
return TRUE;
}
if (!$this
->hasSharedTableStructureChange($entity_type, $original)) {
return FALSE;
}
return $this->storage
->hasData();
}
public function requiresFieldDataMigration(FieldStorageDefinitionInterface $storage_definition, FieldStorageDefinitionInterface $original) {
return !$this->storage
->countFieldData($original, TRUE);
}
public function onEntityTypeCreate(EntityTypeInterface $entity_type) {
$this
->onFieldableEntityTypeCreate($entity_type, $this->entityFieldManager
->getFieldStorageDefinitions($entity_type
->id()));
}
public function onEntityTypeUpdate(EntityTypeInterface $entity_type, EntityTypeInterface $original) {
$this
->checkEntityType($entity_type);
$this
->checkEntityType($original);
if (!$this
->requiresEntityStorageSchemaChanges($entity_type, $original)) {
return;
}
if (!class_exists($original
->getStorageClass()) || $this
->hasSharedTableStructureChange($entity_type, $original)) {
throw new EntityStorageException('It is not possible to change the entity type schema outside of a batch context. Use EntityDefinitionUpdateManagerInterface::updateFieldableEntityType() instead.');
}
$this
->deleteEntitySchemaIndexes($this
->loadEntitySchemaData($entity_type));
$entity_schema = $this
->getEntitySchema($entity_type, TRUE);
$this
->createEntitySchemaIndexes($entity_schema);
$this
->saveEntitySchemaData($entity_type, $entity_schema);
}
public function onEntityTypeDelete(EntityTypeInterface $entity_type) {
$this
->checkEntityType($entity_type);
$schema_handler = $this->database
->schema();
$table_names = $this
->getTableNames($entity_type, $this->fieldStorageDefinitions, $this
->getTableMapping($entity_type));
foreach ($table_names as $table_name) {
if ($schema_handler
->tableExists($table_name)) {
$schema_handler
->dropTable($table_name);
}
}
foreach ($this->fieldStorageDefinitions as $field_storage_definition) {
$this
->deleteFieldSchemaData($field_storage_definition);
}
$this
->deleteEntitySchemaData($entity_type);
}
public function onFieldableEntityTypeCreate(EntityTypeInterface $entity_type, array $field_storage_definitions) {
$this->entityType = $entity_type;
$this->fieldStorageDefinitions = $field_storage_definitions;
$this
->checkEntityType($entity_type);
$schema_handler = $this->database
->schema();
$schema = $this
->getEntitySchema($entity_type, TRUE);
foreach ($schema as $table_name => $table_schema) {
if (!$schema_handler
->tableExists($table_name)) {
$schema_handler
->createTable($table_name, $table_schema);
}
}
$table_mapping = $this
->getTableMapping($this->entityType);
foreach ($this->fieldStorageDefinitions as $field_storage_definition) {
if ($table_mapping
->requiresDedicatedTableStorage($field_storage_definition)) {
$this
->createDedicatedTableSchema($field_storage_definition);
}
elseif ($table_mapping
->allowsSharedTableStorage($field_storage_definition)) {
$this
->createSharedTableSchema($field_storage_definition, TRUE);
}
}
$this
->saveEntitySchemaData($entity_type, $schema);
}
public function onFieldableEntityTypeUpdate(EntityTypeInterface $entity_type, EntityTypeInterface $original, array $field_storage_definitions, array $original_field_storage_definitions, array &$sandbox = NULL) {
$this
->traitOnFieldableEntityTypeUpdate($entity_type, $original, $field_storage_definitions, $original_field_storage_definitions, $sandbox);
}
protected function preUpdateEntityTypeSchema(EntityTypeInterface $entity_type, EntityTypeInterface $original, array $field_storage_definitions, array $original_field_storage_definitions, array &$sandbox = NULL) {
$temporary_prefix = static::getTemporaryTableMappingPrefix($entity_type, $field_storage_definitions);
$sandbox['temporary_table_mapping'] = $this->storage
->getCustomTableMapping($entity_type, $field_storage_definitions, $temporary_prefix);
$sandbox['new_table_mapping'] = $this->storage
->getCustomTableMapping($entity_type, $field_storage_definitions);
$sandbox['original_table_mapping'] = $this->storage
->getCustomTableMapping($original, $original_field_storage_definitions);
$backup_prefix = static::getTemporaryTableMappingPrefix($original, $original_field_storage_definitions, 'old_');
$sandbox['backup_table_mapping'] = $this->storage
->getCustomTableMapping($original, $original_field_storage_definitions, $backup_prefix);
$sandbox['backup_prefix_key'] = substr($backup_prefix, 4);
$sandbox['backup_request_time'] = \Drupal::time()
->getRequestTime();
$temporary_table_names = array_combine($this
->getTableNames($entity_type, $field_storage_definitions, $sandbox['new_table_mapping']), $this
->getTableNames($entity_type, $field_storage_definitions, $sandbox['temporary_table_mapping']));
$this->entityType = $entity_type;
$this->fieldStorageDefinitions = $field_storage_definitions;
$this->storage
->setEntityType($entity_type);
$this->storage
->setFieldStorageDefinitions($field_storage_definitions);
$this->storage
->setTableMapping($sandbox['new_table_mapping']);
$schema = $this
->getEntitySchema($entity_type, TRUE);
$sandbox['new_entity_schema'] = $schema;
$schema = array_intersect_key($schema, $temporary_table_names);
foreach ($schema as $table_name => $table_schema) {
$this->database
->schema()
->createTable($temporary_table_names[$table_name], $table_schema);
}
foreach ($field_storage_definitions as $field_storage_definition) {
if ($sandbox['temporary_table_mapping']
->requiresDedicatedTableStorage($field_storage_definition)) {
$schema = $this
->getDedicatedTableSchema($field_storage_definition, $entity_type);
$schema = array_intersect_key($schema, $temporary_table_names);
foreach ($schema as $table_name => $table_schema) {
$this->database
->schema()
->createTable($temporary_table_names[$table_name], $table_schema);
}
}
}
$this->storage
->setEntityType($original);
$this->storage
->setFieldStorageDefinitions($original_field_storage_definitions);
$this->storage
->setTableMapping($sandbox['original_table_mapping']);
$sandbox['temporary_table_names'] = $temporary_table_names;
}
protected function postUpdateEntityTypeSchema(EntityTypeInterface $entity_type, EntityTypeInterface $original, array $field_storage_definitions, array $original_field_storage_definitions, array &$sandbox = NULL) {
$original_table_mapping = $sandbox['original_table_mapping'];
$new_table_mapping = $sandbox['new_table_mapping'];
$backup_table_mapping = $sandbox['backup_table_mapping'];
$backup_table_names = array_combine($this
->getTableNames($original, $original_field_storage_definitions, $original_table_mapping), $this
->getTableNames($original, $original_field_storage_definitions, $backup_table_mapping));
$renamed_tables = [];
try {
foreach ($backup_table_names as $original_table_name => $backup_table_name) {
$this->database
->schema()
->renameTable($original_table_name, $backup_table_name);
$renamed_tables[$original_table_name] = $backup_table_name;
}
} catch (\Exception $e) {
foreach ($renamed_tables as $original_table_name => $backup_table_name) {
$this->database
->schema()
->renameTable($backup_table_name, $original_table_name);
}
throw $e;
}
try {
foreach ($sandbox['temporary_table_names'] as $current_table_name => $temp_table_name) {
$this->database
->schema()
->renameTable($temp_table_name, $current_table_name);
}
$new_entity_schema = $sandbox['new_entity_schema'];
$this->schema[$entity_type
->id()] = $new_entity_schema;
$this->entityType = $entity_type;
$this->fieldStorageDefinitions = $field_storage_definitions;
$this
->saveEntitySchemaData($entity_type, $new_entity_schema);
$this->storage
->setEntityType($entity_type);
$this->storage
->setFieldStorageDefinitions($field_storage_definitions);
$this->storage
->setTableMapping($new_table_mapping);
foreach ($field_storage_definitions as $field_storage_definition) {
if ($new_table_mapping
->requiresDedicatedTableStorage($field_storage_definition)) {
$this
->createDedicatedTableSchema($field_storage_definition, TRUE);
}
elseif ($new_table_mapping
->allowsSharedTableStorage($field_storage_definition)) {
$this
->createSharedTableSchema($field_storage_definition, TRUE);
}
}
} catch (\Exception $e) {
foreach ($backup_table_names as $original_table_name => $backup_table_name) {
if ($this->database
->schema()
->tableExists($original_table_name)) {
$this->database
->schema()
->dropTable($original_table_name);
}
$this->database
->schema()
->renameTable($backup_table_name, $original_table_name);
}
throw $e;
}
if (Settings::get('entity_update_backup', TRUE)) {
$backup_key = $sandbox['backup_prefix_key'];
$backup = [
'entity_type' => $original,
'field_storage_definitions' => $original_field_storage_definitions,
'table_mapping' => $backup_table_mapping,
'request_time' => $sandbox['backup_request_time'],
];
$this
->updateBackupRepository()
->set("{$original->id()}.{$backup_key}", $backup);
}
else {
foreach ($backup_table_names as $original_table_name => $backup_table_name) {
$this->database
->schema()
->dropTable($backup_table_name);
}
}
}
private function getTableNames(EntityTypeInterface $entity_type, array $field_storage_definitions, TableMappingInterface $table_mapping) {
$table_names = $table_mapping
->getTableNames();
if ($table_mapping instanceof DefaultTableMapping && $entity_type
->isRevisionable()) {
foreach ($field_storage_definitions as $storage_definition) {
if ($table_mapping
->requiresDedicatedTableStorage($storage_definition)) {
$dedicated_revision_table_name = $table_mapping
->getDedicatedRevisionTableName($storage_definition);
if (!$storage_definition
->isRevisionable() && !in_array($dedicated_revision_table_name, $table_names)) {
$table_names[] = $dedicated_revision_table_name;
}
}
}
}
return $table_names;
}
protected function handleEntityTypeSchemaUpdateExceptionOnDataCopy(EntityTypeInterface $entity_type, EntityTypeInterface $original, array &$sandbox) {
foreach ($sandbox['temporary_table_names'] as $table_name) {
$this->database
->schema()
->dropTable($table_name);
}
}
public static function getTemporaryTableMappingPrefix(EntityTypeInterface $entity_type, array $field_storage_definitions, $first_prefix_part = 'tmp_') {
$prefix_parts[] = spl_object_hash($entity_type);
foreach ($field_storage_definitions as $storage_definition) {
$prefix_parts[] = spl_object_hash($storage_definition);
}
$prefix_parts[] = \Drupal::time()
->getRequestTime();
$hash = hash('sha256', implode('', $prefix_parts));
return $first_prefix_part . substr($hash, 0, 6);
}
public function onFieldStorageDefinitionCreate(FieldStorageDefinitionInterface $storage_definition) {
$this->fieldStorageDefinitions[$storage_definition
->getName()] = $storage_definition;
$this
->performFieldSchemaOperation('create', $storage_definition);
}
public function onFieldStorageDefinitionUpdate(FieldStorageDefinitionInterface $storage_definition, FieldStorageDefinitionInterface $original) {
$this->fieldStorageDefinitions[$storage_definition
->getName()] = $storage_definition;
$this
->performFieldSchemaOperation('update', $storage_definition, $original);
}
public function onFieldStorageDefinitionDelete(FieldStorageDefinitionInterface $storage_definition) {
if (!$this->storage
->countFieldData($storage_definition, TRUE)) {
$this
->performFieldSchemaOperation('delete', $storage_definition);
return;
}
if ($storage_definition
->hasCustomStorage()) {
return;
}
$table_mapping = $this
->getTableMapping($this->entityType, [
$storage_definition,
]);
$field_table_name = $table_mapping
->getFieldTableName($storage_definition
->getName());
if ($table_mapping
->requiresDedicatedTableStorage($storage_definition)) {
$table = $table_mapping
->getDedicatedDataTableName($storage_definition);
$new_table = $table_mapping
->getDedicatedDataTableName($storage_definition, TRUE);
$this->database
->schema()
->renameTable($table, $new_table);
if ($this->entityType
->isRevisionable()) {
$revision_table = $table_mapping
->getDedicatedRevisionTableName($storage_definition);
$revision_new_table = $table_mapping
->getDedicatedRevisionTableName($storage_definition, TRUE);
$this->database
->schema()
->renameTable($revision_table, $revision_new_table);
}
}
else {
$shared_table_field_columns = $table_mapping
->getColumnNames($storage_definition
->getName());
$deleted_storage_definition = $this
->deletedFieldsRepository()
->getFieldStorageDefinitions()[$storage_definition
->getUniqueStorageIdentifier()];
$table_mapping = $this
->getTableMapping($this->entityType, [
$deleted_storage_definition,
]);
$dedicated_table_field_schema = $this
->getDedicatedTableSchema($deleted_storage_definition);
$dedicated_table_field_columns = $table_mapping
->getColumnNames($deleted_storage_definition
->getName());
$dedicated_table_name = $table_mapping
->getDedicatedDataTableName($deleted_storage_definition, TRUE);
$dedicated_table_name_mapping[$table_mapping
->getDedicatedDataTableName($deleted_storage_definition)] = $dedicated_table_name;
if ($this->entityType
->isRevisionable()) {
$dedicated_revision_table_name = $table_mapping
->getDedicatedRevisionTableName($deleted_storage_definition, TRUE);
$dedicated_table_name_mapping[$table_mapping
->getDedicatedRevisionTableName($deleted_storage_definition)] = $dedicated_revision_table_name;
}
foreach ($dedicated_table_field_schema as $name => $table) {
if (!$this->database
->schema()
->tableExists($dedicated_table_name_mapping[$name])) {
$this->database
->schema()
->createTable($dedicated_table_name_mapping[$name], $table);
}
else {
throw new EntityStorageException('The field ' . $storage_definition
->getName() . ' has already been deleted and it is in the process of being purged.');
}
}
if ($this->database
->supportsTransactionalDDL()) {
$transaction = $this->database
->startTransaction();
}
try {
$this->database
->insert($dedicated_table_name)
->from($this
->getSelectQueryForFieldStorageDeletion($field_table_name, $shared_table_field_columns, $dedicated_table_field_columns))
->execute();
if (isset($dedicated_revision_table_name)) {
if ($this->entityType
->isTranslatable()) {
$revision_table = $storage_definition
->isRevisionable() ? $this->storage
->getRevisionDataTable() : $this->storage
->getDataTable();
}
else {
$revision_table = $storage_definition
->isRevisionable() ? $this->storage
->getRevisionTable() : $this->storage
->getBaseTable();
}
$this->database
->insert($dedicated_revision_table_name)
->from($this
->getSelectQueryForFieldStorageDeletion($revision_table, $shared_table_field_columns, $dedicated_table_field_columns, $field_table_name))
->execute();
}
} catch (\Exception $e) {
if (isset($transaction)) {
$transaction
->rollBack();
}
else {
foreach ($dedicated_table_field_schema as $name => $table) {
$this->database
->schema()
->dropTable($dedicated_table_name_mapping[$name]);
}
}
throw $e;
}
$this
->deleteSharedTableSchema($storage_definition);
}
unset($this->fieldStorageDefinitions[$storage_definition
->getName()]);
}
public function finalizePurge(FieldStorageDefinitionInterface $storage_definition) {
$this
->performFieldSchemaOperation('delete', $storage_definition);
}
protected function getSelectQueryForFieldStorageDeletion($table_name, array $shared_table_field_columns, array $dedicated_table_field_columns, $base_table = NULL) {
$select = $this->database
->select($table_name, 'entity_table');
if ($bundle = $this->entityType
->getKey('bundle')) {
if ($base_table && $base_table !== $table_name) {
$join_condition = "entity_table.{$this->entityType->getKey('id')} = %alias.{$this->entityType->getKey('id')}";
if ($this->entityType
->isTranslatable()) {
$langcode = $this->entityType
->getKey('langcode');
$join_condition .= " AND entity_table.{$langcode} = %alias.{$langcode}";
}
$select
->join($base_table, 'base_table', $join_condition);
$select
->addField('base_table', $bundle, 'bundle');
}
else {
$select
->addField('entity_table', $bundle, 'bundle');
}
}
else {
$select
->addExpression(':bundle', 'bundle', [
':bundle' => $this->entityType
->id(),
]);
}
$select
->addExpression(':deleted', 'deleted', [
':deleted' => 1,
]);
$select
->addField('entity_table', $this->entityType
->getKey('id'), 'entity_id');
if ($this->entityType
->isRevisionable()) {
$select
->addField('entity_table', $this->entityType
->getKey('revision'), 'revision_id');
}
else {
$select
->addField('entity_table', $this->entityType
->getKey('id'), 'revision_id');
}
if ($langcode = $this->entityType
->getKey('langcode')) {
$select
->addField('entity_table', $langcode, 'langcode');
}
else {
$select
->addExpression(':langcode', 'langcode', [
':langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
]);
}
$select
->addExpression(':delta', 'delta', [
':delta' => 0,
]);
$or = $select
->orConditionGroup();
foreach ($shared_table_field_columns as $field_column_name => $schema_column_name) {
$select
->addField('entity_table', $schema_column_name, $dedicated_table_field_columns[$field_column_name]);
$or
->isNotNull('entity_table.' . $schema_column_name);
}
$select
->condition($or);
$select
->forUpdate(TRUE);
return $select;
}
protected function checkEntityType(EntityTypeInterface $entity_type) {
if ($entity_type
->id() != $this->entityType
->id()) {
throw new EntityStorageException("Unsupported entity type {$entity_type->id()}");
}
return TRUE;
}
protected function getEntitySchema(ContentEntityTypeInterface $entity_type, $reset = FALSE) {
$this
->checkEntityType($entity_type);
$entity_type_id = $entity_type
->id();
if (!isset($this->schema[$entity_type_id]) || $reset) {
$table_mapping = $this
->getTableMapping($entity_type, $this->fieldStorageDefinitions);
$tables = $this
->getEntitySchemaTables($table_mapping);
$schema[$tables['base_table']] = $this
->initializeBaseTable($entity_type);
if (isset($tables['revision_table'])) {
$schema[$tables['revision_table']] = $this
->initializeRevisionTable($entity_type);
}
if (isset($tables['data_table'])) {
$schema[$tables['data_table']] = $this
->initializeDataTable($entity_type);
}
if (isset($tables['revision_data_table'])) {
$schema[$tables['revision_data_table']] = $this
->initializeRevisionDataTable($entity_type);
}
$table_names = array_diff($table_mapping
->getTableNames(), $table_mapping
->getDedicatedTableNames());
foreach ($table_names as $table_name) {
if (!isset($schema[$table_name])) {
$schema[$table_name] = [];
}
foreach ($table_mapping
->getFieldNames($table_name) as $field_name) {
if (!isset($this->fieldStorageDefinitions[$field_name])) {
throw new FieldException("Field storage definition for '{$field_name}' could not be found.");
}
elseif ($table_mapping
->allowsSharedTableStorage($this->fieldStorageDefinitions[$field_name])) {
$column_names = $table_mapping
->getColumnNames($field_name);
$storage_definition = $this->fieldStorageDefinitions[$field_name];
$schema[$table_name] = array_merge_recursive($schema[$table_name], $this
->getSharedTableFieldSchema($storage_definition, $table_name, $column_names));
}
}
}
$this
->processBaseTable($entity_type, $schema[$tables['base_table']]);
if (isset($tables['revision_table'])) {
$this
->processRevisionTable($entity_type, $schema[$tables['revision_table']]);
}
if (isset($tables['data_table'])) {
$this
->processDataTable($entity_type, $schema[$tables['data_table']]);
}
if (isset($tables['revision_data_table'])) {
$this
->processRevisionDataTable($entity_type, $schema[$tables['revision_data_table']]);
}
if (is_subclass_of($entity_type
->getClass(), EntityPublishedInterface::class)) {
$published_key = $entity_type
->getKey('published');
if ($published_key && isset($this->fieldStorageDefinitions[$published_key]) && !$this->fieldStorageDefinitions[$published_key]
->hasCustomStorage()) {
$published_field_table = $table_mapping
->getFieldTableName($published_key);
$id_key = $entity_type
->getKey('id');
if ($bundle_key = $entity_type
->getKey('bundle')) {
$key = "{$published_key}_{$bundle_key}";
$columns = [
$published_key,
$bundle_key,
$id_key,
];
}
else {
$key = $published_key;
$columns = [
$published_key,
$id_key,
];
}
$schema[$published_field_table]['indexes'][$this
->getEntityIndexName($entity_type, $key)] = $columns;
}
}
$this->schema[$entity_type_id] = $schema;
}
return $this->schema[$entity_type_id];
}
protected function getEntitySchemaTables(TableMappingInterface $table_mapping) {
return array_filter([
'base_table' => $table_mapping
->getBaseTable(),
'revision_table' => $table_mapping
->getRevisionTable(),
'data_table' => $table_mapping
->getDataTable(),
'revision_data_table' => $table_mapping
->getRevisionDataTable(),
]);
}
protected function getEntitySchemaData(ContentEntityTypeInterface $entity_type, array $schema) {
$entity_type_id = $entity_type
->id();
$field_schema_identifiers = [];
$table_mapping = $this
->getTableMapping($entity_type);
foreach ($this->fieldStorageDefinitions as $field_name => $storage_definition) {
if ($table_mapping
->allowsSharedTableStorage($storage_definition)) {
$name = $this
->getFieldSchemaIdentifierName($entity_type_id, $field_name);
$field_schema_identifiers[$name] = $name;
foreach ($storage_definition
->getColumns() as $key => $columns) {
$name = $this
->getFieldSchemaIdentifierName($entity_type_id, $field_name, $key);
$field_schema_identifiers[$name] = $name;
}
}
}
$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) {
if ($field_schema_identifiers && !empty($table_schema[$key])) {
$table_schema[$key] = array_diff_key($table_schema[$key], $field_schema_identifiers);
}
}
$schema_data[$table_name] = array_filter($table_schema);
}
return $schema_data;
}
protected function getFieldIndexes($field_name, array $field_schema, array $column_mapping) {
return $this
->getFieldSchemaData($field_name, $field_schema, $column_mapping, 'indexes');
}
protected function getFieldUniqueKeys($field_name, array $field_schema, array $column_mapping) {
return $this
->getFieldSchemaData($field_name, $field_schema, $column_mapping, 'unique keys');
}
protected function getFieldSchemaData($field_name, array $field_schema, array $column_mapping, $schema_key) {
$data = [];
$entity_type_id = $this->entityType
->id();
foreach ($field_schema[$schema_key] as $key => $columns) {
$real_key = $this
->getFieldSchemaIdentifierName($entity_type_id, $field_name, $key);
foreach ($columns as $column) {
if (is_array($column)) {
list($column_name, $length) = $column;
$data[$real_key][] = [
$column_mapping[$column_name],
$length,
];
}
else {
$data[$real_key][] = $column_mapping[$column];
}
}
}
return $data;
}
protected function getFieldSchemaIdentifierName($entity_type_id, $field_name, $key = NULL) {
$real_key = isset($key) ? "{$entity_type_id}_field__{$field_name}__{$key}" : "{$entity_type_id}_field__{$field_name}";
if (strlen($real_key) > 48) {
$entity_type = substr($entity_type_id, 0, 36);
$field_hash = substr(hash('sha256', $real_key), 0, 10);
$real_key = $entity_type . '__' . $field_hash;
}
return $real_key;
}
protected function getFieldForeignKeys($field_name, array $field_schema, array $column_mapping) {
$foreign_keys = [];
foreach ($field_schema['foreign keys'] as $specifier => $specification) {
$entity_type_id = $this->entityType
->id();
$real_specifier = "{$entity_type_id}_field__{$field_name}__{$specifier}";
$foreign_keys[$real_specifier]['table'] = $specification['table'];
foreach ($specification['columns'] as $column => $referenced) {
$foreign_keys[$real_specifier]['columns'][$column_mapping[$column]] = $referenced;
}
}
return $foreign_keys;
}
protected function loadEntitySchemaData(EntityTypeInterface $entity_type) {
return $this
->installedStorageSchema()
->get($entity_type
->id() . '.entity_schema_data', []);
}
protected function saveEntitySchemaData(EntityTypeInterface $entity_type, $schema) {
$data = $this
->getEntitySchemaData($entity_type, $schema);
$this
->installedStorageSchema()
->set($entity_type
->id() . '.entity_schema_data', $data);
}
protected function deleteEntitySchemaData(EntityTypeInterface $entity_type) {
$this
->installedStorageSchema()
->delete($entity_type
->id() . '.entity_schema_data');
}
protected function loadFieldSchemaData(FieldStorageDefinitionInterface $storage_definition) {
return $this
->installedStorageSchema()
->get($storage_definition
->getTargetEntityTypeId() . '.field_schema_data.' . $storage_definition
->getName(), []);
}
protected function saveFieldSchemaData(FieldStorageDefinitionInterface $storage_definition, $schema) {
$this
->processFieldStorageSchema($schema);
$this
->installedStorageSchema()
->set($storage_definition
->getTargetEntityTypeId() . '.field_schema_data.' . $storage_definition
->getName(), $schema);
}
protected function deleteFieldSchemaData(FieldStorageDefinitionInterface $storage_definition) {
$this
->installedStorageSchema()
->delete($storage_definition
->getTargetEntityTypeId() . '.field_schema_data.' . $storage_definition
->getName());
}
protected function initializeBaseTable(ContentEntityTypeInterface $entity_type) {
$entity_type_id = $entity_type
->id();
$schema = [
'description' => "The base table for {$entity_type_id} entities.",
'primary key' => [
$entity_type
->getKey('id'),
],
'indexes' => [],
'foreign keys' => [],
];
if ($entity_type
->hasKey('revision')) {
$revision_key = $entity_type
->getKey('revision');
$key_name = $this
->getEntityIndexName($entity_type, $revision_key);
$schema['unique keys'][$key_name] = [
$revision_key,
];
$schema['foreign keys'][$entity_type_id . '__revision'] = [
'table' => $this->storage
->getRevisionTable(),
'columns' => [
$revision_key => $revision_key,
],
];
}
$this
->addTableDefaults($schema);
return $schema;
}
protected function initializeRevisionTable(ContentEntityTypeInterface $entity_type) {
$entity_type_id = $entity_type
->id();
$id_key = $entity_type
->getKey('id');
$revision_key = $entity_type
->getKey('revision');
$schema = [
'description' => "The revision table for {$entity_type_id} entities.",
'primary key' => [
$revision_key,
],
'indexes' => [],
'foreign keys' => [
$entity_type_id . '__revisioned' => [
'table' => $this->storage
->getBaseTable(),
'columns' => [
$id_key => $id_key,
],
],
],
];
$schema['indexes'][$this
->getEntityIndexName($entity_type, $id_key)] = [
$id_key,
];
$this
->addTableDefaults($schema);
return $schema;
}
protected function initializeDataTable(ContentEntityTypeInterface $entity_type) {
$entity_type_id = $entity_type
->id();
$id_key = $entity_type
->getKey('id');
$schema = [
'description' => "The data table for {$entity_type_id} entities.",
'primary key' => [
$id_key,
$entity_type
->getKey('langcode'),
],
'indexes' => [
$entity_type_id . '__id__default_langcode__langcode' => [
$id_key,
$entity_type
->getKey('default_langcode'),
$entity_type
->getKey('langcode'),
],
],
'foreign keys' => [
$entity_type_id => [
'table' => $this->storage
->getBaseTable(),
'columns' => [
$id_key => $id_key,
],
],
],
];
if ($entity_type
->hasKey('revision')) {
$key = $entity_type
->getKey('revision');
$schema['indexes'][$this
->getEntityIndexName($entity_type, $key)] = [
$key,
];
}
$this
->addTableDefaults($schema);
return $schema;
}
protected function initializeRevisionDataTable(ContentEntityTypeInterface $entity_type) {
$entity_type_id = $entity_type
->id();
$id_key = $entity_type
->getKey('id');
$revision_key = $entity_type
->getKey('revision');
$schema = [
'description' => "The revision data table for {$entity_type_id} entities.",
'primary key' => [
$revision_key,
$entity_type
->getKey('langcode'),
],
'indexes' => [
$entity_type_id . '__id__default_langcode__langcode' => [
$id_key,
$entity_type
->getKey('default_langcode'),
$entity_type
->getKey('langcode'),
],
],
'foreign keys' => [
$entity_type_id => [
'table' => $this->storage
->getBaseTable(),
'columns' => [
$id_key => $id_key,
],
],
$entity_type_id . '__revision' => [
'table' => $this->storage
->getRevisionTable(),
'columns' => [
$revision_key => $revision_key,
],
],
],
];
$this
->addTableDefaults($schema);
return $schema;
}
protected function addTableDefaults(&$schema) {
$schema += [
'fields' => [],
'unique keys' => [],
'indexes' => [],
'foreign keys' => [],
];
}
protected function processBaseTable(ContentEntityTypeInterface $entity_type, array &$schema) {
}
protected function processRevisionTable(ContentEntityTypeInterface $entity_type, array &$schema) {
}
protected function processDataTable(ContentEntityTypeInterface $entity_type, array &$schema) {
$schema['fields'][$entity_type
->getKey('default_langcode')]['not null'] = TRUE;
}
protected function processRevisionDataTable(ContentEntityTypeInterface $entity_type, array &$schema) {
$schema['fields'][$entity_type
->getKey('default_langcode')]['not null'] = TRUE;
}
protected function processIdentifierSchema(&$schema, $key) {
if ($schema['fields'][$key]['type'] == 'int') {
$schema['fields'][$key]['type'] = 'serial';
}
$schema['fields'][$key]['not null'] = TRUE;
unset($schema['fields'][$key]['default']);
}
protected function processFieldStorageSchema(array &$field_storage_schema) {
foreach ($field_storage_schema as $table_name => $table_schema) {
foreach ($table_schema['fields'] as $key => $schema) {
unset($field_storage_schema[$table_name]['fields'][$key]['initial']);
unset($field_storage_schema[$table_name]['fields'][$key]['initial_from_field']);
}
}
}
protected function performFieldSchemaOperation($operation, FieldStorageDefinitionInterface $storage_definition, FieldStorageDefinitionInterface $original = NULL) {
$table_mapping = $this
->getTableMapping($this->entityType, [
$storage_definition,
]);
if ($table_mapping
->requiresDedicatedTableStorage($storage_definition)) {
$this
->{$operation . 'DedicatedTableSchema'}($storage_definition, $original);
}
elseif ($table_mapping
->allowsSharedTableStorage($storage_definition)) {
$this
->{$operation . 'SharedTableSchema'}($storage_definition, $original);
}
}
protected function createDedicatedTableSchema(FieldStorageDefinitionInterface $storage_definition, $only_save = FALSE) {
$schema = $this
->getDedicatedTableSchema($storage_definition);
if (!$only_save) {
foreach ($schema as $name => $table) {
if (!$this->database
->schema()
->tableExists($name)) {
$this->database
->schema()
->createTable($name, $table);
}
}
}
$this
->saveFieldSchemaData($storage_definition, $schema);
}
protected function createSharedTableSchema(FieldStorageDefinitionInterface $storage_definition, $only_save = FALSE) {
$created_field_name = $storage_definition
->getName();
$table_mapping = $this
->getTableMapping($this->entityType, [
$storage_definition,
]);
$column_names = $table_mapping
->getColumnNames($created_field_name);
$schema_handler = $this->database
->schema();
$shared_table_names = array_diff($table_mapping
->getTableNames(), $table_mapping
->getDedicatedTableNames());
$schema = [];
foreach ($shared_table_names as $table_name) {
foreach ($table_mapping
->getFieldNames($table_name) as $field_name) {
if ($field_name == $created_field_name) {
$schema[$table_name] = $this
->getSharedTableFieldSchema($storage_definition, $table_name, $column_names);
if (!$only_save) {
$entity_schema = $this
->getEntitySchema($this->entityType);
foreach ($schema[$table_name]['fields'] as $name => $specifier) {
$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'],
];
}
if (!$schema_handler
->fieldExists($table_name, $name)) {
$schema_handler
->addField($table_name, $name, $specifier, $new_keys);
}
}
if (!empty($schema[$table_name]['indexes'])) {
foreach ($schema[$table_name]['indexes'] as $name => $specifier) {
$this
->addIndex($table_name, $name, $specifier, $schema[$table_name]);
}
}
if (!empty($schema[$table_name]['unique keys'])) {
foreach ($schema[$table_name]['unique keys'] as $name => $specifier) {
$schema_handler
->addUniqueKey($table_name, $name, $specifier);
}
}
}
break;
}
}
}
$this
->saveFieldSchemaData($storage_definition, $schema);
if (!$only_save) {
$entity_schema = $this
->getEntitySchema($this->entityType);
$this
->createEntitySchemaIndexes($entity_schema, $storage_definition);
$this
->saveEntitySchemaData($this->entityType, $entity_schema);
}
}
protected function deleteDedicatedTableSchema(FieldStorageDefinitionInterface $storage_definition) {
$table_mapping = $this
->getTableMapping($this->entityType, [
$storage_definition,
]);
$table_name = $table_mapping
->getDedicatedDataTableName($storage_definition, $storage_definition
->isDeleted());
if ($this->database
->schema()
->tableExists($table_name)) {
$this->database
->schema()
->dropTable($table_name);
}
if ($this->entityType
->isRevisionable()) {
$revision_table_name = $table_mapping
->getDedicatedRevisionTableName($storage_definition, $storage_definition
->isDeleted());
if ($this->database
->schema()
->tableExists($revision_table_name)) {
$this->database
->schema()
->dropTable($revision_table_name);
}
}
$this
->deleteFieldSchemaData($storage_definition);
}
protected function deleteSharedTableSchema(FieldStorageDefinitionInterface $storage_definition) {
$this
->deleteEntitySchemaIndexes($this
->loadEntitySchemaData($this->entityType), $storage_definition);
$deleted_field_name = $storage_definition
->getName();
$table_mapping = $this
->getTableMapping($this->entityType, [
$storage_definition,
]);
$column_names = $table_mapping
->getColumnNames($deleted_field_name);
$schema_handler = $this->database
->schema();
$shared_table_names = array_diff($table_mapping
->getTableNames(), $table_mapping
->getDedicatedTableNames());
foreach ($shared_table_names as $table_name) {
foreach ($table_mapping
->getFieldNames($table_name) as $field_name) {
if ($field_name == $deleted_field_name) {
$schema = $this
->getSharedTableFieldSchema($storage_definition, $table_name, $column_names);
if (!empty($schema['indexes'])) {
foreach ($schema['indexes'] as $name => $specifier) {
$schema_handler
->dropIndex($table_name, $name);
}
}
if (!empty($schema['unique keys'])) {
foreach ($schema['unique keys'] as $name => $specifier) {
$schema_handler
->dropUniqueKey($table_name, $name);
}
}
foreach ($column_names as $column_name) {
$schema_handler
->dropField($table_name, $column_name);
}
break;
}
}
}
$this
->deleteFieldSchemaData($storage_definition);
}
protected function updateDedicatedTableSchema(FieldStorageDefinitionInterface $storage_definition, FieldStorageDefinitionInterface $original) {
if (!$this->storage
->countFieldData($original, TRUE)) {
if ($this->database
->supportsTransactionalDDL()) {
$transaction = $this->database
->startTransaction();
}
try {
$this
->performFieldSchemaOperation('delete', $original);
$this
->performFieldSchemaOperation('create', $storage_definition);
} catch (\Exception $e) {
if ($this->database
->supportsTransactionalDDL()) {
$transaction
->rollBack();
}
else {
$this
->performFieldSchemaOperation('create', $original);
}
throw $e;
}
}
else {
if ($this
->hasColumnChanges($storage_definition, $original)) {
throw new FieldStorageDefinitionUpdateForbiddenException('The SQL storage cannot change the schema for an existing field (' . $storage_definition
->getName() . ' in ' . $storage_definition
->getTargetEntityTypeId() . ' entity) with data.');
}
$table_mapping = $this
->getTableMapping($this->entityType, [
$storage_definition,
]);
$table = $table_mapping
->getDedicatedDataTableName($original);
$revision_table = $table_mapping
->getDedicatedRevisionTableName($original);
$schema = $storage_definition
->getSchema();
$original_schema = $original
->getSchema();
$actual_schema = $this
->getDedicatedTableSchema($storage_definition);
foreach ($original_schema['indexes'] as $name => $columns) {
if (!isset($schema['indexes'][$name]) || $columns != $schema['indexes'][$name]) {
$real_name = $this
->getFieldIndexName($storage_definition, $name);
$this->database
->schema()
->dropIndex($table, $real_name);
$this->database
->schema()
->dropIndex($revision_table, $real_name);
}
}
$table = $table_mapping
->getDedicatedDataTableName($storage_definition);
$revision_table = $table_mapping
->getDedicatedRevisionTableName($storage_definition);
foreach ($schema['indexes'] as $name => $columns) {
if (!isset($original_schema['indexes'][$name]) || $columns != $original_schema['indexes'][$name]) {
$real_name = $this
->getFieldIndexName($storage_definition, $name);
$real_columns = [];
foreach ($columns as $column_name) {
if (is_array($column_name)) {
$real_columns[] = [
$table_mapping
->getFieldColumnName($storage_definition, $column_name[0]),
$column_name[1],
];
}
else {
$real_columns[] = $table_mapping
->getFieldColumnName($storage_definition, $column_name);
}
}
$this
->addIndex($table, $real_name, $real_columns, $actual_schema[$table]);
$this
->addIndex($revision_table, $real_name, $real_columns, $actual_schema[$revision_table]);
}
}
$this
->saveFieldSchemaData($storage_definition, $this
->getDedicatedTableSchema($storage_definition));
}
}
protected function updateSharedTableSchema(FieldStorageDefinitionInterface $storage_definition, FieldStorageDefinitionInterface $original) {
if (!$this->storage
->countFieldData($original, TRUE)) {
if ($this->database
->supportsTransactionalDDL()) {
$transaction = $this->database
->startTransaction();
}
try {
$this
->performFieldSchemaOperation('delete', $original);
$this
->performFieldSchemaOperation('create', $storage_definition);
} catch (\Exception $e) {
if ($this->database
->supportsTransactionalDDL()) {
$transaction
->rollBack();
}
else {
$this
->createSharedTableSchema($original);
}
throw $e;
}
}
else {
if ($this
->hasColumnChanges($storage_definition, $original)) {
throw new FieldStorageDefinitionUpdateForbiddenException('The SQL storage cannot change the schema for an existing field (' . $storage_definition
->getName() . ' in ' . $storage_definition
->getTargetEntityTypeId() . ' entity) with data.');
}
$updated_field_name = $storage_definition
->getName();
$table_mapping = $this
->getTableMapping($this->entityType, [
$storage_definition,
]);
$column_names = $table_mapping
->getColumnNames($updated_field_name);
$schema_handler = $this->database
->schema();
$original_schema = $this
->loadFieldSchemaData($original);
$schema = [];
foreach ($table_mapping
->getTableNames() as $table_name) {
foreach ($table_mapping
->getFieldNames($table_name) as $field_name) {
if ($field_name == $updated_field_name) {
$schema[$table_name] = $this
->getSharedTableFieldSchema($storage_definition, $table_name, $column_names);
foreach ($schema[$table_name]['fields'] as $column_name => $specifier) {
$not_null = !empty($specifier['not null']);
$original_not_null = !empty($original_schema[$table_name]['fields'][$column_name]['not null']);
if ($not_null !== $original_not_null) {
if ($not_null && $this
->hasNullFieldPropertyData($table_name, $column_name)) {
throw new EntityStorageException("The {$column_name} column cannot have NOT NULL constraints as it holds NULL values.");
}
$column_schema = $original_schema[$table_name]['fields'][$column_name];
$column_schema['not null'] = $not_null;
$schema_handler
->changeField($table_name, $column_name, $column_name, $column_schema);
}
}
if (!empty($original_schema[$table_name]['indexes'])) {
foreach ($original_schema[$table_name]['indexes'] as $name => $specifier) {
$schema_handler
->dropIndex($table_name, $name);
}
}
if (!empty($original_schema[$table_name]['unique keys'])) {
foreach ($original_schema[$table_name]['unique keys'] as $name => $specifier) {
$schema_handler
->dropUniqueKey($table_name, $name);
}
}
if (!empty($schema[$table_name]['indexes'])) {
foreach ($schema[$table_name]['indexes'] as $name => $specifier) {
$this
->addIndex($table_name, $name, $specifier, $schema[$table_name]);
}
}
if (!empty($schema[$table_name]['unique keys'])) {
foreach ($schema[$table_name]['unique keys'] as $name => $specifier) {
$schema_handler
->addUniqueKey($table_name, $name, $specifier);
}
}
break;
}
}
}
$this
->saveFieldSchemaData($storage_definition, $schema);
}
}
protected function createEntitySchemaIndexes(array $entity_schema, FieldStorageDefinitionInterface $storage_definition = NULL) {
$schema_handler = $this->database
->schema();
if ($storage_definition) {
$table_mapping = $this
->getTableMapping($this->entityType, [
$storage_definition,
]);
$column_names = $table_mapping
->getColumnNames($storage_definition
->getName());
}
$index_keys = [
'indexes' => 'addIndex',
'unique keys' => 'addUniqueKey',
];
foreach ($this
->getEntitySchemaData($this->entityType, $entity_schema) as $table_name => $schema) {
$schema['fields'] = $entity_schema[$table_name]['fields'];
foreach ($index_keys as $key => $add_method) {
if (!empty($schema[$key])) {
foreach ($schema[$key] as $name => $specifier) {
$create = FALSE;
$specifier_columns = array_map(function ($item) {
return is_string($item) ? $item : reset($item);
}, $specifier);
if (!isset($column_names) || array_intersect($specifier_columns, $column_names)) {
$create = TRUE;
foreach ($specifier_columns as $specifier_column_name) {
if (!$schema_handler
->fieldExists($table_name, $specifier_column_name)) {
$create = FALSE;
break;
}
}
}
if ($create) {
$this
->{$add_method}($table_name, $name, $specifier, $schema);
}
}
}
}
}
}
protected function deleteEntitySchemaIndexes(array $entity_schema_data, FieldStorageDefinitionInterface $storage_definition = NULL) {
$schema_handler = $this->database
->schema();
if ($storage_definition) {
$table_mapping = $this
->getTableMapping($this->entityType, [
$storage_definition,
]);
$column_names = $table_mapping
->getColumnNames($storage_definition
->getName());
}
$index_keys = [
'indexes' => 'dropIndex',
'unique keys' => 'dropUniqueKey',
];
foreach ($entity_schema_data as $table_name => $schema) {
foreach ($index_keys as $key => $drop_method) {
if (!empty($schema[$key])) {
foreach ($schema[$key] as $name => $specifier) {
$specifier_columns = array_map(function ($item) {
return is_string($item) ? $item : reset($item);
}, $specifier);
if (!isset($column_names) || array_intersect($specifier_columns, $column_names)) {
$schema_handler
->{$drop_method}($table_name, $name);
}
}
}
}
}
}
protected function hasNullFieldPropertyData($table_name, $column_name) {
$query = $this->database
->select($table_name, 't')
->fields('t', [
$column_name,
])
->range(0, 1);
$query
->isNull('t.' . $column_name);
$result = $query
->execute()
->fetchAssoc();
return (bool) $result;
}
protected function getSharedTableFieldSchema(FieldStorageDefinitionInterface $storage_definition, $table_name, array $column_mapping) {
$schema = [];
$table_mapping = $this
->getTableMapping($this->entityType, [
$storage_definition,
]);
$field_schema = $storage_definition
->getSchema();
if (array_intersect(array_keys($field_schema['columns']), $table_mapping
->getReservedColumns())) {
throw new FieldException("Illegal field column names on {$storage_definition->getName()}");
}
$field_name = $storage_definition
->getName();
$base_table = $this->storage
->getBaseTable();
$revision_table = $this->storage
->getRevisionTable();
$initial_value = $initial_value_from_field = [];
$storage_definition_is_new = empty($this
->loadFieldSchemaData($storage_definition));
if ($storage_definition_is_new && $storage_definition instanceof BaseFieldDefinition && $table_mapping
->allowsSharedTableStorage($storage_definition)) {
if (($initial_storage_value = $storage_definition
->getInitialValue()) && !empty($initial_storage_value)) {
$initial_value = reset($initial_storage_value);
}
if ($initial_value_field_name = $storage_definition
->getInitialValueFromField()) {
if (!isset($this->fieldStorageDefinitions[$initial_value_field_name])) {
throw new FieldException("Illegal initial value definition on {$storage_definition->getName()}: The field {$initial_value_field_name} does not exist.");
}
if ($storage_definition
->getType() !== $this->fieldStorageDefinitions[$initial_value_field_name]
->getType()) {
throw new FieldException("Illegal initial value definition on {$storage_definition->getName()}: The field types do not match.");
}
if (!$table_mapping
->allowsSharedTableStorage($this->fieldStorageDefinitions[$initial_value_field_name])) {
throw new FieldException("Illegal initial value definition on {$storage_definition->getName()}: Both fields have to be stored in the shared entity tables.");
}
$initial_value_from_field = $table_mapping
->getColumnNames($initial_value_field_name);
}
}
$not_null_keys = $this->entityType
->getKeys();
unset($not_null_keys['label'], $not_null_keys['revision_translation_affected']);
if ($table_name == $base_table) {
unset($not_null_keys['revision']);
}
foreach ($column_mapping as $field_column_name => $schema_field_name) {
$column_schema = $field_schema['columns'][$field_column_name];
$schema['fields'][$schema_field_name] = $column_schema;
$schema['fields'][$schema_field_name]['not null'] = in_array($field_name, $not_null_keys);
if ($initial_value && isset($initial_value[$field_column_name])) {
$schema['fields'][$schema_field_name]['initial'] = SqlContentEntityStorageSchema::castValue($column_schema, $initial_value[$field_column_name]);
}
if (!empty($initial_value_from_field)) {
$schema['fields'][$schema_field_name]['initial_from_field'] = $initial_value_from_field[$field_column_name];
}
}
if (!empty($field_schema['indexes'])) {
$schema['indexes'] = $this
->getFieldIndexes($field_name, $field_schema, $column_mapping);
}
if (!empty($field_schema['unique keys'])) {
$schema['unique keys'] = $this
->getFieldUniqueKeys($field_name, $field_schema, $column_mapping);
}
if (!empty($field_schema['foreign keys'])) {
$schema['foreign keys'] = $this
->getFieldForeignKeys($field_name, $field_schema, $column_mapping);
}
if ($table_name === $base_table && $field_name === $this->entityType
->getKey('id') || $table_name === $revision_table && $field_name === $this->entityType
->getKey('revision')) {
$this
->processIdentifierSchema($schema, $field_name);
}
return $schema;
}
protected function addSharedTableFieldIndex(FieldStorageDefinitionInterface $storage_definition, &$schema, $not_null = FALSE, $size = NULL) {
$name = $storage_definition
->getName();
$real_key = $this
->getFieldSchemaIdentifierName($storage_definition
->getTargetEntityTypeId(), $name);
$schema['indexes'][$real_key] = [
$size ? [
$name,
$size,
] : $name,
];
if ($not_null) {
$schema['fields'][$name]['not null'] = TRUE;
}
}
protected function addSharedTableFieldUniqueKey(FieldStorageDefinitionInterface $storage_definition, &$schema) {
$name = $storage_definition
->getName();
$real_key = $this
->getFieldSchemaIdentifierName($storage_definition
->getTargetEntityTypeId(), $name);
$schema['unique keys'][$real_key] = [
$name,
];
$schema['fields'][$name]['not null'] = TRUE;
}
protected function addSharedTableFieldForeignKey(FieldStorageDefinitionInterface $storage_definition, &$schema, $foreign_table, $foreign_column) {
$name = $storage_definition
->getName();
$real_key = $this
->getFieldSchemaIdentifierName($storage_definition
->getTargetEntityTypeId(), $name);
$schema['foreign keys'][$real_key] = [
'table' => $foreign_table,
'columns' => [
$name => $foreign_column,
],
];
}
protected function getDedicatedTableSchema(FieldStorageDefinitionInterface $storage_definition, ContentEntityTypeInterface $entity_type = NULL) {
$entity_type = $entity_type ?: $this->entityType;
$description_current = "Data storage for {$storage_definition->getTargetEntityTypeId()} field {$storage_definition->getName()}.";
$description_revision = "Revision archive storage for {$storage_definition->getTargetEntityTypeId()} field {$storage_definition->getName()}.";
$id_definition = $this->fieldStorageDefinitions[$entity_type
->getKey('id')];
if ($id_definition
->getType() == 'integer') {
$id_schema = [
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'description' => 'The entity id this data is attached to',
];
}
else {
$id_schema = [
'type' => 'varchar_ascii',
'length' => 128,
'not null' => TRUE,
'description' => 'The entity id this data is attached to',
];
}
if (!$entity_type
->isRevisionable()) {
$revision_id_schema = $id_schema;
$revision_id_schema['description'] = 'The entity revision id this data is attached to, which for an unversioned entity type is the same as the entity id';
}
elseif ($this->fieldStorageDefinitions[$entity_type
->getKey('revision')]
->getType() == 'integer') {
$revision_id_schema = [
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'description' => 'The entity revision id this data is attached to',
];
}
else {
$revision_id_schema = [
'type' => 'varchar',
'length' => 128,
'not null' => TRUE,
'description' => 'The entity revision id this data is attached to',
];
}
$data_schema = [
'description' => $description_current,
'fields' => [
'bundle' => [
'type' => 'varchar_ascii',
'length' => 128,
'not null' => TRUE,
'default' => '',
'description' => 'The field instance bundle to which this row belongs, used when deleting a field instance',
],
'deleted' => [
'type' => 'int',
'size' => 'tiny',
'not null' => TRUE,
'default' => 0,
'description' => 'A boolean indicating whether this data item has been deleted',
],
'entity_id' => $id_schema,
'revision_id' => $revision_id_schema,
'langcode' => [
'type' => 'varchar_ascii',
'length' => 32,
'not null' => TRUE,
'default' => '',
'description' => 'The language code for this data item.',
],
'delta' => [
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'description' => 'The sequence number for this data item, used for multi-value fields',
],
],
'primary key' => [
'entity_id',
'deleted',
'delta',
'langcode',
],
'indexes' => [
'bundle' => [
'bundle',
],
'revision_id' => [
'revision_id',
],
],
];
$schema = $storage_definition
->getSchema();
$properties = $storage_definition
->getPropertyDefinitions();
$table_mapping = $this
->getTableMapping($entity_type, [
$storage_definition,
]);
if (array_intersect(array_keys($schema['columns']), $table_mapping
->getReservedColumns())) {
throw new FieldException("Illegal field column names on {$storage_definition->getName()}");
}
foreach ($schema['columns'] as $column_name => $attributes) {
$real_name = $table_mapping
->getFieldColumnName($storage_definition, $column_name);
$data_schema['fields'][$real_name] = $attributes;
if (isset($properties[$column_name])) {
$data_schema['fields'][$real_name]['not null'] = $properties[$column_name]
->isRequired();
}
}
foreach ($schema['indexes'] as $index_name => $columns) {
$real_name = $this
->getFieldIndexName($storage_definition, $index_name);
foreach ($columns as $column_name) {
if (is_array($column_name)) {
$data_schema['indexes'][$real_name][] = [
$table_mapping
->getFieldColumnName($storage_definition, $column_name[0]),
$column_name[1],
];
}
else {
$data_schema['indexes'][$real_name][] = $table_mapping
->getFieldColumnName($storage_definition, $column_name);
}
}
}
foreach ($schema['unique keys'] as $index_name => $columns) {
$real_name = $this
->getFieldIndexName($storage_definition, $index_name);
foreach ($columns as $column_name) {
if (is_array($column_name)) {
$data_schema['unique keys'][$real_name][] = [
$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);
}
}
}
foreach ($schema['foreign keys'] as $specifier => $specification) {
$real_name = $this
->getFieldIndexName($storage_definition, $specifier);
$data_schema['foreign keys'][$real_name]['table'] = $specification['table'];
foreach ($specification['columns'] as $column_name => $referenced) {
$sql_storage_column = $table_mapping
->getFieldColumnName($storage_definition, $column_name);
$data_schema['foreign keys'][$real_name]['columns'][$sql_storage_column] = $referenced;
}
}
$dedicated_table_schema = [
$table_mapping
->getDedicatedDataTableName($storage_definition) => $data_schema,
];
if ($entity_type
->isRevisionable()) {
$revision_schema = $data_schema;
$revision_schema['description'] = $description_revision;
$revision_schema['primary key'] = [
'entity_id',
'revision_id',
'deleted',
'delta',
'langcode',
];
$revision_schema['fields']['revision_id']['not null'] = TRUE;
$revision_schema['fields']['revision_id']['description'] = 'The entity revision id this data is attached to';
$dedicated_table_schema += [
$table_mapping
->getDedicatedRevisionTableName($storage_definition) => $revision_schema,
];
}
return $dedicated_table_schema;
}
protected function getEntityIndexName(ContentEntityTypeInterface $entity_type, $index) {
return $entity_type
->id() . '__' . $index;
}
protected function getFieldIndexName(FieldStorageDefinitionInterface $storage_definition, $index) {
return $storage_definition
->getName() . '_' . $index;
}
protected function isTableEmpty($table_name) {
return !$this->database
->schema()
->tableExists($table_name) || !$this->database
->select($table_name)
->countQuery()
->range(0, 1)
->execute()
->fetchField();
}
protected function hasColumnChanges(FieldStorageDefinitionInterface $storage_definition, FieldStorageDefinitionInterface $original) {
if ($storage_definition
->getColumns() != $original
->getColumns()) {
return TRUE;
}
if (!$storage_definition
->hasCustomStorage()) {
$keys = array_flip($this
->getColumnSchemaRelevantKeys());
$definition_schema = $this
->getSchemaFromStorageDefinition($storage_definition);
foreach ($this
->loadFieldSchemaData($original) as $table => $table_schema) {
foreach ($table_schema['fields'] as $name => $spec) {
$definition_spec = array_intersect_key($definition_schema[$table]['fields'][$name], $keys);
$stored_spec = array_intersect_key($spec, $keys);
if ($definition_spec != $stored_spec) {
return TRUE;
}
}
}
}
return FALSE;
}
protected function getColumnSchemaRelevantKeys() {
return [
'type',
'size',
'length',
'unsigned',
];
}
protected function addIndex($table, $name, array $specifier, array $schema) {
$schema_handler = $this->database
->schema();
$schema_handler
->dropIndex($table, $name);
$schema_handler
->addIndex($table, $name, $specifier, $schema);
}
protected function addUniqueKey($table, $name, array $specifier) {
$schema_handler = $this->database
->schema();
$schema_handler
->dropUniqueKey($table, $name);
$schema_handler
->addUniqueKey($table, $name, $specifier);
}
public static function castValue(array $info, $value) {
if (isset($value) || !empty($info['not null'])) {
if ($info['type'] === 'int' || $info['type'] === 'serial') {
return (int) $value;
}
elseif ($info['type'] === 'float') {
return (double) $value;
}
return (string) $value;
}
return $value;
}
}