Commit bae3e31e authored by alexpott's avatar alexpott
Browse files

Issue #2753475 by amateescu, timmillwood: Support using the values of another...

Issue #2753475 by amateescu, timmillwood: Support using the values of another field as initial values when adding a new field
parent b85b73c2
...@@ -411,6 +411,11 @@ public function addField($table, $field, $spec, $keys_new = array()) { ...@@ -411,6 +411,11 @@ public function addField($table, $field, $spec, $keys_new = array()) {
->fields(array($field => $spec['initial'])) ->fields(array($field => $spec['initial']))
->execute(); ->execute();
} }
if (isset($spec['initial_from_field'])) {
$this->connection->update($table)
->expression($field, $spec['initial_from_field'])
->execute();
}
if ($fixnull) { if ($fixnull) {
$spec['not null'] = TRUE; $spec['not null'] = TRUE;
$this->changeField($table, $field, $field, $spec); $this->changeField($table, $field, $field, $spec);
......
...@@ -531,6 +531,11 @@ public function addField($table, $field, $spec, $new_keys = array()) { ...@@ -531,6 +531,11 @@ public function addField($table, $field, $spec, $new_keys = array()) {
->fields(array($field => $spec['initial'])) ->fields(array($field => $spec['initial']))
->execute(); ->execute();
} }
if (isset($spec['initial_from_field'])) {
$this->connection->update($table)
->expression($field, $spec['initial_from_field'])
->execute();
}
if ($fixnull) { if ($fixnull) {
$this->connection->query("ALTER TABLE {" . $table . "} ALTER $field SET NOT NULL"); $this->connection->query("ALTER TABLE {" . $table . "} ALTER $field SET NOT NULL");
} }
......
...@@ -316,6 +316,11 @@ public function addField($table, $field, $specification, $keys_new = array()) { ...@@ -316,6 +316,11 @@ public function addField($table, $field, $specification, $keys_new = array()) {
->fields(array($field => $specification['initial'])) ->fields(array($field => $specification['initial']))
->execute(); ->execute();
} }
if (isset($specification['initial_from_field'])) {
$this->connection->update($table)
->expression($field, $specification['initial_from_field'])
->execute();
}
} }
else { else {
// We cannot add the field directly. Use the slower table alteration // We cannot add the field directly. Use the slower table alteration
...@@ -335,6 +340,13 @@ public function addField($table, $field, $specification, $keys_new = array()) { ...@@ -335,6 +340,13 @@ public function addField($table, $field, $specification, $keys_new = array()) {
'arguments' => array(':newfieldinitial' => $specification['initial']), 'arguments' => array(':newfieldinitial' => $specification['initial']),
); );
} }
elseif (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 {
// Else use the default of the field. // Else use the default of the field.
$mapping[$field] = NULL; $mapping[$field] = NULL;
......
...@@ -305,6 +305,8 @@ abstract public function dropTable($table); ...@@ -305,6 +305,8 @@ abstract public function dropTable($table);
* created field will be set to the value of the key in all rows. * created field will be set to the value of the key in all rows.
* This is most useful for creating NOT NULL columns with no default * This is most useful for creating NOT NULL columns with no default
* value in existing tables. * value in existing tables.
* Alternatively, the 'initial_form_field' key may be used, which will
* auto-populate the new field with values from the specified field.
* @param $keys_new * @param $keys_new
* (optional) Keys and indexes specification to be created on the * (optional) Keys and indexes specification to be created on the
* table along with adding the field. The format is the same as a * table along with adding the field. The format is the same as a
......
...@@ -505,6 +505,7 @@ function testSchemaAddField() { ...@@ -505,6 +505,7 @@ function testSchemaAddField() {
array('not null' => FALSE, 'default' => 7), array('not null' => FALSE, 'default' => 7),
array('not null' => TRUE, 'initial' => 1), array('not null' => TRUE, 'initial' => 1),
array('not null' => TRUE, 'initial' => 1, 'default' => 7), array('not null' => TRUE, 'initial' => 1, 'default' => 7),
array('not null' => TRUE, 'initial_from_field' => 'serial_column'),
); );
foreach ($variations as $variation) { foreach ($variations as $variation) {
...@@ -532,6 +533,7 @@ function testSchemaAddField() { ...@@ -532,6 +533,7 @@ function testSchemaAddField() {
array('not null' => FALSE, 'default' => 7), array('not null' => FALSE, 'default' => 7),
array('not null' => TRUE, 'initial' => 1), array('not null' => TRUE, 'initial' => 1),
array('not null' => TRUE, 'initial' => 1, 'default' => 7), array('not null' => TRUE, 'initial' => 1, 'default' => 7),
array('not null' => TRUE, 'initial_from_field' => 'serial_column'),
); );
foreach ($variations as $variation) { foreach ($variations as $variation) {
...@@ -620,6 +622,19 @@ protected function assertFieldCharacteristics($table_name, $field_name, $field_s ...@@ -620,6 +622,19 @@ protected function assertFieldCharacteristics($table_name, $field_name, $field_s
$this->assertEqual($count, 0, 'Initial values filled out.'); $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. // Check that the default value has been registered.
if (isset($field_spec['default'])) { if (isset($field_spec['default'])) {
// Try inserting a row, and check the resulting value of the new column. // Try inserting a row, and check the resulting value of the new column.
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment