diff --git a/core/lib/Drupal/Core/Database/Driver/mysql/Schema.php b/core/lib/Drupal/Core/Database/Driver/mysql/Schema.php index 107495b..6fd999e 100644 --- a/core/lib/Drupal/Core/Database/Driver/mysql/Schema.php +++ b/core/lib/Drupal/Core/Database/Driver/mysql/Schema.php @@ -411,6 +411,11 @@ public function addField($table, $field, $spec, $keys_new = array()) { ->fields(array($field => $spec['initial'])) ->execute(); } + if (isset($spec['initial_from_field'])) { + $this->connection->update($table) + ->expression($field, $spec['initial_from_field']) + ->execute(); + } if ($fixnull) { $spec['not null'] = TRUE; $this->changeField($table, $field, $field, $spec); diff --git a/core/lib/Drupal/Core/Database/Driver/pgsql/Schema.php b/core/lib/Drupal/Core/Database/Driver/pgsql/Schema.php index 4cd660b..c0775c9 100644 --- a/core/lib/Drupal/Core/Database/Driver/pgsql/Schema.php +++ b/core/lib/Drupal/Core/Database/Driver/pgsql/Schema.php @@ -531,6 +531,11 @@ public function addField($table, $field, $spec, $new_keys = array()) { ->fields(array($field => $spec['initial'])) ->execute(); } + if (isset($spec['initial_from_field'])) { + $this->connection->update($table) + ->expression($field, $spec['initial_from_field']) + ->execute(); + } if ($fixnull) { $this->connection->query("ALTER TABLE {" . $table . "} ALTER $field SET NOT NULL"); } diff --git a/core/lib/Drupal/Core/Database/Driver/sqlite/Schema.php b/core/lib/Drupal/Core/Database/Driver/sqlite/Schema.php index a3c001c..5120a058 100644 --- a/core/lib/Drupal/Core/Database/Driver/sqlite/Schema.php +++ b/core/lib/Drupal/Core/Database/Driver/sqlite/Schema.php @@ -316,6 +316,11 @@ public function addField($table, $field, $specification, $keys_new = array()) { ->fields(array($field => $specification['initial'])) ->execute(); } + if (isset($specification['initial_from_field'])) { + $this->connection->update($table) + ->expression($field, $specification['initial_from_field']) + ->execute(); + } } else { // We cannot add the field directly. Use the slower table alteration @@ -335,6 +340,13 @@ public function addField($table, $field, $specification, $keys_new = array()) { 'arguments' => array(':newfieldinitial' => $specification['initial']), ); } + if (isset($specification['initial_from_field'])) { + // If we have a initial value, copy it over. + $mapping[$field] = array( + 'expression' => $specification['initial_from_field'], + 'arguments' => [], + ); + } else { // Else use the default of the field. $mapping[$field] = NULL; diff --git a/core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorageSchema.php b/core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorageSchema.php index d76c132..eb0999c 100644 --- a/core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorageSchema.php +++ b/core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorageSchema.php @@ -1138,6 +1138,13 @@ protected function createSharedTableSchema(FieldStorageDefinitionInterface $stor $schema[$table_name] = $this->getSharedTableFieldSchema($storage_definition, $table_name, $column_names); if (!$only_save) { foreach ($schema[$table_name]['fields'] as $name => $specifier) { + // Use the value of the entity ID as the initial value for the + // revision ID field. + if ($created_field_name == $this->entityType->getKey('revision')) { + $id_column_name = $table_mapping->getColumnNames($this->entityType->getKey('id')); + $specifier['initial_from_field'] = reset($id_column_name); + } + // 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)) { diff --git a/core/tests/Drupal/KernelTests/Core/Database/SchemaTest.php b/core/tests/Drupal/KernelTests/Core/Database/SchemaTest.php index 7866146..c47e4e6 100644 --- a/core/tests/Drupal/KernelTests/Core/Database/SchemaTest.php +++ b/core/tests/Drupal/KernelTests/Core/Database/SchemaTest.php @@ -485,6 +485,7 @@ function testSchemaAddField() { array('not null' => TRUE, 'initial' => 'd'), array('not null' => FALSE, 'default' => NULL), array('not null' => TRUE, 'initial' => 'd', 'default' => '7'), + array('not null' => TRUE, 'initial_from_field' => 'serial_column'), ); foreach ($variations as $variation) { @@ -505,6 +506,7 @@ function testSchemaAddField() { array('not null' => FALSE, 'default' => 7), array('not null' => TRUE, 'initial' => 1), array('not null' => TRUE, 'initial' => 1, 'default' => 7), + array('not null' => TRUE, 'initial_from_field' => 'serial_column'), ); foreach ($variations as $variation) { @@ -532,6 +534,7 @@ function testSchemaAddField() { array('not null' => FALSE, 'default' => 7), array('not null' => TRUE, 'initial' => 1), array('not null' => TRUE, 'initial' => 1, 'default' => 7), + array('not null' => TRUE, 'initial_from_field' => 'serial_column'), ); foreach ($variations as $variation) { @@ -620,6 +623,19 @@ protected function assertFieldCharacteristics($table_name, $field_name, $field_s $this->assertEqual($count, 0, 'Initial values filled out.'); } + // Check that the initial value from another field has been registered. + if (isset($field_spec['initial_from_field'])) { + // There should be no row with a value different than + // $field_spec['initial_from_field']. + $count = db_select($table_name) + ->fields($table_name, array('serial_column')) + ->where($table_name . '.' . $field_spec['initial_from_field'] . ' <> ' . $table_name . '.' . $field_name) + ->countQuery() + ->execute() + ->fetchField(); + $this->assertEqual($count, 0, 'Initial values from another field filled out.'); + } + // Check that the default value has been registered. if (isset($field_spec['default'])) { // Try inserting a row, and check the resulting value of the new column.