Skip to content

Commit

Permalink
Create rename field statements in postgres
Browse files Browse the repository at this point in the history
Refs #3622
  • Loading branch information
nojimage authored and markstory committed Feb 14, 2013
1 parent 1b22d48 commit fbb1a81
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
6 changes: 6 additions & 0 deletions lib/Cake/Model/Datasource/Database/Postgres.php
Expand Up @@ -526,6 +526,12 @@ public function alterSchema($compare, $table = null) {
$default = isset($col['default']) ? $col['default'] : null; $default = isset($col['default']) ? $col['default'] : null;
$nullable = isset($col['null']) ? $col['null'] : null; $nullable = isset($col['null']) ? $col['null'] : null;
unset($col['default'], $col['null']); unset($col['default'], $col['null']);
if ($field !== $col['name']) {
$newName = $this->name($col['name']);
$out .= "\tRENAME {$fieldName} TO {$newName};\n";
$out .= 'ALTER TABLE ' . $this->fullTableName($curTable) . " \n";
$fieldName = $newName;
}
$colList[] = 'ALTER COLUMN ' . $fieldName . ' TYPE ' . str_replace(array($fieldName, 'NOT NULL'), '', $this->buildColumn($col)); $colList[] = 'ALTER COLUMN ' . $fieldName . ' TYPE ' . str_replace(array($fieldName, 'NOT NULL'), '', $this->buildColumn($col));
if (isset($nullable)) { if (isset($nullable)) {
$nullable = ($nullable) ? 'DROP NOT NULL' : 'SET NOT NULL'; $nullable = ($nullable) ? 'DROP NOT NULL' : 'SET NOT NULL';
Expand Down
15 changes: 15 additions & 0 deletions lib/Cake/Test/Case/Model/Datasource/Database/PostgresTest.php
Expand Up @@ -739,6 +739,21 @@ public function testAlterIndexes() {
$this->Dbo->query($this->Dbo->dropSchema($schema1)); $this->Dbo->query($this->Dbo->dropSchema($schema1));
} }


/**
* Test the alterSchema RENAME statements
*
* @return void
*/
public function testAlterSchemaRenameTo() {
$query = $this->Dbo->alterSchema(array(
'posts' => array('change' => array('title' => array('name' => 'subject', 'type' => 'string', 'null' => false)))
));
$this->assertRegExp('/RENAME "title" TO "subject";/i', $query);
$this->assertRegExp('/ALTER COLUMN "subject" TYPE /i', $query);
$this->assertNotRegExp('/;\n\tALTER COLUMN "subject" TYPE /i', $query);
$this->assertNotRegExp('/ALTER COLUMN "title" TYPE "subject"/i', $query);
}

/** /**
* Test it is possible to use virtual field with postgresql * Test it is possible to use virtual field with postgresql
* *
Expand Down

1 comment on commit fbb1a81

@kaz29
Copy link
Contributor

@kaz29 kaz29 commented on fbb1a81 Apr 27, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Please sign in to comment.