Skip to content

Commit

Permalink
Fixing broken tests in dbo_mysql caused by changes in CakeSchema.
Browse files Browse the repository at this point in the history
Adding tableParameter altering to DboMysql.
Tests added.
  • Loading branch information
markstory committed Oct 5, 2009
1 parent 7b0bc01 commit d2bf31f
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 21 deletions.
9 changes: 4 additions & 5 deletions cake/libs/model/cake_schema.php
Expand Up @@ -486,8 +486,10 @@ function compare($old, $new = null) {

if (isset($old[$table]['indexes']) && isset($new[$table]['indexes'])) {
$diff = $this->_compareIndexes($new[$table]['indexes'], $old[$table]['indexes']);
if ($diff) {
if ($diff && isset($diff['drop'])) {
$tables[$table]['drop']['indexes'] = $diff['drop'];
}
if ($diff && isset($diff['add'])) {
$tables[$table]['add']['indexes'] = $diff['add'];
}
}
Expand Down Expand Up @@ -577,9 +579,6 @@ function _compareTableParameters($new, $old) {
if (!is_array($new) || !is_array($old)) {
return false;
}

$change = array();

$change = array_diff_assoc($new, $old);
return $change;
}
Expand Down Expand Up @@ -632,7 +631,7 @@ function _compareIndexes($new, $old) {
}
}
}
return compact('add', 'drop');
return array_filter(compact('add', 'drop'));
}
}
?>
3 changes: 3 additions & 0 deletions cake/libs/model/datasources/dbo/dbo_mysql.php
Expand Up @@ -326,6 +326,9 @@ function dropSchema($schema, $table = null) {
* @todo Implement this method.
**/
function _alterTableParameters($table, $parameters) {
if (isset($parameters['change'])) {
return $this->buildTableParameters($parameters['change']);
}
return array();
}

Expand Down
6 changes: 0 additions & 6 deletions cake/tests/cases/libs/model/cake_schema.test.php
Expand Up @@ -538,12 +538,10 @@ function testSchemaComparison() {
'add' => array(
'post_id' => array('type' => 'integer', 'null' => false, 'default' => 0),
'title' => array('type' => 'string', 'null' => false, 'length' => 100),
'indexes' => array(),
),
'drop' => array(
'article_id' => array('type' => 'integer', 'null' => false),
'tableParameters' => array(),
'indexes' => array(),
),
'change' => array(
'comment' => array('type' => 'text', 'null' => false, 'default' => null),
Expand All @@ -552,11 +550,9 @@ function testSchemaComparison() {
'posts' => array(
'add' => array(
'summary' => array('type' => 'text', 'null' => 1),
'indexes' => array(),
),
'drop' => array(
'tableParameters' => array(),
'indexes' => array(),
),
'change' => array(
'author_id' => array('type' => 'integer', 'null' => true, 'default' => ''),
Expand Down Expand Up @@ -633,7 +629,6 @@ function testTableParametersAndIndexComparison() {
$compare = $this->Schema->compare($old, $new);
$expected = array(
'posts' => array(
'drop' => array('indexes' => array()),
'add' => array(
'indexes' => array('author_id' => array('column' => 'author_id')),
),
Expand All @@ -646,7 +641,6 @@ function testTableParametersAndIndexComparison() {
)
),
'comments' => array(
'add' => array('indexes' => array()),
'drop' => array(
'indexes' => array('post_id' => array('column' => 'post_id')),
),
Expand Down
65 changes: 55 additions & 10 deletions cake/tests/cases/libs/model/datasources/dbo/dbo_mysql.test.php
@@ -1,26 +1,21 @@
<?php
/* SVN FILE: $Id$ */

/**
* DboMysqlTest file
*
* PHP versions 4 and 5
*
* CakePHP(tm) : Rapid Development Framework (http://www.cakephp.org)
* Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
* Copyright 2005-2009, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @filesource
* @copyright Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
* @copyright Copyright 2005-2009, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
* @link http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project
* @package cake
* @subpackage cake.cake.libs
* @since CakePHP(tm) v 1.2.0
* @version $Revision$
* @modifiedby $LastChangedBy$
* @lastmodified $Date$
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
*/
App::import('Core', array('Model', 'DataSource', 'DboSource', 'DboMysql'));
Expand Down Expand Up @@ -190,7 +185,7 @@ function skip() {
*
* @access public
*/
function setUp() {
function startTest() {
$db = ConnectionManager::getDataSource('test_suite');
$this->db = new DboMysqlTestDb($db->config);
$this->model = new MysqlTestModel();
Expand All @@ -201,7 +196,7 @@ function setUp() {
*
* @access public
*/
function tearDown() {
function endTest() {
unset($this->db);
}

Expand Down Expand Up @@ -568,7 +563,7 @@ function testColumn() {
* @return void
*/
function testAlterSchemaIndexes() {
App::import('Core', 'CakeSchema');
App::import('Model', 'CakeSchema');
$this->db->cacheSources = $this->db->testing = false;

$schema1 =& new CakeSchema(array(
Expand Down Expand Up @@ -634,6 +629,56 @@ function testAlterSchemaIndexes() {
$this->db->query($this->db->dropSchema($schema1));
}

/**
* test altering the table settings with schema.
*
* @return void
**/
function testAlteringTableParameters() {
App::import('Model', 'CakeSchema');
$this->db->cacheSources = $this->db->testing = false;

$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),
'tableParameters' => array(
'charset' => 'latin1',
'collate' => 'latin1_general_ci',
'engine' => 'MyISAM'
)
)
));
$this->db->query($this->db->createSchema($schema1));
$schema2 =& 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),
'tableParameters' => array(
'charset' => 'utf8',
'collate' => 'utf8_general_ci',
'engine' => 'InnoDB'
)
)
));
$result = $this->db->alterSchema($schema2->compare($schema1));
$this->assertPattern('/DEFAULT CHARSET=utf8/', $result);
$this->assertPattern('/ENGINE=InnoDB/', $result);
$this->assertPattern('/COLLATE=utf8_general_ci/', $result);

$this->db->query($result);
$result = $this->db->listDetailedSources('altertest');
$this->assertEqual($result['Collation'], 'utf8_general_ci');
$this->assertEqual($result['Engine'], 'InnoDB');
$this->assertEqual($result['charset'], 'utf8');

$this->db->query($this->db->dropSchema($schema1));
}

/**
* testReadTableParameters method
*
Expand Down

0 comments on commit d2bf31f

Please sign in to comment.