SqlContentEntityStorageSchema.php 74.1 KB
Newer Older
1
2
3
4
<?php

/**
 * @file
5
 * Contains \Drupal\Core\Entity\Sql\SqlContentEntityStorageSchema.
6
7
 */

8
namespace Drupal\Core\Entity\Sql;
9

10
use Drupal\Core\Database\Connection;
11
use Drupal\Core\Database\DatabaseException;
12
13
use Drupal\Core\Entity\ContentEntityTypeInterface;
use Drupal\Core\Entity\EntityManagerInterface;
14
use Drupal\Core\Entity\EntityStorageException;
15
use Drupal\Core\Entity\EntityTypeInterface;
16
use Drupal\Core\Entity\Exception\FieldStorageDefinitionUpdateForbiddenException;
17
use Drupal\Core\Entity\Schema\DynamicallyFieldableEntityStorageSchemaInterface;
18
use Drupal\Core\Field\FieldException;
19
use Drupal\Core\Field\FieldStorageDefinitionInterface;
20
use Drupal\field\FieldStorageConfigInterface;
21
22
23

/**
 * Defines a schema handler that supports revisionable, translatable entities.
24
25
26
27
28
 *
 * Entity types may extend this class and optimize the generated schema for all
 * entity base tables by overriding getEntitySchema() for cross-field
 * optimizations and getSharedTableFieldSchema() for optimizations applying to
 * a single field.
29
 */
30
class SqlContentEntityStorageSchema implements DynamicallyFieldableEntityStorageSchemaInterface {
31

32
33
34
35
36
37
38
  /**
   * The entity manager.
   *
   * @var \Drupal\Core\Entity\EntityManagerInterface
   */
  protected $entityManager;

39
40
41
42
43
44
45
46
47
48
  /**
   * The entity type this schema builder is responsible for.
   *
   * @var \Drupal\Core\Entity\ContentEntityTypeInterface
   */
  protected $entityType;

  /**
   * The storage field definitions for this entity type.
   *
49
   * @var \Drupal\Core\Field\FieldStorageDefinitionInterface[]
50
51
52
   */
  protected $fieldStorageDefinitions;

53
54
55
56
57
58
59
60
  /**
   * The original storage field definitions for this entity type. Used during
   * field schema updates.
   *
   * @var \Drupal\Core\Field\FieldDefinitionInterface[]
   */
  protected $originalDefinitions;

61
62
63
  /**
   * The storage object for the given entity type.
   *
64
   * @var \Drupal\Core\Entity\Sql\SqlContentEntityStorage
65
66
67
68
69
70
71
72
73
74
   */
  protected $storage;

  /**
   * A static cache of the generated schema array.
   *
   * @var array
   */
  protected $schema;

75
76
77
78
79
80
81
  /**
   * The database connection to be used.
   *
   * @var \Drupal\Core\Database\Connection
   */
  protected $database;

82
83
84
85
86
87
88
  /**
   * The key-value collection for tracking installed storage schema.
   *
   * @var \Drupal\Core\KeyValueStore\KeyValueStoreInterface
   */
  protected $installedStorageSchema;

89
  /**
90
   * Constructs a SqlContentEntityStorageSchema.
91
92
93
94
95
   *
   * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
   *   The entity manager.
   * @param \Drupal\Core\Entity\ContentEntityTypeInterface $entity_type
   *   The entity type.
96
   * @param \Drupal\Core\Entity\Sql\SqlContentEntityStorage $storage
97
   *   The storage of the entity type. This must be an SQL-based storage.
98
99
   * @param \Drupal\Core\Database\Connection $database
   *   The database connection to be used.
100
   */
101
  public function __construct(EntityManagerInterface $entity_manager, ContentEntityTypeInterface $entity_type, SqlContentEntityStorage $storage, Connection $database) {
102
    $this->entityManager = $entity_manager;
103
104
105
    $this->entityType = $entity_type;
    $this->fieldStorageDefinitions = $entity_manager->getFieldStorageDefinitions($entity_type->id());
    $this->storage = $storage;
106
107
108
    $this->database = $database;
  }

109
  /**
110
   * Gets the keyvalue collection for tracking the installed schema.
111
112
113
114
115
116
117
118
119
120
121
122
123
124
   *
   * @return \Drupal\Core\KeyValueStore\KeyValueStoreInterface
   *
   * @todo Inject this dependency in the constructor once this class can be
   *   instantiated as a regular entity handler:
   *   https://www.drupal.org/node/2332857.
   */
  protected function installedStorageSchema() {
    if (!isset($this->installedStorageSchema)) {
      $this->installedStorageSchema = \Drupal::keyValue('entity.storage_schema.sql');
    }
    return $this->installedStorageSchema;
  }

125
126
127
128
129
  /**
   * {@inheritdoc}
   */
  public function requiresEntityStorageSchemaChanges(EntityTypeInterface $entity_type, EntityTypeInterface $original) {
    return
alexpott's avatar
alexpott committed
130
      $this->hasSharedTableStructureChange($entity_type, $original) ||
131
132
      // Detect changes in key or index definitions.
      $this->getEntitySchemaData($entity_type, $this->getEntitySchema($entity_type, TRUE)) != $this->loadEntitySchemaData($original);
133
134
  }

alexpott's avatar
alexpott committed
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
  /**
   * Detects whether there is a change in the shared table structure.
   *
   * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
   *   The new entity type.
   * @param \Drupal\Core\Entity\EntityTypeInterface $original
   *   The origin entity type.
   *
   * @return bool
   *   Returns TRUE if either the revisionable or translatable flag changes or
   *   a table has been renamed.
   */
  protected function hasSharedTableStructureChange(EntityTypeInterface $entity_type, EntityTypeInterface $original) {
    return
      $entity_type->isRevisionable() != $original->isRevisionable() ||
      $entity_type->isTranslatable() != $original->isTranslatable() ||
      $this->hasSharedTableNameChanges($entity_type, $original);
  }

154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
  /**
   * Detects whether any table name got renamed in an entity type update.
   *
   * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
   *   The new entity type.
   * @param \Drupal\Core\Entity\EntityTypeInterface $original
   *   The origin entity type.
   *
   * @return bool
   *   Returns TRUE if there have been changes.
   */
  protected function hasSharedTableNameChanges(EntityTypeInterface $entity_type, EntityTypeInterface $original) {
    return
      $entity_type->getBaseTable() != $original->getBaseTable() ||
      $entity_type->getDataTable() != $original->getDataTable() ||
      $entity_type->getRevisionTable() != $original->getRevisionTable() ||
      $entity_type->getRevisionDataTable() != $original->getRevisionDataTable();
  }

173
174
175
176
  /**
   * {@inheritdoc}
   */
  public function requiresFieldStorageSchemaChanges(FieldStorageDefinitionInterface $storage_definition, FieldStorageDefinitionInterface $original) {
177
178
179
    $table_mapping = $this->storage->getTableMapping();

    if (
180
181
182
      $storage_definition->hasCustomStorage() != $original->hasCustomStorage() ||
      $storage_definition->getSchema() != $original->getSchema() ||
      $storage_definition->isRevisionable() != $original->isRevisionable() ||
183
184
185
186
187
188
      $table_mapping->allowsSharedTableStorage($storage_definition) != $table_mapping->allowsSharedTableStorage($original) ||
      $table_mapping->requiresDedicatedTableStorage($storage_definition) != $table_mapping->requiresDedicatedTableStorage($original)
    ) {
      return TRUE;
    }

189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
    if ($storage_definition->hasCustomStorage()) {
      // The field has custom storage, so we don't know if a schema change is
      // needed or not, but since per the initial checks earlier in this
      // function, nothing about the definition changed that we manage, we
      // return FALSE.
      return FALSE;
    }

    return $this->getSchemaFromStorageDefinition($storage_definition) != $this->loadFieldSchemaData($original);
  }

