Skip to content

Commit

Permalink
Fix migration generation when only indexes are changed (closes #189)
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris Burke committed Oct 16, 2014
1 parent c98f004 commit c952119
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 2 deletions.
7 changes: 5 additions & 2 deletions Console/Command/MigrationShell.php
Original file line number Diff line number Diff line change
Expand Up @@ -901,9 +901,12 @@ protected function _generateMigration($name, $class, $migration) {
}
unset($fields['indexes']);

$content .= "\t\t\t\t'" . $table . "' => array('" . implode("', '", $fields) . "',";
$content .= "\t\t\t\t'" . $table . "' => array(";
if (!empty($fields)) {
$content .= "'" . implode("', '", $fields) . "', ";
}
if (!empty($indexes)) {
$content .= " 'indexes' => array('" . implode("', '", $indexes) . "')";
$content .= "'indexes' => array('" . implode("', '", $indexes) . "')";
}
$content .= "),\n";
}
Expand Down
57 changes: 57 additions & 0 deletions Test/Case/Console/Command/MigrationShellTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -683,6 +683,63 @@ public function testWriteMigration() {
$this->_unlink('12345_migration_test_file.php');
}

/**
* Test writing migration that only contains index changes
*
* @return void
* @link https://github.com/CakeDC/migrations/issues/189
*/
public function testWriteMigrationIndexesOnly() {
$this->_unlink('12345_migration_test_file.php');

$users = $this->tables['users'];
$users['indexes'] = array('UNIQUE_USER' => array('column' => 'user', 'unique' => true));
$migration = array(
'up' => array(
'create_field' => array(
'posts' => array(
'indexes' => array(
'USER_ID' => array('column' => 'user_id', 'unique' => false)
)
)
)
),
'down' => array(
'drop_field' => array(
'posts' => array(
'indexes' => array('USER_ID')
)
)
)
);

$this->assertFalse(file_exists(TMP . 'tests' . DS . '12345_migration_test_file.php'));
$this->assertTrue($this->Shell->writeMigration('migration_test_file', 12345, $migration));
$this->assertTrue(file_exists(TMP . 'tests' . DS . '12345_migration_test_file.php'));

$result = $this->_getMigrationVariable(TMP . 'tests' . DS . '12345_migration_test_file.php');
$expected = <<<TEXT
public \$migration = array(
'up' => array(
'create_field' => array(
'posts' => array(
'indexes' => array(
'USER_ID' => array('column' => 'user_id', 'unique' => false),
),
),
),
),
'down' => array(
'drop_field' => array(
'posts' => array('indexes' => array('USER_ID')),
),
),
);
TEXT;
$this->assertEquals($result, str_replace("\r\n", "\n", $expected));
$this->_unlink('12345_migration_test_file.php');
}

/**
* TestGenerate method
*
Expand Down

0 comments on commit c952119

Please sign in to comment.