Merge branch 'postgres_altercolum_error' into master

Merge changes from #5512 into master. This fixes changing columns in
postgres to integer from string types.
This commit is contained in:
mark_story 2015-03-14 22:23:43 -04:00
commit 827465fa30
2 changed files with 41 additions and 3 deletions

View file

@ -1,7 +1,5 @@
<?php
/**
* PostgreSQL layer for DBO.
*
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
@ -557,9 +555,13 @@ class Postgres extends DboSource {
if ($boolToInt) {
$colList[] = 'ALTER COLUMN ' . $fieldName . ' SET DEFAULT NULL';
$colList[] = 'ALTER COLUMN ' . $fieldName . ' TYPE ' . str_replace(array($fieldName, 'NOT NULL'), '', $this->buildColumn($col)) . ' USING CASE WHEN TRUE THEN 1 ELSE 0 END';
} else {
if ($original['type'] === 'text' && $col['type'] === 'integer') {
$colList[] = 'ALTER COLUMN ' . $fieldName . ' TYPE ' . str_replace(array($fieldName, 'NOT NULL'), '', $this->buildColumn($col)) . " USING cast({$fieldName} as INTEGER)";
} else {
$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';

View file

@ -731,6 +731,42 @@ class PostgresTest extends CakeTestCase {
$this->Dbo->query($this->Dbo->dropSchema($Old));
}
/**
* Test the alterSchema changing text to integer
*
* @return void
*/
public function testAlterSchemaTextToIntegerField() {
$default = array(
'connection' => 'test',
'name' => 'TextField',
'text_fields' => array(
'id' => array('type' => 'integer', 'key' => 'primary'),
'name' => array('type' => 'string', 'length' => 50),
'active' => array('type' => 'text', 'null' => false),
)
);
$Old = new CakeSchema($default);
$result = $this->Dbo->query($this->Dbo->createSchema($Old));
$this->assertTrue($result);
$modified = $default;
$modified['text_fields']['active'] = array('type' => 'integer', 'null' => true);
$New = new CakeSchema($modified);
$this->Dbo->query($this->Dbo->alterSchema($New->compare($Old)));
$result = $this->Dbo->describe('text_fields');
$this->Dbo->query($this->Dbo->dropSchema($Old));
$expected = array(
'type' => 'integer',
'null' => true,
'default' => null,
'length' => null,
);
$this->assertEquals($expected, $result['active']);
}
/**
* Test the alter index capabilities of postgres
*