Verified Commit fdfb3c66 authored by Alex Pott's avatar Alex Pott
Browse files

Issue #3406172 by mandclu: Provide a flag to allow updates to stored schema for fields

parent e0644f92
Loading
Loading
Loading
Loading
Loading
+2 −2
Original line number Diff line number Diff line
@@ -1746,7 +1746,7 @@ protected function updateDedicatedTableSchema(FieldStorageDefinitionInterface $s
      }
    }
    else {
      if ($this->hasColumnChanges($storage_definition, $original)) {
      if (empty($storage_definition->getSetting('column_changes_handled')) && $this->hasColumnChanges($storage_definition, $original)) {
        throw new FieldStorageDefinitionUpdateForbiddenException('The SQL storage cannot change the schema for an existing field (' . $storage_definition->getName() . ' in ' . $storage_definition->getTargetEntityTypeId() . ' entity) with data.');
      }
      // There is data, so there are no column changes. Drop all the prior
@@ -1839,7 +1839,7 @@ protected function updateSharedTableSchema(FieldStorageDefinitionInterface $stor
      }
    }
    else {
      if ($this->hasColumnChanges($storage_definition, $original)) {
      if (empty($storage_definition->getSetting('column_changes_handled')) && $this->hasColumnChanges($storage_definition, $original)) {
        throw new FieldStorageDefinitionUpdateForbiddenException('The SQL storage cannot change the schema for an existing field (' . $storage_definition->getName() . ' in ' . $storage_definition->getTargetEntityTypeId() . ' entity) with data.');
      }

+18 −0
Original line number Diff line number Diff line
@@ -112,4 +112,22 @@ public function testColumnUpdate() {
    $entity_definition_update_manager->updateFieldStorageDefinition($field_storage_definition);
  }

  /**
   * Tests that schema changes are updated for fields with data with the flag.
   */
  public function testColumnUpdateWithFlag() {
    // Change the field type in the stored schema.
    $schema = \Drupal::keyValue('entity.storage_schema.sql')->get('entity_test_rev.field_schema_data.test');
    $schema['entity_test_rev__test']['fields']['test_value']['type'] = 'varchar_ascii';
    \Drupal::keyValue('entity.storage_schema.sql')->set('entity_test_rev.field_schema_data.test', $schema);

    // Now attempt to run automatic updates. It should succeed if the
    // column_changes_handled flag is passed.
    $entity_definition_update_manager = \Drupal::entityDefinitionUpdateManager();
    $field_storage_definition = $entity_definition_update_manager->getFieldStorageDefinition('test', 'entity_test_rev');
    // Provide the flag to allow schema updates.
    $field_storage_definition->setSetting('column_changes_handled', TRUE);
    $entity_definition_update_manager->updateFieldStorageDefinition($field_storage_definition);
  }

}