summaryrefslogtreecommitdiff
path: root/includes/database/pgsql
diff options
context:
space:
mode:
authorAngie Byron <webchick@24967.no-reply.drupal.org>2010-09-01 01:28:41 +0000
committerAngie Byron <webchick@24967.no-reply.drupal.org>2010-09-01 01:28:41 +0000
commita405b10d681584039d8a9ca8a8ec8f23c6037118 (patch)
tree0f95f69514545d1a51d854414ca3d36d2ea8c51e /includes/database/pgsql
parent1ba166ff8e25f09035ffc90dc7bf095fa1dcd0cd (diff)
downloadbrdo-a405b10d681584039d8a9ca8a8ec8f23c6037118.tar.gz
brdo-a405b10d681584039d8a9ca8a8ec8f23c6037118.tar.bz2
#761976 by Berdir, jhedstrom, tpfeiffer, Damien Tournoud, Rob van den Bogaard, andypost: Fixed DatabaseSchema_pgsql()::changeField() is broken.
Diffstat (limited to 'includes/database/pgsql')
-rw-r--r--includes/database/pgsql/schema.inc29
1 files changed, 21 insertions, 8 deletions
diff --git a/includes/database/pgsql/schema.inc b/includes/database/pgsql/schema.inc
index afe3a5672..4c9ce4638 100644
--- a/includes/database/pgsql/schema.inc
+++ b/includes/database/pgsql/schema.inc
@@ -470,17 +470,30 @@ class DatabaseSchema_pgsql extends DatabaseSchema {
if (in_array($typecast, array('serial', 'bigserial', 'numeric'))) {
$typecast = 'int';
}
-
-
- $this->connection->query("ALTER TABLE {" . $table . "} ALTER $field SET $field_new = CAST(" . $field . "_old as " . $typecast . ")");
-
- $this->addField($table, "$field_new", $spec);
-
- $this->dropField($table, $field . '_old');
+ $this->connection->query('ALTER TABLE {' . $table . '} ALTER "' . $field . '" TYPE ' . $typecast . ' USING "' . $field . '"::' . $typecast);
+
+ // Map type definition to the PostgreSQL type.
+ $pgsql_type = $map[$spec['type'] . ':' . $spec['size']];
+ if (in_array($pgsql_type, array('serial', 'bigserial'))) {
+ // Type "serial" is known to PostgreSQL, but *only* during table creation,
+ // not when altering. Because of that, the sequence needs to be created
+ // and initialized by hand.
+ $seq = "{" . $table . "}_" . $field_new . "_seq";
+ $this->connection->query("CREATE SEQUENCE " . $seq);
+ // Set sequence to maximal field value to not conflict with existing
+ // entries.
+ $this->connection->query("SELECT setval('" . $seq . "', MAX(\"" . $field . '")) FROM {' . $table . "}");
+ $this->connection->query('ALTER TABLE {' . $table . '} ALTER "' . $field . '" SET DEFAULT nextval(\'' . $seq . '\')');
+ }
// Rename the column if necessary.
if ($field != $field_new) {
- $this->connection->query('ALTER TABLE {' . $table . '} RENAME "' . $field . '" TO "' . $field_new . '_old"');
+ $this->connection->query('ALTER TABLE {' . $table . '} RENAME "' . $field . '" TO "' . $field_new . '"');
+ }
+
+ // Change description if necessary.
+ if (!empty($spec['description'])) {
+ $this->connection->query('COMMENT ON COLUMN {' . $table . '}."' . $field_new . '" IS ' . $this->prepareComment($spec['description']));
}
if (isset($new_keys)) {