  /**
   * Gets the schema data for the given field storage definition.
   *
   * @param \Drupal\Core\Field\FieldStorageDefinitionInterface $storage_definition
   *   The field storage definition. The field that must not have custom
   *   storage, i.e. the storage must take care of storing the field.
   *
   * @return array
   *   The schema data.
   */
  protected function getSchemaFromStorageDefinition(FieldStorageDefinitionInterface $storage_definition) {
    assert('!$storage_definition->hasCustomStorage();');
    $table_mapping = $this->storage->getTableMapping();
    $schema = [];
214
    if ($table_mapping->requiresDedicatedTableStorage($storage_definition)) {
215
      $schema = $this->getDedicatedTableSchema($storage_definition);
216
217
218
219
220
221
222
223
224
225
    }
    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);
        }
      }
    }
226
    return $schema;
227
228
229
230
231
232
  }

  /**
   * {@inheritdoc}
   */
  public function requiresEntityDataMigration(EntityTypeInterface $entity_type, EntityTypeInterface $original) {
233
234
    // If the original storage has existing entities, or it is impossible to
    // determine if that is the case, require entity data to be migrated.
235
    $original_storage_class = $original->getStorageClass();
236
237
    if (!class_exists($original_storage_class)) {
      return TRUE;
238
    }
alexpott's avatar
alexpott committed
239
240
241
242
243
244
245

    // Data migration is not needed when only indexes changed, as they can be
    // applied if there is data.
    if (!$this->hasSharedTableStructureChange($entity_type, $original)) {
      return FALSE;
    }

246
247
248
    // Use the original entity type since the storage has not been updated.
    $original_storage = $this->entityManager->createHandlerInstance($original_storage_class, $original);
    return $original_storage->hasData();
249
250
251
252
253
254
  }

  /**
   * {@inheritdoc}
   */
  public function requiresFieldDataMigration(FieldStorageDefinitionInterface $storage_definition, FieldStorageDefinitionInterface $original) {
255
    return !$this->storage->countFieldData($original, TRUE);
256
257
  }

258
259
260
261
  /**
   * {@inheritdoc}
   */
  public function onEntityTypeCreate(EntityTypeInterface $entity_type) {
262
    $this->checkEntityType($entity_type);
263
    $schema_handler = $this->database->schema();
264
265

    // Create entity tables.
266
267
268
269
270
271
    $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);
      }
    }
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288

    // Create dedicated field tables.
    $field_storage_definitions = $this->entityManager->getFieldStorageDefinitions($entity_type->id());
    $table_mapping = $this->storage->getTableMapping($field_storage_definitions);
    foreach ($field_storage_definitions as $field_storage_definition) {
      if ($table_mapping->requiresDedicatedTableStorage($field_storage_definition)) {
        $this->createDedicatedTableSchema($field_storage_definition);
      }
      elseif ($table_mapping->allowsSharedTableStorage($field_storage_definition)) {
        // The shared tables are already fully created, but we need to save the
        // per-field schema definitions for later use.
        $this->createSharedTableSchema($field_storage_definition, TRUE);
      }
    }

    // Save data about entity indexes and keys.
    $this->saveEntitySchemaData($entity_type, $schema);
289
290
291
292
293
294
  }

  /**
   * {@inheritdoc}
   */
  public function onEntityTypeUpdate(EntityTypeInterface $entity_type, EntityTypeInterface $original) {
295
296
297
298
299
300
301
302
    $this->checkEntityType($entity_type);
    $this->checkEntityType($original);

    // If no schema changes are needed, we don't need to do anything.
    if (!$this->requiresEntityStorageSchemaChanges($entity_type, $original)) {
      return;
    }

303
304
    // If a migration is required, we can't proceed.
    if ($this->requiresEntityDataMigration($entity_type, $original)) {
305
      throw new EntityStorageException('The SQL storage cannot change the schema for an existing entity type (' . $entity_type->id() . ') with data.');
306
307
    }

308
    // If we have no data just recreate the entity schema from scratch.
309
    if ($this->isTableEmpty($this->storage->getBaseTable())) {
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
      if ($this->database->supportsTransactionalDDL()) {
        // If the database supports transactional DDL, we can go ahead and rely
        // on it. If not, we will have to rollback manually if something fails.
        $transaction = $this->database->startTransaction();
      }
      try {
        $this->onEntityTypeDelete($original);
        $this->onEntityTypeCreate($entity_type);
      }
      catch (\Exception $e) {
        if ($this->database->supportsTransactionalDDL()) {
          $transaction->rollback();
        }
        else {
          // Recreate original schema.
          $this->onEntityTypeCreate($original);
        }
        throw $e;
      }
    }
    else {
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
      $schema_handler = $this->database->schema();

      // Drop original indexes and unique keys.
      foreach ($this->loadEntitySchemaData($entity_type) as $table_name => $schema) {
        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);
          }
        }
      }

      // Create new indexes and unique keys.
      $entity_schema = $this->getEntitySchema($entity_type, TRUE);
      foreach ($this->getEntitySchemaData($entity_type, $entity_schema) as $table_name => $schema) {
350
351
352
353
        // Add fields schema because database driver may depend on this data to
        // perform index normalization.
        $schema['fields'] = $entity_schema[$table_name]['fields'];

354
355
        if (!empty($schema['indexes'])) {
          foreach ($schema['indexes'] as $name => $specifier) {
356
            $schema_handler->addIndex($table_name, $name, $specifier, $schema);
357
358
359
360
361
362
363
364
365
366
367
          }
        }
        if (!empty($schema['unique keys'])) {
          foreach ($schema['unique keys'] as $name => $specifier) {
            $schema_handler->addUniqueKey($table_name, $name, $specifier);
          }
        }
      }

      // Store the updated entity schema.
      $this->saveEntitySchemaData($entity_type, $entity_schema);
368
    }
369
370
371
372
373
  }

  /**
   * {@inheritdoc}
   */
374
  public function onEntityTypeDelete(EntityTypeInterface $entity_type) {
375
    $this->checkEntityType($entity_type);
376
    $schema_handler = $this->database->schema();
377
378
379
380
381
382
383
    $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.
384
    foreach ($this->getEntitySchemaTables() as $table_name) {
385
386
387
388
      if ($schema_handler->tableExists($table_name)) {
        $schema_handler->dropTable($table_name);
      }
    }
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404

    // 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);
    foreach ($field_storage_definitions as $field_storage_definition) {
      if ($table_mapping->requiresDedicatedTableStorage($field_storage_definition)) {
        $this->deleteDedicatedTableSchema($field_storage_definition);
      }
    }
    $this->originalDefinitions = NULL;

    $this->storage->setEntityType($actual_definition);

    // Delete the entity schema.
    $this->deleteEntitySchemaData($entity_type);
405
406
  }

