Fixing alter schema queries generated for Postgres, which dos not support altering column type, nullable and default value at the same time

This commit is contained in:
José Lorenzo Rodríguez 2010-03-09 16:09:16 -04:30
parent acccf964a5
commit cbcd394d0b
2 changed files with 22 additions and 3 deletions
cake
libs/model/datasources/dbo
tests/cases/libs/model/datasources/dbo

View file

@ -546,7 +546,23 @@ class DboPostgres extends DboSource {
$col['name'] = $field; $col['name'] = $field;
} }
$fieldName = $this->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)); $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; break;
} }

View file

@ -564,7 +564,7 @@ class DboPostgresTest extends CakeTestCase {
'alter_posts' => array( 'alter_posts' => array(
'id' => array('type' => 'integer', 'key' => 'primary'), 'id' => array('type' => 'integer', 'key' => 'primary'),
'author_id' => array('type' => 'integer', 'null' => false), 'author_id' => array('type' => 'integer', 'null' => false),
'title' => array('type' => 'string', 'null' => false), 'title' => array('type' => 'string', 'null' => true),
'body' => array('type' => 'text'), 'body' => array('type' => 'text'),
'published' => array('type' => 'string', 'length' => 1, 'default' => 'N'), 'published' => array('type' => 'string', 'length' => 1, 'default' => 'N'),
'created' => array('type' => 'datetime'), 'created' => array('type' => 'datetime'),
@ -578,10 +578,10 @@ class DboPostgresTest extends CakeTestCase {
'name' => 'AlterPosts', 'name' => 'AlterPosts',
'alter_posts' => array( 'alter_posts' => array(
'id' => array('type' => 'integer', 'key' => 'primary'), '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), 'title' => array('type' => 'string', 'null' => false),
'body' => array('type' => 'string', 'length' => 500), 'body' => array('type' => 'string', 'length' => 500),
'status' => array('type' => 'integer', 'length' => 3), 'status' => array('type' => 'integer', 'length' => 3, 'default' => 1),
'created' => array('type' => 'datetime'), 'created' => array('type' => 'datetime'),
'updated' => array('type' => 'datetime'), 'updated' => array('type' => 'datetime'),
) )
@ -593,6 +593,9 @@ class DboPostgresTest extends CakeTestCase {
$this->assertTrue(isset($result['status'])); $this->assertTrue(isset($result['status']));
$this->assertFalse(isset($result['published'])); $this->assertFalse(isset($result['published']));
$this->assertEqual($result['body']['type'], 'string'); $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)); $this->db->query($this->db->dropSchema($New));
} }