Skip to content

Commit

Permalink
Implementing CakeSchema:;_compareTableParameters. Adding tests for ta…
Browse files Browse the repository at this point in the history
…bleParameter comparison.
  • Loading branch information
markstory committed Oct 5, 2009
1 parent 5021d11 commit 778755c
Show file tree
Hide file tree
Showing 2 changed files with 118 additions and 13 deletions.
14 changes: 10 additions & 4 deletions cake/libs/model/cake_schema.php
Expand Up @@ -494,8 +494,7 @@ function compare($old, $new = null) {
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;
}
}
}
Expand Down Expand Up @@ -575,7 +574,14 @@ function __columns(&$Obj) {
* @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;
}

/**
Expand Down Expand Up @@ -626,7 +632,7 @@ function _compareIndexes($new, $old) {
}
}
}
return array_filter(compact('add', 'drop'));
return compact('add', 'drop');
}
}
?>
117 changes: 108 additions & 9 deletions cake/tests/cases/libs/model/cake_schema.test.php
Expand Up @@ -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
Expand Down Expand Up @@ -537,31 +537,130 @@ function testSchemaComparison() {
'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
*
Expand Down

0 comments on commit 778755c

Please sign in to comment.