407
408
409
410
  /**
   * {@inheritdoc}
   */
  public function onFieldStorageDefinitionCreate(FieldStorageDefinitionInterface $storage_definition) {
411
    $this->performFieldSchemaOperation('create', $storage_definition);
412
413
414
415
416
417
  }

  /**
   * {@inheritdoc}
   */
  public function onFieldStorageDefinitionUpdate(FieldStorageDefinitionInterface $storage_definition, FieldStorageDefinitionInterface $original) {
418
419
420
421
422
423
    // Store original definitions so that switching between shared and dedicated
    // field table layout works.
    $this->originalDefinitions = $this->fieldStorageDefinitions;
    $this->originalDefinitions[$original->getName()] = $original;
    $this->performFieldSchemaOperation('update', $storage_definition, $original);
    $this->originalDefinitions = NULL;
424
425
426
427
428
429
  }

  /**
   * {@inheritdoc}
   */
  public function onFieldStorageDefinitionDelete(FieldStorageDefinitionInterface $storage_definition) {
430
431
432
    // Only configurable fields currently support purging, so prevent deletion
    // of ones we can't purge if they have existing data.
    // @todo Add purging to all fields: https://www.drupal.org/node/2282119.
433
434
    try {
      if (!($storage_definition instanceof FieldStorageConfigInterface) && $this->storage->countFieldData($storage_definition, TRUE)) {
435
        throw new FieldStorageDefinitionUpdateForbiddenException('Unable to delete a field (' . $storage_definition->getName() . ' in ' . $storage_definition->getTargetEntityTypeId() . ' entity) with data that cannot be purged.');
436
437
438
439
440
441
442
443
      }
    }
    catch (DatabaseException $e) {
      // This may happen when changing field storage schema, since we are not
      // able to use a table mapping matching the passed storage definition.
      // @todo Revisit this once we are able to instantiate the table mapping
      //   properly. See https://www.drupal.org/node/2274017.
      return;
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
    }

    // Retrieve a table mapping which contains the deleted field still.
    $table_mapping = $this->storage->getTableMapping(
      $this->entityManager->getLastInstalledFieldStorageDefinitions($this->entityType->id())
    );
    if ($table_mapping->requiresDedicatedTableStorage($storage_definition)) {
      // Move the table to a unique name while the table contents are being
      // deleted.
      $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);
      }
    }

    // @todo Remove when finalizePurge() is invoked from the outside for all
    //   fields: https://www.drupal.org/node/2282119.
    if (!($storage_definition instanceof FieldStorageConfigInterface)) {
      $this->performFieldSchemaOperation('delete', $storage_definition);
    }
  }

  /**
   * {@inheritdoc}
   */
  public function finalizePurge(FieldStorageDefinitionInterface $storage_definition) {
    $this->performFieldSchemaOperation('delete', $storage_definition);
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
  }

  /**
   * Checks that we are dealing with the correct entity type.
   *
   * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
   *   The entity type to be checked.
   *
   * @return bool
   *   TRUE if the entity type matches the current one.
   *
   * @throws \Drupal\Core\Entity\EntityStorageException
   */
  protected function checkEntityType(EntityTypeInterface $entity_type) {
    if ($entity_type->id() != $this->entityType->id()) {
490
      throw new EntityStorageException("Unsupported entity type {$entity_type->id()}");
491
492
493
494
    }
    return TRUE;
  }

495
  /**
496
   * Gets the entity schema for the specified entity type.
497
   *
498
499
500
501
502
503
   * Entity types may override this method in order to optimize the generated
   * schema of the entity tables. However, only cross-field optimizations should
   * be added here; e.g., an index spanning multiple fields. Optimizations that
   * apply to a single field have to be added via
   * SqlContentEntityStorageSchema::getSharedTableFieldSchema() instead.
   *
504
505
506
507
508
509
510
511
512
   * @param \Drupal\Core\Entity\ContentEntityTypeInterface $entity_type
   *   The entity type definition.
   * @param bool $reset
   *   (optional) If set to TRUE static cache will be ignored and a new schema
   *   array generation will be performed. Defaults to FALSE.
   *
   * @return array
   *   A Schema API array describing the entity schema, excluding dedicated
   *   field tables.
513
514
   *
   * @throws \Drupal\Core\Field\FieldException
515
516
   */
  protected function getEntitySchema(ContentEntityTypeInterface $entity_type, $reset = FALSE) {
517
    $this->checkEntityType($entity_type);
518
    $entity_type_id = $entity_type->id();
519

520
    if (!isset($this->schema[$entity_type_id]) || $reset) {
521
522
523
524
525
526
527
528
      // 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.
529
      $tables = $this->getEntitySchemaTables();
530
531
532

      // Initialize the table schema.
      $schema[$tables['base_table']] = $this->initializeBaseTable($entity_type);
533
      if (isset($tables['revision_table'])) {
534
        $schema[$tables['revision_table']] = $this->initializeRevisionTable($entity_type);
535
536
      }
      if (isset($tables['data_table'])) {
537
        $schema[$tables['data_table']] = $this->initializeDataTable($entity_type);
538
539
      }
      if (isset($tables['revision_data_table'])) {
540
        $schema[$tables['revision_data_table']] = $this->initializeRevisionDataTable($entity_type);
541
542
      }

543
      // We need to act only on shared entity schema tables.
544
      $table_mapping = $this->storage->getTableMapping();
545
546
547
548
549
550
      $table_names = array_diff($table_mapping->getTableNames(), $table_mapping->getDedicatedTableNames());
      $storage_definitions = $this->entityManager->getFieldStorageDefinitions($entity_type_id);
      foreach ($table_names as $table_name) {
        if (!isset($schema[$table_name])) {
          $schema[$table_name] = array();
        }
551
        foreach ($table_mapping->getFieldNames($table_name) as $field_name) {
552
          if (!isset($storage_definitions[$field_name])) {
553
            throw new FieldException("Field storage definition for '$field_name' could not be found.");
554
555
556
557
558
559
560
          }
          // Add the schema for base field definitions.
          elseif ($table_mapping->allowsSharedTableStorage($storage_definitions[$field_name])) {
            $column_names = $table_mapping->getColumnNames($field_name);
            $storage_definition = $storage_definitions[$field_name];
            $schema[$table_name] = array_merge_recursive($schema[$table_name], $this->getSharedTableFieldSchema($storage_definition, $table_name, $column_names));
          }
561
562
563
564
        }
      }

      // Process tables after having gathered field information.
565
      $this->processBaseTable($entity_type, $schema[$tables['base_table']]);
566
      if (isset($tables['revision_table'])) {
567
        $this->processRevisionTable($entity_type, $schema[$tables['revision_table']]);
568
569
      }
      if (isset($tables['data_table'])) {
570
        $this->processDataTable($entity_type, $schema[$tables['data_table']]);
571
572
      }
      if (isset($tables['revision_data_table'])) {
573
        $this->processRevisionDataTable($entity_type, $schema[$tables['revision_data_table']]);
574
575
      }

576
      $this->schema[$entity_type_id] = $schema;
577
578
579

      // Restore the actual definition.
      $this->storage->setEntityType($actual_definition);
580
581
    }

582
    return $this->schema[$entity_type_id];
583
584
585
586
587
588
589
590
  }

  /**
   * Gets a list of entity type tables.
   *
   * @return array
   *   A list of entity type tables, keyed by table key.
   */
591
  protected function getEntitySchemaTables() {
592
593
594
595
596
597
598
599
600
    return array_filter(array(
      'base_table' => $this->storage->getBaseTable(),
      'revision_table' => $this->storage->getRevisionTable(),
      'data_table' => $this->storage->getDataTable(),
      'revision_data_table' => $this->storage->getRevisionDataTable(),
    ));
  }

  /**
601
   * Gets entity schema definitions for index and key definitions.
602
   *
603
604
   * @param \Drupal\Core\Entity\ContentEntityTypeInterface $entity_type
   *   The entity type definition.
605
   * @param array $schema
606
607
608
609
610
611
   *   The entity schema array.
   *
   * @return array
   *   A stripped down version of the $schema Schema API array containing, for
   *   each table, only the key and index definitions not derived from field
   *   storage definitions.
612
   */
613
614
615
616
617
  protected function getEntitySchemaData(ContentEntityTypeInterface $entity_type, array $schema) {
    $schema_data = array();
    $entity_type_id = $entity_type->id();
    $keys = array('indexes', 'unique keys');
    $unused_keys = array_flip(array('description', 'fields', 'foreign keys'));
618

619
620
621
622
623
624
625
626
627
628
629
630
    foreach ($schema as $table_name => $table_schema) {
      $table_schema = array_diff_key($table_schema, $unused_keys);
      foreach ($keys as $key) {
        // Exclude data generated from field storage definitions, we will check
        // that separately.
        if (!empty($table_schema[$key])) {
          $data_keys = array_keys($table_schema[$key]);
          $entity_keys = array_filter($data_keys, function ($key) use ($entity_type_id) {
            return strpos($key, $entity_type_id . '_field__') !== 0;
          });
          $table_schema[$key] = array_intersect_key($table_schema[$key], array_flip($entity_keys));
        }
631
      }
632
      $schema_data[$table_name] = array_filter($table_schema);
633
634
    }

635
    return $schema_data;
636
637
638
  }

  /**
639
   * Gets an index schema array for a given field.
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
   *
   * @param string $field_name
   *   The name of the field.
   * @param array $field_schema
   *   The schema of the field.
   * @param string[] $column_mapping
   *   A mapping of field column names to database column names.
   *
   * @return array
   *   The schema definition for the indexes.
   */
  protected function getFieldIndexes($field_name, array $field_schema, array $column_mapping) {
    return $this->getFieldSchemaData($field_name, $field_schema, $column_mapping, 'indexes');
  }

  /**
656
   * Gets a unique key schema array for a given field.
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
   *
   * @param string $field_name
   *   The name of the field.
   * @param array $field_schema
   *   The schema of the field.
   * @param string[] $column_mapping
   *   A mapping of field column names to database column names.
   *
   * @return array
   *   The schema definition for the unique keys.
   */
  protected function getFieldUniqueKeys($field_name, array $field_schema, array $column_mapping) {
    return $this->getFieldSchemaData($field_name, $field_schema, $column_mapping, 'unique keys');
  }

  /**
673
   * Gets field schema data for the given key.
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
   *
   * @param string $field_name
   *   The name of the field.
   * @param array $field_schema
   *   The schema of the field.
   * @param string[] $column_mapping
   *   A mapping of field column names to database column names.
   * @param string $schema_key
   *   The type of schema data. Either 'indexes' or 'unique keys'.
   *
   * @return array
   *   The schema definition for the specified key.
   */
  protected function getFieldSchemaData($field_name, array $field_schema, array $column_mapping, $schema_key) {
    $data = array();

    foreach ($field_schema[$schema_key] as $key => $columns) {
      // To avoid clashes with entity-level indexes or unique keys we use
      // "{$entity_type_id}_field__" as a prefix instead of just
      // "{$entity_type_id}__". We additionally namespace the specifier by the
      // field name to avoid clashes when multiple fields of the same type are
      // added to an entity type.
      $entity_type_id = $this->entityType->id();
697
      $real_key = $this->getFieldSchemaIdentifierName($entity_type_id, $field_name, $key);
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
      foreach ($columns as $column) {
        // Allow for indexes and unique keys to specified as an array of column
        // name and length.
        if (is_array($column)) {
          list($column_name, $length) = $column;
          $data[$real_key][] = array($column_mapping[$column_name], $length);
        }
        else {
          $data[$real_key][] = $column_mapping[$column];
        }
      }
    }

    return $data;
  }

714
715
716
717
718
719
720
  /**
   * Generates a safe schema identifier (name of an index, column name etc.).
   *
   * @param string $entity_type_id
   *   The ID of the entity type.
   * @param string $field_name
   *   The name of the field.
721
722
   * @param string|null $key
   *   (optional) A further key to append to the name.
723
724
725
726
   *
   * @return string
   *   The field identifier name.
   */
727
728
  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}";
729
730
731
732
733
734
735
736
737
738
739
740
741
    // Limit the string to 48 characters, keeping a 16 characters margin for db
    // prefixes.
    if (strlen($real_key) > 48) {
      // Use a shorter separator, a truncated entity_type, and a hash of the
      // field name.
      // Truncate to the same length for the current and revision tables.
      $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;
  }

