Skip to content

Commit

Permalink
refs #9174 fix CakeSchema compare when changing field length to the d…
Browse files Browse the repository at this point in the history
…efault one
  • Loading branch information
andrej-griniuk committed Jul 26, 2016
1 parent a73a707 commit 1476936
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 2 deletions.
5 changes: 3 additions & 2 deletions lib/Cake/Model/CakeSchema.php
Expand Up @@ -486,8 +486,9 @@ public function compare($old, $new = null) {

foreach ($fields as $field => $value) {
if (!empty($old[$table][$field])) {
$diff = $this->_arrayDiffAssoc($value, $old[$table][$field]);
if (!empty($diff) && $field !== 'indexes' && $field !== 'tableParameters') {
$diff = !empty($this->_arrayDiffAssoc($value, $old[$table][$field]))
|| !empty($this->_arrayDiffAssoc($old[$table][$field], $value));
if ($diff && $field !== 'indexes' && $field !== 'tableParameters') {
$tables[$table]['change'][$field] = $value;
}
}
Expand Down
57 changes: 57 additions & 0 deletions lib/Cake/Test/Case/Model/CakeSchemaTest.php
Expand Up @@ -953,6 +953,63 @@ public function testCompareVarcharToDatetime() {
$this->assertEquals($expected, $compare, 'Invalid SQL, datetime does not have length');
}

/**
* Test comparing with field length/limit changed from some non-default value to the default
*
* @return void
*/
public function testCompareLimitToDefault() {
$old = array(
'posts' => array(
'id' => array('type' => 'integer', 'null' => false, 'default' => 1, 'key' => 'primary'),
'author_id' => array('type' => 'integer', 'null' => false, 'limit' => 5),
'title' => array('type' => 'string', 'null' => true, 'length' => 45),
'indexes' => array(
'PRIMARY' => array('column' => 'id', 'unique' => true)
),
'tableParameters' => array(
'charset' => 'latin1',
'collate' => 'latin1_general_ci'
)
),
);
$new = array(
'posts' => array(
'id' => array('type' => 'integer', 'null' => false, 'key' => 'primary'),
'author_id' => array('type' => 'integer', 'null' => false),
'title' => array('type' => 'varchar', 'null' => true),
'indexes' => array(
'PRIMARY' => array('column' => 'id', 'unique' => true)
),
'tableParameters' => array(
'charset' => 'latin1',
'collate' => 'latin1_general_ci'
)
),
);
$compare = $this->Schema->compare($old, $new);
$expected = array(
'posts' => array(
'change' => array(
'id' => array(
'type' => 'integer',
'null' => false,
'key' => 'primary'
),
'author_id' => array(
'type' => 'integer',
'null' => false,
),
'title' => array(
'type' => 'varchar',
'null' => true,
)
)
),
);
$this->assertEquals($expected, $compare, 'Invalid SQL, field length change not detected');
}

/**
* testSchemaLoading method
*
Expand Down

0 comments on commit 1476936

Please sign in to comment.