Commit 3dfb808a authored by webchick's avatar webchick

#373225 by Josh Waihi: Make PostgreSQL cast values when changing database...

#373225 by Josh Waihi: Make PostgreSQL cast values when changing database columns to prevent data loss.
parent 5f3033d7
......@@ -547,9 +547,20 @@ public function changeField(&$ret, $table, $field, $field_new, $spec, $new_keys
$not_null = isset($spec['not null']) ? $spec['not null'] : FALSE;
unset($spec['not null']);
if (!array_key_exists('size', $spec)) {
$spec['size'] = 'normal';
}
$this->addField($ret, $table, "$field_new", $spec);
$ret[] = update_sql("UPDATE {" . $table . "} SET $field_new = " . $field . "_old");
// We need to typecast the new column to best be able to transfer the data
// Schema_pgsql::getFieldTypeMap() will return possibilities that are not
// 'cast-able' such as 'serial' - so they need to be casted int instead.
$map = $this->getFieldTypeMap();
$typecast = $map[$spec['type'] . ':' . $spec['size']];
if (in_array($typecast, array('serial', 'bigserial', 'numeric'))) {
$typecast = 'int';
}
$ret[] = update_sql("UPDATE {" . $table . "} SET $field_new = CAST(" . $field . "_old as " . $typecast . ")");
if ($not_null) {
$ret[] = update_sql("ALTER TABLE {" . $table . "} ALTER $field_new SET NOT NULL");
......
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