mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2025-01-31 09:06:17 +00:00
Fixing issues in alterSchema in DboMysql and DboPostgres, where fields 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
This commit is contained in:
parent
aba97d0ab2
commit
686b9c2c8b
4 changed files with 72 additions and 3 deletions
|
@ -286,7 +286,7 @@ class DboMysqlBase extends DboSource {
|
|||
$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) {
|
||||
|
|
|
@ -562,7 +562,7 @@ class DboPostgres extends DboSource {
|
|||
$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 @@ class DboPostgres extends DboSource {
|
|||
} else {
|
||||
$out = '';
|
||||
}
|
||||
$out .= implode(";\n\t", $this->_alterIndexes($curTable, $indexes)) . ";";
|
||||
$out .= implode(";\n\t", $this->_alterIndexes($curTable, $indexes));
|
||||
}
|
||||
}
|
||||
return $out;
|
||||
|
|
|
@ -698,6 +698,40 @@ class DboMysqlTest extends CakeTestCase {
|
|||
$this->db->query($this->db->dropSchema($schema1));
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
*
|
||||
|
|
|
@ -824,4 +824,39 @@ class DboPostgresTest extends CakeTestCase {
|
|||
));
|
||||
$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');
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue