diff --git a/core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorageSchema.php b/core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorageSchema.php index b11807b430..ecfa56409a 100644 --- a/core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorageSchema.php +++ b/core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorageSchema.php @@ -1368,6 +1368,17 @@ protected function createSharedTableSchema(FieldStorageDefinitionInterface $stor $new_keys = []; if (!empty($schema[$table_name]['primary key']) && in_array($name, $schema[$table_name]['primary key'], TRUE)) { $new_keys = ['primary key' => $schema[$table_name]['primary key']]; + + // If the field is part of a primary key, it might be a serial + // field. Serial fields are incorrectly stored as integer fields + // in the field schema, so we adapt the schema here manually. + // @todo Remove this in + // https://www.drupal.org/project/drupal/issues/2928906 + $base_table_serial = ($created_field_name === $this->entityType->getKey('id')) && ($table_name === $this->storage->getBaseTable()); + $revision_table_serial = ($created_field_name === $this->entityType->getKey('revision')) && ($table_name === $this->storage->getRevisionTable()); + if (($specifier['type'] === 'int') && ($base_table_serial || $revision_table_serial)) { + $specifier['type'] = 'serial'; + } } // Check if the field exists because it might already have been @@ -2432,11 +2443,13 @@ private function dropPrimaryKey(FieldStorageDefinitionInterface $storage_definit // schema, so we explicitly check the conditions for marking it serial. // @todo Remove this in // https://www.drupal.org/project/drupal/issues/2928906 - $base_table_serial = ($field_name === $this->entityType->getKey('id')) && ($table_name === $this->entityType->getBaseTable()); - $revision_table_serial = ($field_name === $this->entityType->getKey('revision')) && ($table_name === $this->entityType->getRevisionTable()); + $base_table_serial = ($field_name === $this->entityType->getKey('id')) && ($table_name === $this->storage->getBaseTable()); + $revision_table_serial = ($field_name === $this->entityType->getKey('revision')) && ($table_name === $this->storage->getRevisionTable()); if ($base_table_serial || $revision_table_serial) { foreach ($schema['primary key'] as $column_name) { - $schema_handler->changeField($table_name, $column_name, $column_name, $schema['fields'][$column_name]); + if ($schema['fields'][$column_name]['type'] === 'int') { + $schema_handler->changeField($table_name, $column_name, $column_name, $schema['fields'][$column_name]); + } } } $schema_handler->dropPrimaryKey($table_name); diff --git a/core/modules/system/tests/src/Functional/Entity/Update/SqlContentEntityStorageSchemaConverterTest.php b/core/modules/system/tests/src/Functional/Entity/Update/SqlContentEntityStorageSchemaConverterTest.php index 4b7fccbeb5..639be89565 100644 --- a/core/modules/system/tests/src/Functional/Entity/Update/SqlContentEntityStorageSchemaConverterTest.php +++ b/core/modules/system/tests/src/Functional/Entity/Update/SqlContentEntityStorageSchemaConverterTest.php @@ -220,15 +220,9 @@ public function testMakeRevisionableErrorHandling() { foreach ($new_storage_definitions as $storage_definition) { $new_field_schema_data[$storage_definition->getName()] = $this->installedStorageSchema->get('entity_test_update.field_schema_data.' . $storage_definition->getName(), []); } - // Before https://www.drupal.org/project/drupal/issues/2928906 the field - // schema data of integer identifier fields was incorrectly stored as 'int' - // instead of 'serial'. So we explicitly make that change before checking - // equality. - /* @see \Drupal\Core\Entity\Sql\SqlContentEntityStorageSchema::hasColumnChanges() */ - $this->assertSame($original_field_schema_data['id']['entity_test_update']['fields']['id']['type'], 'int'); - $original_field_schema_data['id']['entity_test_update']['fields']['id']['type'] = 'serial'; - // Also add keys that are stored in the field schema as of - // https://www.drupal.org/project/drupal/issues/2928906. + // Before https://www.drupal.org/project/drupal/issues/2929120 various keys + // were missing from the field schema, so we explicitly add those before + // checking equality. $this->assertFalse(isset($original_field_schema_data['id']['entity_test_update']['primary key'])); $original_field_schema_data['id']['entity_test_update']['primary key'] = ['id']; $this->assertFalse(isset($original_field_schema_data['id']['entity_test_update_data']['primary key']));