742
  /**
743
   * Gets field foreign keys.
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
   *
   * @param string $field_name
   *   The name of the field.
   * @param array $field_schema
   *   The schema of the field.
   * @param string[] $column_mapping
   *   A mapping of field column names to database column names.
   *
   * @return array
   *   The schema definition for the foreign keys.
   */
  protected function getFieldForeignKeys($field_name, array $field_schema, array $column_mapping) {
    $foreign_keys = array();

    foreach ($field_schema['foreign keys'] as $specifier => $specification) {
      // To avoid clashes with entity-level foreign keys we use
      // "{$entity_type_id}_field__" as a prefix instead of just
      // "{$entity_type_id}__". We additionally namespace the specifier by the
      // field name to avoid clashes when multiple fields of the same type are
      // added to an entity type.
      $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;
  }

775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
  /**
   * Loads stored schema data for the given entity type definition.
   *
   * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
   *   The entity type definition.
   *
   * @return array
   *   The entity schema data array.
   */
  protected function loadEntitySchemaData(EntityTypeInterface $entity_type) {
    return $this->installedStorageSchema()->get($entity_type->id() . '.entity_schema_data', array());
  }

  /**
   * Stores schema data for the given entity type definition.
   *
   * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
   *   The entity type definition.
   * @param array $schema
   *   The entity schema data array.
   */
  protected function saveEntitySchemaData(EntityTypeInterface $entity_type, $schema) {
    $data = $this->getEntitySchemaData($entity_type, $schema);
    $this->installedStorageSchema()->set($entity_type->id() . '.entity_schema_data', $data);
  }

  /**
   * Deletes schema data for the given entity type definition.
   *
   * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
   *   The entity type definition.
   */
  protected function deleteEntitySchemaData(EntityTypeInterface $entity_type) {
    $this->installedStorageSchema()->delete($entity_type->id() . '.entity_schema_data');
  }

  /**
   * Loads stored schema data for the given field storage definition.
   *
   * @param \Drupal\Core\Field\FieldStorageDefinitionInterface $storage_definition
   *   The field storage definition.
   *
   * @return array
   *   The field schema data array.
   */
  protected function loadFieldSchemaData(FieldStorageDefinitionInterface $storage_definition) {
    return $this->installedStorageSchema()->get($storage_definition->getTargetEntityTypeId() . '.field_schema_data.' . $storage_definition->getName(), array());
  }

  /**
   * Stores schema data for the given field storage definition.
   *
   * @param \Drupal\Core\Field\FieldStorageDefinitionInterface $storage_definition
   *   The field storage definition.
   * @param array $schema
   *   The field schema data array.
   */
  protected function saveFieldSchemaData(FieldStorageDefinitionInterface $storage_definition, $schema) {
    $this->installedStorageSchema()->set($storage_definition->getTargetEntityTypeId() . '.field_schema_data.' . $storage_definition->getName(), $schema);
  }

  /**
   * Deletes schema data for the given field storage definition.
   *
   * @param \Drupal\Core\Field\FieldStorageDefinitionInterface $storage_definition
   *   The field storage definition.
   */
  protected function deleteFieldSchemaData(FieldStorageDefinitionInterface $storage_definition) {
    $this->installedStorageSchema()->delete($storage_definition->getTargetEntityTypeId() . '.field_schema_data.' . $storage_definition->getName());
  }

846
847
848
  /**
   * Initializes common information for a base table.
   *
849
850
851
   * @param \Drupal\Core\Entity\ContentEntityTypeInterface $entity_type
   *   The entity type.
   *
852
853
854
   * @return array
   *   A partial schema array for the base table.
   */
855
856
  protected function initializeBaseTable(ContentEntityTypeInterface $entity_type) {
    $entity_type_id = $entity_type->id();
857
858
859

    $schema = array(
      'description' => "The base table for $entity_type_id entities.",
860
      'primary key' => array($entity_type->getKey('id')),
861
862
863
864
      'indexes' => array(),
      'foreign keys' => array(),
    );

865
866
867
    if ($entity_type->hasKey('revision')) {
      $revision_key = $entity_type->getKey('revision');
      $key_name = $this->getEntityIndexName($entity_type, $revision_key);
868
869
870
871
872
873
874
      $schema['unique keys'][$key_name] = array($revision_key);
      $schema['foreign keys'][$entity_type_id . '__revision'] = array(
        'table' => $this->storage->getRevisionTable(),
        'columns' => array($revision_key => $revision_key),
      );
    }

875
876
    $this->addTableDefaults($schema);

877
878
879
880
881
882
    return $schema;
  }

  /**
   * Initializes common information for a revision table.
   *
883
884
885
   * @param \Drupal\Core\Entity\ContentEntityTypeInterface $entity_type
   *   The entity type.
   *
886
887
888
   * @return array
   *   A partial schema array for the revision table.
   */
889
890
891
892
  protected function initializeRevisionTable(ContentEntityTypeInterface $entity_type) {
    $entity_type_id = $entity_type->id();
    $id_key = $entity_type->getKey('id');
    $revision_key = $entity_type->getKey('revision');
893
894
895
896
897
898

    $schema = array(
      'description' => "The revision table for $entity_type_id entities.",
      'primary key' => array($revision_key),
      'indexes' => array(),
      'foreign keys' => array(
899
        $entity_type_id . '__revisioned' => array(
900
901
902
903
904
905
          'table' => $this->storage->getBaseTable(),
          'columns' => array($id_key => $id_key),
        ),
      ),
    );

906
    $schema['indexes'][$this->getEntityIndexName($entity_type, $id_key)] = array($id_key);
907

908
909
    $this->addTableDefaults($schema);

910
911
912
913
914
915
    return $schema;
  }

  /**
   * Initializes common information for a data table.
   *
916
917
918
   * @param \Drupal\Core\Entity\ContentEntityTypeInterface $entity_type
   *   The entity type.
   *
919
920
921
   * @return array
   *   A partial schema array for the data table.
   */
922
923
924
  protected function initializeDataTable(ContentEntityTypeInterface $entity_type) {
    $entity_type_id = $entity_type->id();
    $id_key = $entity_type->getKey('id');
925
926
927

    $schema = array(
      'description' => "The data table for $entity_type_id entities.",
928
      'primary key' => array($id_key, $entity_type->getKey('langcode')),
929
930
931
      'indexes' => array(
        $entity_type_id . '__id__default_langcode__langcode' => array($id_key, $entity_type->getKey('default_langcode'), $entity_type->getKey('langcode')),
      ),
932
933
934
935
936
937
938
939
      'foreign keys' => array(
        $entity_type_id => array(
          'table' => $this->storage->getBaseTable(),
          'columns' => array($id_key => $id_key),
        ),
      ),
    );

940
941
942
    if ($entity_type->hasKey('revision')) {
      $key = $entity_type->getKey('revision');
      $schema['indexes'][$this->getEntityIndexName($entity_type, $key)] = array($key);
943
944
    }

945
946
    $this->addTableDefaults($schema);

947
948
949
950
951
952
    return $schema;
  }

  /**
   * Initializes common information for a revision data table.
   *
953
954
955
   * @param \Drupal\Core\Entity\ContentEntityTypeInterface $entity_type
   *   The entity type.
   *
956
957
958
   * @return array
   *   A partial schema array for the revision data table.
   */
959
960
961
962
  protected function initializeRevisionDataTable(ContentEntityTypeInterface $entity_type) {
    $entity_type_id = $entity_type->id();
    $id_key = $entity_type->getKey('id');
    $revision_key = $entity_type->getKey('revision');
963
964
965

    $schema = array(
      'description' => "The revision data table for $entity_type_id entities.",
966
      'primary key' => array($revision_key, $entity_type->getKey('langcode')),
967
968
969
      'indexes' => array(
        $entity_type_id . '__id__default_langcode__langcode' => array($id_key, $entity_type->getKey('default_langcode'), $entity_type->getKey('langcode')),
      ),
970
971
972
973
974
975
976
977
978
979
980
981
      'foreign keys' => array(
        $entity_type_id => array(
          'table' => $this->storage->getBaseTable(),
          'columns' => array($id_key => $id_key),
        ),
        $entity_type_id . '__revision' => array(
          'table' => $this->storage->getRevisionTable(),
          'columns' => array($revision_key => $revision_key),
        )
      ),
    );

982
983
    $this->addTableDefaults($schema);

984
985
986
    return $schema;
  }

987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
  /**
   * Adds defaults to a table schema definition.
   *
   * @param $schema
   *   The schema definition array for a single table, passed by reference.
   */
  protected function addTableDefaults(&$schema) {
    $schema += array(
      'fields' => array(),
      'unique keys' => array(),
      'indexes' => array(),
      'foreign keys' => array(),
    );
  }

1002
1003
1004
  /**
   * Processes the gathered schema for a base table.
   *
1005
1006
   * @param \Drupal\Core\Entity\ContentEntityTypeInterface $entity_type
   *   The entity type.
1007
1008
1009
1010
1011
1012
   * @param array $schema
   *   The table schema, passed by reference.
   *
   * @return array
   *   A partial schema array for the base table.
   */
1013
1014
  protected function processBaseTable(ContentEntityTypeInterface $entity_type, array &$schema) {
    $this->processIdentifierSchema($schema, $entity_type->getKey('id'));
1015
1016
1017
1018
1019
  }

  /**
   * Processes the gathered schema for a base table.
   *
1020
1021
   * @param \Drupal\Core\Entity\ContentEntityTypeInterface $entity_type
   *   The entity type.
1022
1023
1024
1025
1026
1027
   * @param array $schema
   *   The table schema, passed by reference.
   *
   * @return array
   *   A partial schema array for the base table.
   */
1028
1029
  protected function processRevisionTable(ContentEntityTypeInterface $entity_type, array &$schema) {
    $this->processIdentifierSchema($schema, $entity_type->getKey('revision'));
1030
1031
1032
1033
1034
  }

  /**
   * Processes the gathered schema for a base table.
   *
1035
1036
   * @param \Drupal\Core\Entity\ContentEntityTypeInterface $entity_type
   *   The entity type.
1037
1038
1039
1040
1041
1042
   * @param array $schema
   *   The table schema, passed by reference.
   *
   * @return array
   *   A partial schema array for the base table.
   */
1043
  protected function processDataTable(ContentEntityTypeInterface $entity_type, array &$schema) {
1044
1045
1046
    // Marking the respective fields as NOT NULL makes the indexes more
    // performant.
    $schema['fields'][$entity_type->getKey('default_langcode')]['not null'] = TRUE;
1047
1048
1049
1050
1051
  }

  /**
   * Processes the gathered schema for a base table.
   *
1052
1053
   * @param \Drupal\Core\Entity\ContentEntityTypeInterface $entity_type
   *   The entity type.
1054
1055
1056
1057
1058
1059
   * @param array $schema
   *   The table schema, passed by reference.
   *
   * @return array
   *   A partial schema array for the base table.
   */
1060
  protected function processRevisionDataTable(ContentEntityTypeInterface $entity_type, array &$schema) {
1061
1062
1063
    // Marking the respective fields as NOT NULL makes the indexes more
    // performant.
    $schema['fields'][$entity_type->getKey('default_langcode')]['not null'] = TRUE;
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
  }

  /**
   * Processes the specified entity key.
   *
   * @param array $schema
   *   The table schema, passed by reference.
   * @param string $key
   *   The entity key name.
   */
  protected function processIdentifierSchema(&$schema, $key) {
    if ($schema['fields'][$key]['type'] == 'int') {
      $schema['fields'][$key]['type'] = 'serial';
    }
1078
    $schema['fields'][$key]['not null'] = TRUE;
1079
1080
1081
    unset($schema['fields'][$key]['default']);
  }

1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
  /**
   * Performs the specified operation on a field.
   *
   * This figures out whether the field is stored in a dedicated or shared table
   * and forwards the call to the proper handler.
   *
   * @param string $operation
   *   The name of the operation to be performed.
   * @param \Drupal\Core\Field\FieldStorageDefinitionInterface $storage_definition
   *   The field storage definition.
   * @param \Drupal\Core\Field\FieldStorageDefinitionInterface $original
   *   (optional) The original field storage definition. This is relevant (and
   *   required) only for updates. Defaults to NULL.
   */
  protected function performFieldSchemaOperation($operation, FieldStorageDefinitionInterface $storage_definition, FieldStorageDefinitionInterface $original = NULL) {
    $table_mapping = $this->storage->getTableMapping();
    if ($table_mapping->requiresDedicatedTableStorage($storage_definition)) {
      $this->{$operation . 'DedicatedTableSchema'}($storage_definition, $original);
    }
    elseif ($table_mapping->allowsSharedTableStorage($storage_definition)) {
      $this->{$operation . 'SharedTableSchema'}($storage_definition, $original);
    }
  }

  /**
   * Creates the schema for a field stored in a dedicated table.
   *
   * @param \Drupal\Core\Field\FieldStorageDefinitionInterface $storage_definition
   *   The storage definition of the field being created.
   */
  protected function createDedicatedTableSchema(FieldStorageDefinitionInterface $storage_definition) {
    $schema = $this->getDedicatedTableSchema($storage_definition);
    foreach ($schema as $name => $table) {
1115
1116
1117
1118
1119
      // Check if the table exists because it might already have been
      // created as part of the earlier entity type update event.
      if (!$this->database->schema()->tableExists($name)) {
        $this->database->schema()->createTable($name, $table);
      }
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
    }
    $this->saveFieldSchemaData($storage_definition, $schema);
  }

  /**
   * Creates the schema for a field stored in a shared table.
   *
   * @param \Drupal\Core\Field\FieldStorageDefinitionInterface $storage_definition
   *   The storage definition of the field being created.
   * @param bool $only_save
   *   (optional) Whether to skip modification of database tables and only save
   *   the schema data for future comparison. For internal use only. This is
   *   used by onEntityTypeCreate() after it has already fully created the
   *   shared tables.
   */
  protected function createSharedTableSchema(FieldStorageDefinitionInterface $storage_definition, $only_save = FALSE) {
    $created_field_name = $storage_definition->getName();
    $table_mapping = $this->storage->getTableMapping();
    $column_names = $table_mapping->getColumnNames($created_field_name);
    $schema_handler = $this->database->schema();
    $shared_table_names = array_diff($table_mapping->getTableNames(), $table_mapping->getDedicatedTableNames());

    // Iterate over the mapped table to find the ones that will host the created
    // field schema.
    $schema = array();
    foreach ($shared_table_names as $table_name) {
      foreach ($table_mapping->getFieldNames($table_name) as $field_name) {
        if ($field_name == $created_field_name) {
          // Create field columns.
          $schema[$table_name] = $this->getSharedTableFieldSchema($storage_definition, $table_name, $column_names);
          if (!$only_save) {
            foreach ($schema[$table_name]['fields'] as $name => $specifier) {
1152
1153
1154
1155
1156
              // 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);
              }
1157
1158
1159
            }
            if (!empty($schema[$table_name]['indexes'])) {
              foreach ($schema[$table_name]['indexes'] as $name => $specifier) {
1160
1161
1162
                // Check if the index exists because it might already have been
                // created as part of the earlier entity type update event.
                if (!$schema_handler->indexExists($table_name, $name)) {
1163
                  $schema_handler->addIndex($table_name, $name, $specifier, $schema[$table_name]);
1164
                }
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
              }
            }
            if (!empty($schema[$table_name]['unique keys'])) {
              foreach ($schema[$table_name]['unique keys'] as $name => $specifier) {
                $schema_handler->addUniqueKey($table_name, $name, $specifier);
              }
            }
          }
          // After creating the field schema skip to the next table.
          break;
        }
      }
    }
    $this->saveFieldSchemaData($storage_definition, $schema);
  }

  /**
   * Deletes the schema for a field stored in a dedicated table.
   *
   * @param \Drupal\Core\Field\FieldStorageDefinitionInterface $storage_definition
   *   The storage definition of the field being deleted.
   */
  protected function deleteDedicatedTableSchema(FieldStorageDefinitionInterface $storage_definition) {
    // When switching from dedicated to shared field table layout we need need
    // to delete the field tables with their regular names. When this happens
    // original definitions will be defined.
    $deleted = !$this->originalDefinitions;
    $table_mapping = $this->storage->getTableMapping();
    $table_name = $table_mapping->getDedicatedDataTableName($storage_definition, $deleted);
    $this->database->schema()->dropTable($table_name);
    if ($this->entityType->isRevisionable()) {
      $revision_name = $table_mapping->getDedicatedRevisionTableName($storage_definition, $deleted);
      $this->database->schema()->dropTable($revision_name);
    }
    $this->deleteFieldSchemaData($storage_definition);
  }

  /**
   * Deletes the schema for a field stored in a shared table.
   *
   * @param \Drupal\Core\Field\FieldStorageDefinitionInterface $storage_definition
   *   The storage definition of the field being deleted.
   */
  protected function deleteSharedTableSchema(FieldStorageDefinitionInterface $storage_definition) {
    $deleted_field_name = $storage_definition->getName();
    $table_mapping = $this->storage->getTableMapping(
      $this->entityManager->getLastInstalledFieldStorageDefinitions($this->entityType->id())
    );
    $column_names = $table_mapping->getColumnNames($deleted_field_name);
    $schema_handler = $this->database->schema();
    $shared_table_names = array_diff($table_mapping->getTableNames(), $table_mapping->getDedicatedTableNames());

    // Iterate over the mapped table to find the ones that host the deleted
    // field schema.
    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);

          // Drop indexes and unique keys first.
          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);
            }
          }
          // Drop columns.
          foreach ($column_names as $column_name) {
            $schema_handler->dropField($table_name, $column_name);
          }
          // After deleting the field schema skip to the next table.
          break;
        }
      }
    }

    $this->deleteFieldSchemaData($storage_definition);
  }

  /**
   * Updates the schema for a field stored in a shared table.
   *
   * @param \Drupal\Core\Field\FieldStorageDefinitionInterface $storage_definition
   *   The storage definition of the field being updated.
   * @param \Drupal\Core\Field\FieldStorageDefinitionInterface $original
   *   The original storage definition; i.e., the definition before the update.
   *
   * @throws \Drupal\Core\Entity\Exception\FieldStorageDefinitionUpdateForbiddenException
   *   Thrown when the update to the field is forbidden.
   * @throws \Exception
   *   Rethrown exception if the table recreation fails.
   */
  protected function updateDedicatedTableSchema(FieldStorageDefinitionInterface $storage_definition, FieldStorageDefinitionInterface $original) {
    if (!$this->storage->countFieldData($original, TRUE)) {
      // There is no data. Re-create the tables completely.
      if ($this->database->supportsTransactionalDDL()) {
        // If the database supports transactional DDL, we can go ahead and rely
        // on it. If not, we will have to rollback manually if something fails.
        $transaction = $this->database->startTransaction();
      }
      try {
        // Since there is no data we may be switching from a shared table schema
        // to a dedicated table schema, hence we should use the proper API.
        $this->performFieldSchemaOperation('delete', $original);
        $this->performFieldSchemaOperation('create', $storage_definition);
      }
      catch (\Exception $e) {
        if ($this->database->supportsTransactionalDDL()) {
          $transaction->rollback();
        }
        else {
          // Recreate tables.
          $this->performFieldSchemaOperation('create', $original);
        }
        throw $e;
      }
    }
    else {
1287
      if ($this->hasColumnChanges($storage_definition, $original)) {
1288
        throw new FieldStorageDefinitionUpdateForbiddenException('The SQL storage cannot change the schema for an existing field (' . $storage_definition->getName() . ' in ' . $storage_definition->getTargetEntityTypeId() . ' entity) with data.');
1289
1290
1291
1292
1293
1294
1295
1296
      }
      // There is data, so there are no column changes. Drop all the prior
      // indexes and create all the new ones, except for all the priors that
      // exist unchanged.
      $table_mapping = $this->storage->getTableMapping();
      $table = $table_mapping->getDedicatedDataTableName($original);
      $revision_table = $table_mapping->getDedicatedRevisionTableName($original);

1297
      // Get the field schemas.
1298
1299
1300
      $schema = $storage_definition->getSchema();
      $original_schema = $original->getSchema();

1301
1302
1303
      // Gets the SQL schema for a dedicated tables.
      $actual_schema = $this->getDedicatedTableSchema($storage_definition);

1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
      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 = array();
          foreach ($columns as $column_name) {
            // Indexes 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)) {
              $real_columns[] = array(
                $table_mapping->getFieldColumnName($storage_definition, $column_name[0]),
                $column_name[1],
              );
            }
            else {
              $real_columns[] = $table_mapping->getFieldColumnName($storage_definition, $column_name);
            }
          }
1330
1331
          $this->database->schema()->addIndex($table, $real_name, $real_columns, $actual_schema[$table]);
          $this->database->schema()->addIndex($revision_table, $real_name, $real_columns, $actual_schema[$revision_table]);
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
        }
      }
      $this->saveFieldSchemaData($storage_definition, $this->getDedicatedTableSchema($storage_definition));
    }
  }

  /**
   * Updates the schema for a field stored in a shared table.
   *
   * @param \Drupal\Core\Field\FieldStorageDefinitionInterface $storage_definition
   *   The storage definition of the field being updated.
   * @param \Drupal\Core\Field\FieldStorageDefinitionInterface $original
   *   The original storage definition; i.e., the definition before the update.
   *
   * @throws \Drupal\Core\Entity\Exception\FieldStorageDefinitionUpdateForbiddenException
   *   Thrown when the update to the field is forbidden.
   * @throws \Exception
   *   Rethrown exception if the table recreation fails.
   */
  protected function updateSharedTableSchema(FieldStorageDefinitionInterface $storage_definition, FieldStorageDefinitionInterface $original) {
    if (!$this->storage->countFieldData($original, TRUE)) {
      if ($this->database->supportsTransactionalDDL()) {
        // If the database supports transactional DDL, we can go ahead and rely
        // on it. If not, we will have to rollback manually if something fails.
        $transaction = $this->database->startTransaction();
      }
      try {
        // Since there is no data we may be switching from a dedicated table
        // to a schema table schema, hence we should use the proper API.
        $this->performFieldSchemaOperation('delete', $original);
        $this->performFieldSchemaOperation('create', $storage_definition);
      }
      catch (\Exception $e) {
        if ($this->database->supportsTransactionalDDL()) {
          $transaction->rollback();
        }
        else {
          // Recreate original schema.
          $this->createSharedTableSchema($original);
        }
        throw $e;
      }
    }
    else {
1376
      if ($this->hasColumnChanges($storage_definition, $original)) {
1377
        throw new FieldStorageDefinitionUpdateForbiddenException('The SQL storage cannot change the schema for an existing field (' . $storage_definition->getName() . ' in ' . $storage_definition->getTargetEntityTypeId() . ' entity) with data.');
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
      }

      $updated_field_name = $storage_definition->getName();
      $table_mapping = $this->storage->getTableMapping();
      $column_names = $table_mapping->getColumnNames($updated_field_name);
      $schema_handler = $this->database->schema();

      // Iterate over the mapped table to find the ones that host the deleted
      // field schema.
      $original_schema = $this->loadFieldSchemaData($original);
      $schema = array();
      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);

1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
            // Handle NOT NULL constraints.
            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, $field_name, $field_name, $column_schema);
              }
            }

