From cbcd394d0baa63b8c62e19005772dcafb715aaff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Lorenzo=20Rodr=C3=ADguez?= Date: Tue, 9 Mar 2010 16:09:16 -0430 Subject: [PATCH] Fixing alter schema queries generated for Postgres, which dos not support altering column type, nullable and default value at the same time --- cake/libs/model/datasources/dbo/dbo_postgres.php | 16 ++++++++++++++++ .../model/datasources/dbo/dbo_postgres.test.php | 9 ++++++--- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/cake/libs/model/datasources/dbo/dbo_postgres.php b/cake/libs/model/datasources/dbo/dbo_postgres.php index 194fed549..25f3cb03a 100644 --- a/cake/libs/model/datasources/dbo/dbo_postgres.php +++ b/cake/libs/model/datasources/dbo/dbo_postgres.php @@ -546,7 +546,23 @@ class DboPostgres extends DboSource { $col['name'] = $field; } $fieldName = $this->name($field); + + $default = isset($col['default']) ? $col['default'] : null; + $nullable = isset($col['null']) ? $col['null'] : null; + unset($col['default'], $col['null']); $colList[] = 'ALTER COLUMN '. $fieldName .' TYPE ' . str_replace($fieldName, '', $this->buildColumn($col)); + + if (isset($nullable)) { + $nullable = ($nullable) ? 'DROP NOT NULL' : 'SET NOT NULL'; + $colList[] = 'ALTER COLUMN '. $fieldName .' ' . $nullable; + } + + if (isset($default)) { + $colList[] = 'ALTER COLUMN '. $fieldName .' SET DEFAULT ' . $default; + } else { + $colList[] = 'ALTER COLUMN '. $fieldName .' DROP DEFAULT'; + } + } break; } diff --git a/cake/tests/cases/libs/model/datasources/dbo/dbo_postgres.test.php b/cake/tests/cases/libs/model/datasources/dbo/dbo_postgres.test.php index e789bae4a..5f408b117 100644 --- a/cake/tests/cases/libs/model/datasources/dbo/dbo_postgres.test.php +++ b/cake/tests/cases/libs/model/datasources/dbo/dbo_postgres.test.php @@ -564,7 +564,7 @@ class DboPostgresTest extends CakeTestCase { 'alter_posts' => array( 'id' => array('type' => 'integer', 'key' => 'primary'), 'author_id' => array('type' => 'integer', 'null' => false), - 'title' => array('type' => 'string', 'null' => false), + 'title' => array('type' => 'string', 'null' => true), 'body' => array('type' => 'text'), 'published' => array('type' => 'string', 'length' => 1, 'default' => 'N'), 'created' => array('type' => 'datetime'), @@ -578,10 +578,10 @@ class DboPostgresTest extends CakeTestCase { 'name' => 'AlterPosts', 'alter_posts' => array( 'id' => array('type' => 'integer', 'key' => 'primary'), - 'author_id' => array('type' => 'integer', 'null' => false), + 'author_id' => array('type' => 'integer', 'null' => true), 'title' => array('type' => 'string', 'null' => false), 'body' => array('type' => 'string', 'length' => 500), - 'status' => array('type' => 'integer', 'length' => 3), + 'status' => array('type' => 'integer', 'length' => 3, 'default' => 1), 'created' => array('type' => 'datetime'), 'updated' => array('type' => 'datetime'), ) @@ -593,6 +593,9 @@ class DboPostgresTest extends CakeTestCase { $this->assertTrue(isset($result['status'])); $this->assertFalse(isset($result['published'])); $this->assertEqual($result['body']['type'], 'string'); + $this->assertEqual($result['status']['default'], 1); + $this->assertEqual($result['author_id']['null'], true); + $this->assertEqual($result['title']['null'], false); $this->db->query($this->db->dropSchema($New)); }