From fbb1a812dda97f77d59ca012bffa2e0f4e5a4eee Mon Sep 17 00:00:00 2001 From: nojimage Date: Wed, 13 Feb 2013 21:44:49 +0900 Subject: [PATCH] Create rename field statements in postgres Refs #3622 --- lib/Cake/Model/Datasource/Database/Postgres.php | 6 ++++++ .../Model/Datasource/Database/PostgresTest.php | 15 +++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/lib/Cake/Model/Datasource/Database/Postgres.php b/lib/Cake/Model/Datasource/Database/Postgres.php index 002d25f68e9..3065b4d40ae 100644 --- a/lib/Cake/Model/Datasource/Database/Postgres.php +++ b/lib/Cake/Model/Datasource/Database/Postgres.php @@ -526,6 +526,12 @@ public function alterSchema($compare, $table = null) { $default = isset($col['default']) ? $col['default'] : null; $nullable = isset($col['null']) ? $col['null'] : 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)); if (isset($nullable)) { $nullable = ($nullable) ? 'DROP NOT NULL' : 'SET NOT NULL'; diff --git a/lib/Cake/Test/Case/Model/Datasource/Database/PostgresTest.php b/lib/Cake/Test/Case/Model/Datasource/Database/PostgresTest.php index 324b9dcc8dc..bd2faa20028 100644 --- a/lib/Cake/Test/Case/Model/Datasource/Database/PostgresTest.php +++ b/lib/Cake/Test/Case/Model/Datasource/Database/PostgresTest.php @@ -739,6 +739,21 @@ public function testAlterIndexes() { $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 *