Skip to content
Browse files

Fixing issues in alterSchema in DboMysql and DboPostgres, where field…

…s would be appended to each table being altered.

Also fixed an issue in DboPostgres where the generated alter statements would contain too many ;.
Test cases added.  Fixes #1023
  • Loading branch information...
1 parent aba97d0 commit 686b9c2c8bc0d4636ea0d0e5abc040bae90e4156 @markstory markstory committed
View
2 cake/libs/model/datasources/dbo/dbo_mysql.php
@@ -286,7 +286,7 @@ function alterSchema($compare, $table = null) {
$out = '';
$colList = array();
foreach ($compare as $curTable => $types) {
- $indexes = $tableParameters = array();
+ $indexes = $tableParameters = $colList = array();
if (!$table || $table == $curTable) {
$out .= 'ALTER TABLE ' . $this->fullTableName($curTable) . " \n";
foreach ($types as $type => $column) {
View
4 cake/libs/model/datasources/dbo/dbo_postgres.php
@@ -562,7 +562,7 @@ function alterSchema($compare, $table = null) {
$out = '';
$colList = array();
foreach ($compare as $curTable => $types) {
- $indexes = array();
+ $indexes = $colList = array();
if (!$table || $table == $curTable) {
$out .= 'ALTER TABLE ' . $this->fullTableName($curTable) . " \n";
foreach ($types as $type => $column) {
@@ -630,7 +630,7 @@ function alterSchema($compare, $table = null) {
} else {
$out = '';
}
- $out .= implode(";\n\t", $this->_alterIndexes($curTable, $indexes)) . ";";
+ $out .= implode(";\n\t", $this->_alterIndexes($curTable, $indexes));
}
}
return $out;
View
34 cake/tests/cases/libs/model/datasources/dbo/dbo_mysql.test.php
@@ -699,6 +699,40 @@ function testAlteringTableParameters() {
}
/**
+ * test alterSchema on two tables.
+ *
+ * @return void
+ */
+ function testAlteringTwoTables() {
+ $schema1 =& new CakeSchema(array(
+ 'name' => 'AlterTest1',
+ 'connection' => 'test_suite',
+ 'altertest' => array(
+ 'id' => array('type' => 'integer', 'null' => false, 'default' => 0),
+ 'name' => array('type' => 'string', 'null' => false, 'length' => 50),
+ ),
+ 'other_table' => array(
+ 'id' => array('type' => 'integer', 'null' => false, 'default' => 0),
+ 'name' => array('type' => 'string', 'null' => false, 'length' => 50),
+ )
+ ));
+ $schema2 =& new CakeSchema(array(
+ 'name' => 'AlterTest1',
+ 'connection' => 'test_suite',
+ 'altertest' => array(
+ 'id' => array('type' => 'integer', 'null' => false, 'default' => 0),
+ 'field_two' => array('type' => 'string', 'null' => false, 'length' => 50),
+ ),
+ 'other_table' => array(
+ 'id' => array('type' => 'integer', 'null' => false, 'default' => 0),
+ 'field_two' => array('type' => 'string', 'null' => false, 'length' => 50),
+ )
+ ));
+ $result = $this->db->alterSchema($schema2->compare($schema1));
+ $this->assertEqual(2, substr_count($result, 'field_two'), 'Too many fields');
+ }
+
+/**
* testReadTableParameters method
*
* @access public
View
35 cake/tests/cases/libs/model/datasources/dbo/dbo_postgres.test.php
@@ -824,4 +824,39 @@ function testUpdateAllWithNonQualifiedConditions() {
));
$this->assertEqual($result, 3, 'Article count is wrong or fixture has changed.');
}
+
+/**
+ * test alterSchema on two tables.
+ *
+ * @return void
+ */
+ function testAlteringTwoTables() {
+ $schema1 =& new CakeSchema(array(
+ 'name' => 'AlterTest1',
+ 'connection' => 'test_suite',
+ 'altertest' => array(
+ 'id' => array('type' => 'integer', 'null' => false, 'default' => 0),
+ 'name' => array('type' => 'string', 'null' => false, 'length' => 50),
+ ),
+ 'other_table' => array(
+ 'id' => array('type' => 'integer', 'null' => false, 'default' => 0),
+ 'name' => array('type' => 'string', 'null' => false, 'length' => 50),
+ )
+ ));
+ $schema2 =& new CakeSchema(array(
+ 'name' => 'AlterTest1',
+ 'connection' => 'test_suite',
+ 'altertest' => array(
+ 'id' => array('type' => 'integer', 'null' => false, 'default' => 0),
+ 'field_two' => array('type' => 'string', 'null' => false, 'length' => 50),
+ ),
+ 'other_table' => array(
+ 'id' => array('type' => 'integer', 'null' => false, 'default' => 0),
+ 'field_two' => array('type' => 'string', 'null' => false, 'length' => 50),
+ )
+ ));
+ $result = $this->db->alterSchema($schema2->compare($schema1));
+ $this->assertEqual(2, substr_count($result, 'field_two'), 'Too many fields');
+ $this->assertFalse(strpos(';ALTER', $result), 'Too many semi colons');
+ }
}

0 comments on commit 686b9c2

Please sign in to comment.
Something went wrong with that request. Please try again.