1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
            // Drop original indexes and unique keys.
            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);
              }
            }
            // Create new indexes and unique keys.
            if (!empty($schema[$table_name]['indexes'])) {
              foreach ($schema[$table_name]['indexes'] as $name => $specifier) {
1422
                $schema_handler->addIndex($table_name, $name, $specifier, $schema[$table_name]);
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
              }
            }
            if (!empty($schema[$table_name]['unique keys'])) {
              foreach ($schema[$table_name]['unique keys'] as $name => $specifier) {
                $schema_handler->addUniqueKey($table_name, $name, $specifier);
              }
            }
            // After deleting the field schema skip to the next table.
            break;
          }
        }
      }
      $this->saveFieldSchemaData($storage_definition, $schema);
    }
  }

1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
  /**
   * Checks whether a field property has NULL values.
   *
   * @param string $table_name
   *   The name of the table to inspect.
   * @param string $column_name
   *   The name of the column holding the field property data.
   *
   * @return bool
   *   TRUE if NULL data is found, FALSE otherwise.
   */
  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;
  }

1459
  /**
1460
   * Gets the schema for a single field definition.
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
   *
   * Entity types may override this method in order to optimize the generated
   * schema for given field. While all optimizations that apply to a single
   * field have to be added here, all cross-field optimizations should be via
   * SqlContentEntityStorageSchema::getEntitySchema() instead; e.g.,
   * an index spanning multiple fields.
   *
   * @param \Drupal\Core\Field\FieldStorageDefinitionInterface $storage_definition
   *   The storage definition of the field whose schema has to be returned.
   * @param string $table_name
   *   The name of the table columns will be added to.
   * @param string[] $column_mapping
   *   A mapping of field column names to database column names.
   *
   * @return array
   *   The schema definition for the table with the following keys:
   *   - fields: The schema definition for the each field columns.
   *   - indexes: The schema definition for the indexes.
   *   - unique keys: The schema definition for the unique keys.
   *   - foreign keys: The schema definition for the foreign keys.
   *
   * @throws \Drupal\Core\Field\FieldException
   *   Exception thrown if the schema contains reserved column names.
   */
  protected function getSharedTableFieldSchema(FieldStorageDefinitionInterface $storage_definition, $table_name, array $column_mapping) {
    $schema = array();
    $field_schema = $storage_definition->getSchema();

    // Check that the schema does not include forbidden column names.
    if (array_intersect(array_keys($field_schema['columns']), $this->storage->getTableMapping()->getReservedColumns())) {
1491
      throw new FieldException("Illegal field column names on {$storage_definition->getName()}");
1492
1493
1494
    }

    $field_name = $storage_definition->getName();
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
    $base_table = $this->storage->getBaseTable();

    // A shared table contains rows for entities where the field is empty
    // (since other fields stored in the same table might not be empty), thus
    // the only columns that can be 'not null' are those for required
    // properties of required fields. However, even those would break in the
    // case where a new field is added to a table that contains existing rows.
    // For now, we only hardcode 'not null' to a couple "entity keys", in order
    // to keep their indexes optimized.
    // @todo Revisit once we have support for 'initial' in
    //   https://www.drupal.org/node/2346019.
    $not_null_keys = $this->entityType->getKeys();
    // Label fields are not necessarily required.
    unset($not_null_keys['label']);
    // Because entity ID and revision ID are both serial fields in the base and
    // revision table respectively, the revision ID is not known yet, when
    // inserting data into the base table. Instead the revision ID in the base
    // table is updated after the data has been inserted into the revision
    // table. For this reason the revision ID field cannot be marked as NOT
    // NULL.
    if ($table_name == $base_table) {
      unset($not_null_keys['revision']);
    }
1518
1519
1520
1521
1522

    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;
1523
      $schema['fields'][$schema_field_name]['not null'] = in_array($field_name, $not_null_keys);
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
    }

    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);
    }

    return $schema;
  }

  /**
   * Adds an index for the specified field to the given schema definition.
   *
   * @param \Drupal\Core\Field\FieldStorageDefinitionInterface $storage_definition
   *   The storage definition of the field for which an index should be added.
   * @param array $schema
   *   A reference to the schema array to be updated.
   * @param bool $not_null
   *   (optional) Whether to also add a 'not null' constraint to the column
   *   being indexed. Doing so improves index performance. Defaults to FALSE,
   *   in case the field needs to support NULL values.
   * @param int $size
   *   (optional) The index size. Defaults to no limit.
   */
  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] = array($size ? array($name, $size) : $name);
    if ($not_null) {
      $schema['fields'][$name]['not null'] = TRUE;
    }
  }

  /**
   * Adds a unique key for the specified field to the given schema definition.
   *
   * Also adds a 'not null' constraint, because many databases do not reliably
   * support unique keys on null columns.
   *
   * @param \Drupal\Core\Field\FieldStorageDefinitionInterface $storage_definition
   *   The storage definition of the field to which to add a unique key.
   * @param array $schema
   *   A reference to the schema array to be updated.
   */
  protected function addSharedTableFieldUniqueKey(FieldStorageDefinitionInterface $storage_definition, &$schema) {
    $name = $storage_definition->getName();
    $real_key = $this->getFieldSchemaIdentifierName($storage_definition->getTargetEntityTypeId(), $name);
    $schema['unique keys'][$real_key] = array($name);
    $schema['fields'][$name]['not null'] = TRUE;
  }

  /**
   * Adds a foreign key for the specified field to the given schema definition.
   *
   * @param \Drupal\Core\Field\FieldStorageDefinitionInterface $storage_definition
   *   The storage definition of the field to which to add a foreign key.
   * @param array $schema
   *   A reference to the schema array to be updated.
   * @param string $foreign_table
   *   The foreign table.
   * @param string $foreign_column
   *   The foreign column.
   */
  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] = array(
      'table' => $foreign_table,
      'columns' => array($name => $foreign_column),
    );
  }

  /**
1604
   * Gets the SQL schema for a dedicated table.
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
   *
   * @param \Drupal\Core\Field\FieldStorageDefinitionInterface $storage_definition
   *   The field storage definition.
   * @param \Drupal\Core\Entity\ContentEntityTypeInterface $entity_type
   *   (optional) The entity type definition. Defaults to the one returned by
   *   the entity manager.
   *
   * @return array
   *   The schema definition for the table with the following keys:
   *   - fields: The schema definition for the each field columns.
   *   - indexes: The schema definition for the indexes.
   *   - unique keys: The schema definition for the unique keys.
   *   - foreign keys: The schema definition for the foreign keys.
   *
   * @throws \Drupal\Core\Field\FieldException
   *   Exception thrown if the schema contains reserved column names.
   *
   * @see hook_schema()
   */
  protected function getDedicatedTableSchema(FieldStorageDefinitionInterface $storage_definition, ContentEntityTypeInterface $entity_type = NULL) {
    $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[$this->entityType->getKey('id')];
    if ($id_definition->getType() == 'integer') {
      $id_schema = array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'description' => 'The entity id this data is attached to',
      );
    }
    else {
      $id_schema = array(
1639
        'type' => 'varchar_ascii',
1640
1641
1642
1643