From 778755c2b03c2d045fb7627307391ac31ef63256 Mon Sep 17 00:00:00 2001 From: mark_story Date: Sun, 4 Oct 2009 20:53:49 -0400 Subject: [PATCH] Implementing CakeSchema:;_compareTableParameters. Adding tests for tableParameter comparison. --- cake/libs/model/cake_schema.php | 14 ++- .../cases/libs/model/cake_schema.test.php | 117 ++++++++++++++++-- 2 files changed, 118 insertions(+), 13 deletions(-) diff --git a/cake/libs/model/cake_schema.php b/cake/libs/model/cake_schema.php index dabb7c348..4bc1cac91 100644 --- a/cake/libs/model/cake_schema.php +++ b/cake/libs/model/cake_schema.php @@ -494,8 +494,7 @@ class CakeSchema extends Object { if (isset($old[$table]['tableParameters']) && isset($new[$table]['tableParameters'])) { $diff = $this->_compareTableParameters($new[$table]['tableParameters'], $old[$table]['tableParameters']); if ($diff) { - $tables[$table]['drop']['tableParameters'] = $diff['drop']; - $tables[$table]['add']['tableParameters'] = $diff['add']; + $tables[$table]['change']['tableParameters'] = $diff; } } } @@ -575,7 +574,14 @@ class CakeSchema extends Object { * @return mixed False on failure, or an array of parameters to add & drop. **/ function _compareTableParameters($new, $old) { - + if (!is_array($new) || !is_array($old)) { + return false; + } + + $change = array(); + + $change = array_diff_assoc($new, $old); + return $change; } /** @@ -626,7 +632,7 @@ class CakeSchema extends Object { } } } - return array_filter(compact('add', 'drop')); + return compact('add', 'drop'); } } ?> \ No newline at end of file diff --git a/cake/tests/cases/libs/model/cake_schema.test.php b/cake/tests/cases/libs/model/cake_schema.test.php index 084226b49..28cd863d9 100644 --- a/cake/tests/cases/libs/model/cake_schema.test.php +++ b/cake/tests/cases/libs/model/cake_schema.test.php @@ -19,7 +19,7 @@ * @since CakePHP(tm) v 1.2.0.5550 * @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License */ -App::import('Core', 'CakeSchema'); +App::import('Model', 'CakeSchema', false); /** * Test for Schema database management @@ -537,31 +537,130 @@ class CakeSchemaTest extends CakeTestCase { 'comments' => array( 'add' => array( 'post_id' => array('type' => 'integer', 'null' => false, 'default' => 0), - 'title' => array('type' => 'string', 'null' => false, 'length' => 100) + 'title' => array('type' => 'string', 'null' => false, 'length' => 100), + 'indexes' => array(), ), 'drop' => array( 'article_id' => array('type' => 'integer', 'null' => false), - 'tableParameters' => array() + 'tableParameters' => array(), + 'indexes' => array(), ), 'change' => array( - 'comment' => array('type' => 'text', 'null' => false, 'default' => null) + 'comment' => array('type' => 'text', 'null' => false, 'default' => null), ) ), 'posts' => array( - 'add' => array('summary' => array('type' => 'text', 'null' => 1)), - 'drop' => array('tableParameters' => 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' => ''), 'title' => array('type' => 'string', 'null' => false, 'default' => 'Title'), - 'published' => array( - 'type' => 'string', 'null' => true, 'default' => 'Y', 'length' => '1' - ) + 'published' => array('type' => 'string', 'null' => true, 'default' => 'Y', 'length' => '1') ) ), ); $this->assertEqual($expected, $compare); } +/** + * Test comparing tableParameters and indexes. + * + * @return void + **/ + function testTableParametersAndIndexComparison() { + $old = array( + 'posts' => array( + 'id' => array('type' => 'integer', 'null' => false, 'default' => 0, 'key' => 'primary'), + 'author_id' => array('type' => 'integer', 'null' => false), + 'title' => array('type' => 'string', 'null' => false), + 'indexes' => array( + 'PRIMARY' => array('column' => 'id', 'unique' => true) + ), + 'tableParameters' => array( + 'charset' => 'latin1', + 'collate' => 'latin1_general_ci' + ) + ), + 'comments' => array( + 'id' => array('type' => 'integer', 'null' => false, 'default' => 0, 'key' => 'primary'), + 'post_id' => array('type' => 'integer', 'null' => false, 'default' => 0), + 'comment' => array('type' => 'text'), + 'indexes' => array( + 'PRIMARY' => array('column' => 'id', 'unique' => true), + 'post_id' => array('column' => 'post_id'), + ), + 'tableParameters' => array( + 'engine' => 'InnoDB', + 'charset' => 'latin1', + 'collate' => 'latin1_general_ci' + ) + ) + ); + $new = array( + 'posts' => array( + 'id' => array('type' => 'integer', 'null' => false, 'default' => 0, 'key' => 'primary'), + 'author_id' => array('type' => 'integer', 'null' => false), + 'title' => array('type' => 'string', 'null' => false), + 'indexes' => array( + 'PRIMARY' => array('column' => 'id', 'unique' => true), + 'author_id' => array('column' => 'author_id'), + ), + 'tableParameters' => array( + 'charset' => 'utf8', + 'collate' => 'utf8_general_ci', + 'engine' => 'MyISAM' + ) + ), + 'comments' => array( + 'id' => array('type' => 'integer', 'null' => false, 'default' => 0, 'key' => 'primary'), + 'post_id' => array('type' => 'integer', 'null' => false, 'default' => 0), + 'comment' => array('type' => 'text'), + 'indexes' => array( + 'PRIMARY' => array('column' => 'id', 'unique' => true), + ), + 'tableParameters' => array( + 'charset' => 'utf8', + 'collate' => 'utf8_general_ci' + ) + ) + ); + $compare = $this->Schema->compare($old, $new); + $expected = array( + 'posts' => array( + 'drop' => array('indexes' => array()), + 'add' => array( + 'indexes' => array('author_id' => array('column' => 'author_id')), + ), + 'change' => array( + 'tableParameters' => array( + 'charset' => 'utf8', + 'collate' => 'utf8_general_ci', + 'engine' => 'MyISAM' + ) + ) + ), + 'comments' => array( + 'add' => array('indexes' => array()), + 'drop' => array( + 'indexes' => array('post_id' => array('column' => 'post_id')), + ), + 'change' => array( + 'tableParameters' => array( + 'charset' => 'utf8', + 'collate' => 'utf8_general_ci', + ) + ) + ) + ); + $this->assertEqual($compare, $expected); + } + /** * testSchemaLoading method *