diff --git a/Console/Command/MigrationShell.php b/Console/Command/MigrationShell.php index ce212d56..b0a8a0f1 100644 --- a/Console/Command/MigrationShell.php +++ b/Console/Command/MigrationShell.php @@ -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"; } diff --git a/Test/Case/Console/Command/MigrationShellTest.php b/Test/Case/Console/Command/MigrationShellTest.php index d822420b..8f072f77 100644 --- a/Test/Case/Console/Command/MigrationShellTest.php +++ b/Test/Case/Console/Command/MigrationShellTest.php @@ -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 = << 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 *