Skip to content

Commit 76bb49c

Browse files
committed
implement code & test for 'remove fields' from cli arguments
Signed-off-by: Joshua Paling <joshua.paling@gmail.com>
1 parent c577686 commit 76bb49c

File tree

3 files changed

+41
-0
lines changed

3 files changed

+41
-0
lines changed

Console/Command/MigrationShell.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,7 @@ protected function _generateFromCliArgs(&$migration, &$migrationName, &$comparis
434434
}
435435

436436
$cli = $this->_parseCommandLine($migrationName);
437+
// debug($cli); die;
437438

438439
$action = $cli['action'];
439440
$table = $cli['table'];
@@ -445,6 +446,12 @@ protected function _generateFromCliArgs(&$migration, &$migrationName, &$comparis
445446
$migration['down']['drop_table'] = array($table);
446447
} elseif ($action == 'drop_table') {
447448
$migration['up']['drop_table'] = array($table);
449+
} elseif ($action == 'drop_field') {
450+
$fieldsToDrop = array();
451+
foreach ($fields as $name => $value) {
452+
$fieldsToDrop[] = $name;
453+
}
454+
$migration['up']['drop_field'][$table] = $fieldsToDrop;
448455
}
449456

450457
/* if (in_array($action, array('create_table', 'alter_table', 'create_field'))) {

Test/Case/Console/Command/MigrationShellTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -813,6 +813,31 @@ public function testGenerateFromCliParamsDropTable() {
813813
$this->assertEquals($expected, $result);
814814
}
815815

816+
/**
817+
* testGenerateFromCliParamsRemoveFields method
818+
* test the case of using a command such as:
819+
* app/Console/cake Migrations.migration generate remove_name_and_desc_from_products name description
820+
*
821+
* @return void
822+
*/
823+
public function testGenerateFromCliParamsRemoveFields() {
824+
$this->Shell->expects($this->at(0))->method('in')->will($this->returnValue('n'));
825+
$this->assertEmpty(glob(TMP . 'tests' . DS . '*remove_name_and_desc_from_products.php'));
826+
827+
$this->Shell->args = array('remove_name_and_desc_from_products', 'name', 'description');
828+
$this->Shell->params['force'] = true;
829+
$this->Shell->generate();
830+
$files = glob(TMP . 'tests' . DS . '*remove_name_and_desc_from_products.php');
831+
$this->assertNotEmpty($files);
832+
$result = $this->_getMigrationVariable(current($files));
833+
foreach ($files as $f) {
834+
unlink($f);
835+
}
836+
837+
$expected = file_get_contents(CakePlugin::path('Migrations') . '/Test/Fixture/test_migration_remove_fields_from_cli.txt');
838+
$this->assertEquals($expected, $result);
839+
}
840+
816841
/**
817842
* TestGenerateDump method
818843
*
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
public $migration = array(
2+
'up' => array(
3+
'drop_field' => array(
4+
'products' => array('name', 'description',),
5+
),
6+
),
7+
'down' => array(
8+
),
9+
);

0 commit comments

Comments
 (0)