diff --git a/core/modules/field/field.install b/core/modules/field/field.install index ecd2b3150bd05eabbb112c670a3f00881509cfd9..80639a0042a7f9f202a4ab24b4d628f0b21eeaa7 100644 --- a/core/modules/field/field.install +++ b/core/modules/field/field.install @@ -6,6 +6,8 @@ */ use Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem; +use Drupal\field\Entity\FieldConfig; +use Drupal\field\Entity\FieldStorageConfig; /** * Removes the stale 'target_bundle' storage setting on entity_reference fields. @@ -104,3 +106,30 @@ function field_update_8003() { } } } + +/** + * Update the definition of deleted fields. + */ +function field_update_8500() { + $state = \Drupal::state(); + + // Convert the old deleted field definitions from an array to a FieldConfig + // object. + $deleted_field_definitions = $state->get('field.field.deleted', []); + foreach ($deleted_field_definitions as $key => $deleted_field_definition) { + if (is_array($deleted_field_definition)) { + $deleted_field_definitions[$key] = new FieldConfig($deleted_field_definition); + } + } + $state->set('field.field.deleted', $deleted_field_definitions); + + // Convert the old deleted field storage definitions from an array to a + // FieldStorageConfig object. + $deleted_field_storage_definitions = $state->get('field.storage.deleted', []); + foreach ($deleted_field_storage_definitions as $key => $deleted_field_storage_definition) { + if (is_array($deleted_field_storage_definition)) { + $deleted_field_storage_definitions[$key] = new FieldStorageConfig($deleted_field_storage_definition); + } + } + $state->set('field.storage.deleted', $deleted_field_storage_definitions); +} diff --git a/core/modules/field/tests/fixtures/update/drupal-8.update_deleted_field_definitions-2931436.php b/core/modules/field/tests/fixtures/update/drupal-8.update_deleted_field_definitions-2931436.php new file mode 100644 index 0000000000000000000000000000000000000000..9a210e0bfec9f44d9a2608d873bdadc86c9e5204 --- /dev/null +++ b/core/modules/field/tests/fixtures/update/drupal-8.update_deleted_field_definitions-2931436.php @@ -0,0 +1,203 @@ +<?php +// @codingStandardsIgnoreFile + +/** + * @file + * Contains SQL necessary to add a deleted field to the node entity type. + */ + +use Drupal\Core\Database\Database; + +$connection = Database::getConnection(); + +// Add the field schema data and the deleted field definitions. +$connection->insert('key_value') + ->fields([ + 'collection', + 'name', + 'value', + ]) + ->values([ + 'collection' => 'entity.storage_schema.sql', + 'name' => 'node.field_schema_data.field_test', + 'value' => 'a:2:{s:16:"node__field_test";a:4:{s:11:"description";s:39:"Data storage for node field field_test.";s:6:"fields";a:7:{s:6:"bundle";a:5:{s:4:"type";s:13:"varchar_ascii";s:6:"length";i:128;s:8:"not null";b:1;s:7:"default";s:0:"";s:11:"description";s:88:"The field instance bundle to which this row belongs, used when deleting a field instance";}s:7:"deleted";a:5:{s:4:"type";s:3:"int";s:4:"size";s:4:"tiny";s:8:"not null";b:1;s:7:"default";i:0;s:11:"description";s:60:"A boolean indicating whether this data item has been deleted";}s:9:"entity_id";a:4:{s:4:"type";s:3:"int";s:8:"unsigned";b:1;s:8:"not null";b:1;s:11:"description";s:38:"The entity id this data is attached to";}s:11:"revision_id";a:4:{s:4:"type";s:3:"int";s:8:"unsigned";b:1;s:8:"not null";b:1;s:11:"description";s:47:"The entity revision id this data is attached to";}s:8:"langcode";a:5:{s:4:"type";s:13:"varchar_ascii";s:6:"length";i:32;s:8:"not null";b:1;s:7:"default";s:0:"";s:11:"description";s:37:"The language code for this data item.";}s:5:"delta";a:4:{s:4:"type";s:3:"int";s:8:"unsigned";b:1;s:8:"not null";b:1;s:11:"description";s:67:"The sequence number for this data item, used for multi-value fields";}s:16:"field_test_value";a:3:{s:4:"type";s:7:"varchar";s:6:"length";i:254;s:8:"not null";b:1;}}s:11:"primary key";a:4:{i:0;s:9:"entity_id";i:1;s:7:"deleted";i:2;s:5:"delta";i:3;s:8:"langcode";}s:7:"indexes";a:2:{s:6:"bundle";a:1:{i:0;s:6:"bundle";}s:11:"revision_id";a:1:{i:0;s:11:"revision_id";}}}s:25:"node_revision__field_test";a:4:{s:11:"description";s:51:"Revision archive storage for node field field_test.";s:6:"fields";a:7:{s:6:"bundle";a:5:{s:4:"type";s:13:"varchar_ascii";s:6:"length";i:128;s:8:"not null";b:1;s:7:"default";s:0:"";s:11:"description";s:88:"The field instance bundle to which this row belongs, used when deleting a field instance";}s:7:"deleted";a:5:{s:4:"type";s:3:"int";s:4:"size";s:4:"tiny";s:8:"not null";b:1;s:7:"default";i:0;s:11:"description";s:60:"A boolean indicating whether this data item has been deleted";}s:9:"entity_id";a:4:{s:4:"type";s:3:"int";s:8:"unsigned";b:1;s:8:"not null";b:1;s:11:"description";s:38:"The entity id this data is attached to";}s:11:"revision_id";a:4:{s:4:"type";s:3:"int";s:8:"unsigned";b:1;s:8:"not null";b:1;s:11:"description";s:47:"The entity revision id this data is attached to";}s:8:"langcode";a:5:{s:4:"type";s:13:"varchar_ascii";s:6:"length";i:32;s:8:"not null";b:1;s:7:"default";s:0:"";s:11:"description";s:37:"The language code for this data item.";}s:5:"delta";a:4:{s:4:"type";s:3:"int";s:8:"unsigned";b:1;s:8:"not null";b:1;s:11:"description";s:67:"The sequence number for this data item, used for multi-value fields";}s:16:"field_test_value";a:3:{s:4:"type";s:7:"varchar";s:6:"length";i:254;s:8:"not null";b:1;}}s:11:"primary key";a:5:{i:0;s:9:"entity_id";i:1;s:11:"revision_id";i:2;s:7:"deleted";i:3;s:5:"delta";i:4;s:8:"langcode";}s:7:"indexes";a:2:{s:6:"bundle";a:1:{i:0;s:6:"bundle";}s:11:"revision_id";a:1:{i:0;s:11:"revision_id";}}}}', + ]) + ->values([ + 'collection' => 'state', + 'name' => 'field.field.deleted', + 'value' => 'a:1:{s:36:"5d0d9870-560b-46c4-b838-0dcded0502dd";a:18:{s:4:"uuid";s:36:"5d0d9870-560b-46c4-b838-0dcded0502dd";s:8:"langcode";s:2:"en";s:6:"status";b:1;s:12:"dependencies";a:1:{s:6:"config";a:2:{i:0;s:29:"field.storage.node.field_test";i:1;s:17:"node.type.article";}}s:2:"id";s:23:"node.article.field_test";s:10:"field_name";s:10:"field_test";s:11:"entity_type";s:4:"node";s:6:"bundle";s:7:"article";s:5:"label";s:4:"Test";s:11:"description";s:0:"";s:8:"required";b:0;s:12:"translatable";b:0;s:13:"default_value";a:0:{}s:22:"default_value_callback";s:0:"";s:8:"settings";a:0:{}s:10:"field_type";s:5:"email";s:7:"deleted";b:1;s:18:"field_storage_uuid";s:36:"ce93d7c2-1da7-4a2c-9e6d-b4925e3b129f";}}', + ]) + ->values([ + 'collection' => 'state', + 'name' => 'field.storage.deleted', + 'value' => 'a:1:{s:36:"ce93d7c2-1da7-4a2c-9e6d-b4925e3b129f";a:18:{s:4:"uuid";s:36:"ce93d7c2-1da7-4a2c-9e6d-b4925e3b129f";s:8:"langcode";s:2:"en";s:6:"status";b:1;s:12:"dependencies";a:1:{s:6:"module";a:1:{i:0;s:4:"node";}}s:2:"id";s:15:"node.field_test";s:10:"field_name";s:10:"field_test";s:11:"entity_type";s:4:"node";s:4:"type";s:5:"email";s:8:"settings";a:0:{}s:6:"module";s:4:"core";s:6:"locked";b:0;s:11:"cardinality";i:1;s:12:"translatable";b:1;s:7:"indexes";a:0:{}s:22:"persist_with_no_fields";b:0;s:14:"custom_storage";b:0;s:7:"deleted";b:1;s:7:"bundles";a:0:{}}}', + ]) + ->execute(); + +// Create and populate the deleted field tables. +// @see \Drupal\Core\Entity\Sql\DefaultTableMapping::getDedicatedDataTableName() +$deleted_field_data_table_name = "field_deleted_data_" . substr(hash('sha256', 'ce93d7c2-1da7-4a2c-9e6d-b4925e3b129f'), 0, 10); +$connection->schema()->createTable($deleted_field_data_table_name, array( + 'fields' => array( + 'bundle' => array( + 'type' => 'varchar_ascii', + 'not null' => TRUE, + 'length' => '128', + 'default' => '', + ), + 'deleted' => array( + 'type' => 'int', + 'not null' => TRUE, + 'size' => 'tiny', + 'default' => '0', + ), + 'entity_id' => array( + 'type' => 'int', + 'not null' => TRUE, + 'size' => 'normal', + 'unsigned' => TRUE, + ), + 'revision_id' => array( + 'type' => 'int', + 'not null' => TRUE, + 'size' => 'normal', + 'unsigned' => TRUE, + ), + 'langcode' => array( + 'type' => 'varchar_ascii', + 'not null' => TRUE, + 'length' => '32', + 'default' => '', + ), + 'delta' => array( + 'type' => 'int', + 'not null' => TRUE, + 'size' => 'normal', + 'unsigned' => TRUE, + ), + 'field_test_value' => array( + 'type' => 'varchar', + 'not null' => TRUE, + 'length' => '254', + ), + ), + 'primary key' => array( + 'entity_id', + 'deleted', + 'delta', + 'langcode', + ), + 'indexes' => array( + 'bundle' => array( + 'bundle', + ), + 'revision_id' => array( + 'revision_id', + ), + ), +)); + +$connection->insert($deleted_field_data_table_name) +->fields(array( + 'bundle', + 'deleted', + 'entity_id', + 'revision_id', + 'langcode', + 'delta', + 'field_test_value', +)) +->values(array( + 'bundle' => 'article', + 'deleted' => '1', + 'entity_id' => '1', + 'revision_id' => '1', + 'langcode' => 'en', + 'delta' => '0', + 'field_test_value' => 'test@test.com', +)) +->execute(); + +// @see \Drupal\Core\Entity\Sql\DefaultTableMapping::getDedicatedDataTableName() +$deleted_field_revision_table_name = "field_deleted_revision_" . substr(hash('sha256', 'ce93d7c2-1da7-4a2c-9e6d-b4925e3b129f'), 0, 10); +$connection->schema()->createTable($deleted_field_revision_table_name, array( + 'fields' => array( + 'bundle' => array( + 'type' => 'varchar_ascii', + 'not null' => TRUE, + 'length' => '128', + 'default' => '', + ), + 'deleted' => array( + 'type' => 'int', + 'not null' => TRUE, + 'size' => 'tiny', + 'default' => '0', + ), + 'entity_id' => array( + 'type' => 'int', + 'not null' => TRUE, + 'size' => 'normal', + 'unsigned' => TRUE, + ), + 'revision_id' => array( + 'type' => 'int', + 'not null' => TRUE, + 'size' => 'normal', + 'unsigned' => TRUE, + ), + 'langcode' => array( + 'type' => 'varchar_ascii', + 'not null' => TRUE, + 'length' => '32', + 'default' => '', + ), + 'delta' => array( + 'type' => 'int', + 'not null' => TRUE, + 'size' => 'normal', + 'unsigned' => TRUE, + ), + 'field_test_value' => array( + 'type' => 'varchar', + 'not null' => TRUE, + 'length' => '254', + ), + ), + 'primary key' => array( + 'entity_id', + 'revision_id', + 'deleted', + 'delta', + 'langcode', + ), + 'indexes' => array( + 'bundle' => array( + 'bundle', + ), + 'revision_id' => array( + 'revision_id', + ), + ), +)); + +$connection->insert($deleted_field_revision_table_name) +->fields(array( + 'bundle', + 'deleted', + 'entity_id', + 'revision_id', + 'langcode', + 'delta', + 'field_test_value', +)) +->values(array( + 'bundle' => 'article', + 'deleted' => '1', + 'entity_id' => '1', + 'revision_id' => '1', + 'langcode' => 'en', + 'delta' => '0', + 'field_test_value' => 'test@test.com', +)) +->execute(); diff --git a/core/modules/field/tests/src/Functional/Update/FieldUpdateTest.php b/core/modules/field/tests/src/Functional/Update/FieldUpdateTest.php index b9175b127794a2d229732567c0299438a7f7befe..2791ef06460099ef76bb80e5b5825dc4611cb0e2 100644 --- a/core/modules/field/tests/src/Functional/Update/FieldUpdateTest.php +++ b/core/modules/field/tests/src/Functional/Update/FieldUpdateTest.php @@ -3,9 +3,12 @@ namespace Drupal\Tests\field\Functional\Update; use Drupal\Core\Config\Config; +use Drupal\Core\Field\FieldDefinitionInterface; +use Drupal\Core\Field\FieldStorageDefinitionInterface; use Drupal\field\Entity\FieldConfig; use Drupal\FunctionalTests\Update\UpdatePathTestBase; use Drupal\node\Entity\Node; +use Drupal\Tests\Traits\Core\CronRunTrait; /** * Tests that field settings are properly updated during database updates. @@ -14,6 +17,8 @@ */ class FieldUpdateTest extends UpdatePathTestBase { + use CronRunTrait; + /** * The config factory service. * @@ -21,12 +26,45 @@ class FieldUpdateTest extends UpdatePathTestBase { */ protected $configFactory; + /** + * The database connection. + * + * @var \Drupal\Core\Database\Connection + */ + protected $database; + + /** + * The key-value collection for tracking installed storage schema. + * + * @var \Drupal\Core\KeyValueStore\KeyValueStoreInterface + */ + protected $installedStorageSchema; + + /** + * The state service. + * + * @var \Drupal\Core\State\StateInterface + */ + protected $state; + + /** + * The deleted fields repository. + * + * @var \Drupal\Core\Field\DeletedFieldsRepositoryInterface + */ + protected $deletedFieldsRepository; + /** * {@inheritdoc} */ protected function setUp() { parent::setUp(); + $this->configFactory = $this->container->get('config.factory'); + $this->database = $this->container->get('database'); + $this->installedStorageSchema = $this->container->get('keyvalue')->get('entity.storage_schema.sql'); + $this->state = $this->container->get('state'); + $this->deletedFieldsRepository = $this->container->get('entity_field.deleted_fields_repository'); } /** @@ -37,6 +75,7 @@ protected function setDatabaseDumpFiles() { __DIR__ . '/../../../../../system/tests/fixtures/update/drupal-8.bare.standard.php.gz', __DIR__ . '/../../../fixtures/update/drupal-8.views_entity_reference_plugins-2429191.php', __DIR__ . '/../../../fixtures/update/drupal-8.remove_handler_submit_setting-2715589.php', + __DIR__ . '/../../../fixtures/update/drupal-8.update_deleted_field_definitions-2931436.php', ]; } @@ -127,6 +166,76 @@ public function testFieldUpdate8003() { $this->assertEqual($handler_settings['auto_create_bundle'], 'tags'); } + /** + * Tests field_update_8500(). + * + * @see field_update_8500() + */ + public function testFieldUpdate8500() { + $field_name = 'field_test'; + $field_uuid = '5d0d9870-560b-46c4-b838-0dcded0502dd'; + $field_storage_uuid = 'ce93d7c2-1da7-4a2c-9e6d-b4925e3b129f'; + + // Check that we have pre-existing entries for 'field.field.deleted' and + // 'field.storage.deleted'. + $deleted_fields = $this->state->get('field.field.deleted'); + $this->assertCount(1, $deleted_fields); + $this->assertArrayHasKey($field_uuid, $deleted_fields); + + $deleted_field_storages = $this->state->get('field.storage.deleted'); + $this->assertCount(1, $deleted_field_storages); + $this->assertArrayHasKey($field_storage_uuid, $deleted_field_storages); + + // Ensure that cron does not run automatically after running the updates. + $this->state->set('system.cron_last', REQUEST_TIME + 100); + + // Run updates. + $this->runUpdates(); + + // Now that we can use the API, check that the "delete fields" state entries + // have been converted to proper field definition objects. + $deleted_fields = $this->deletedFieldsRepository->getFieldDefinitions(); + + $this->assertCount(1, $deleted_fields); + $this->assertArrayHasKey($field_uuid, $deleted_fields); + $this->assertTrue($deleted_fields[$field_uuid] instanceof FieldDefinitionInterface); + $this->assertEquals($field_name, $deleted_fields[$field_uuid]->getName()); + + $deleted_field_storages = $this->deletedFieldsRepository->getFieldStorageDefinitions(); + $this->assertCount(1, $deleted_field_storages); + $this->assertArrayHasKey($field_storage_uuid, $deleted_field_storages); + $this->assertTrue($deleted_field_storages[$field_storage_uuid] instanceof FieldStorageDefinitionInterface); + $this->assertEquals($field_name, $deleted_field_storages[$field_storage_uuid]->getName()); + + // Check that the installed storage schema still exists. + $this->assertNotNull($this->installedStorageSchema->get("node.field_schema_data.$field_name")); + + // Check that the deleted field tables exist. + /** @var \Drupal\Core\Entity\Sql\DefaultTableMapping $table_mapping */ + $table_mapping = \Drupal::entityTypeManager()->getStorage('node')->getTableMapping(); + + $deleted_field_data_table_name = $table_mapping->getDedicatedDataTableName($deleted_field_storages[$field_storage_uuid], TRUE); + $this->assertTrue($this->database->schema()->tableExists($deleted_field_data_table_name)); + $deleted_field_revision_table_name = $table_mapping->getDedicatedRevisionTableName($deleted_field_storages[$field_storage_uuid], TRUE); + $this->assertTrue($this->database->schema()->tableExists($deleted_field_revision_table_name)); + + // Run cron and repeat the checks above. + $this->cronRun(); + + $deleted_fields = $this->deletedFieldsRepository->getFieldDefinitions(); + $this->assertCount(0, $deleted_fields); + + $deleted_field_storages = $this->deletedFieldsRepository->getFieldStorageDefinitions(); + $this->assertCount(0, $deleted_field_storages); + + // Check that the installed storage schema has been deleted. + $this->assertNull($this->installedStorageSchema->get("node.field_schema_data.$field_name")); + + // Check that the deleted field tables have been deleted. + $this->assertFalse($this->database->schema()->tableExists($deleted_field_data_table_name)); + $this->assertFalse($this->database->schema()->tableExists($deleted_field_revision_table_name)); + } + /** * Asserts that a config depends on 'entity_reference' or not *