Skip to content

Commit 805bedc

Browse files
committed
Merge branch 'ftwbzhao-develop'
2 parents 29f8dac + b7afa72 commit 805bedc

2 files changed

Lines changed: 43 additions & 3 deletions

File tree

src/Command/Generate/Migration.php

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@
2222
*/
2323
class Migration extends Command
2424
{
25-
public function __construct(Context $context, Stdio $stdio, CI_Controller $ci) {
25+
public function __construct(Context $context, Stdio $stdio, CI_Controller $ci)
26+
{
2627
parent::__construct($context, $stdio, $ci);
2728
$this->load->config('migration');
2829
}
@@ -44,8 +45,32 @@ public function __invoke($type, $classname)
4445
}
4546

4647
$migration_path = $this->config->item('migration_path');
48+
4749
$file_path = $migration_path . date('YmdHis') . '_' . $classname . '.php';
4850

51+
// check file exist
52+
if (file_exists($file_path)) {
53+
$this->stdio->errln(
54+
"<<red>>The file \"$file_path\" already exists<<reset>>"
55+
);
56+
return Status::FAILURE;
57+
}
58+
59+
// check class exist
60+
foreach (glob($migration_path . '*_*.php') as $file) {
61+
$name = basename($file, '.php');
62+
63+
// use date('YmdHis') so...
64+
if (preg_match('/^\d{14}_(\w+)$/', $name, $match)) {
65+
if (strtolower($match[1]) === strtolower($classname)) {
66+
$this->stdio->errln(
67+
"<<red>>The Class \"$classname\" already exists<<reset>>"
68+
);
69+
return Status::FAILURE;
70+
}
71+
}
72+
}
73+
4974
$template = file_get_contents(__DIR__ . '/templates/Migration.txt');
5075
$search = [
5176
'@@classname@@',

tests/Command/GenerateTest.php

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,22 @@ public function test_migration_generate()
5050
$migration_path = __DIR__ . '/../Fake/migrations/';
5151
$this->ci->config->set_item('migration_path', $migration_path);
5252
$status = $this->cmd->__invoke('migration', 'Test_of_generate_migration');
53-
$this->assertEquals(0, $status);
53+
$this->assertEquals(Status::SUCCESS, $status);
54+
55+
foreach (glob($migration_path . '*_Test_of_generate_migration.php') as $file) {
56+
unlink($file);
57+
}
58+
}
59+
60+
public function test_migration_generate_class_exist()
61+
{
62+
$migration_path = __DIR__ . '/../Fake/migrations/';
63+
$this->ci->config->set_item('migration_path', $migration_path);
64+
$status = $this->cmd->__invoke('migration', 'Test_of_generate_migration');
65+
$status = $this->cmd->__invoke('migration', 'Test_of_generate_migration');
66+
$this->assertEquals(Status::FAILURE, $status);
67+
$status = $this->cmd->__invoke('migration', 'Test_of_Generate_Migration');
68+
$this->assertEquals(Status::FAILURE, $status);
5469

5570
foreach (glob($migration_path . '*_Test_of_generate_migration.php') as $file) {
5671
unlink($file);
@@ -63,7 +78,7 @@ public function test_migration_cannot_write_to_file()
6378
$this->ci->config->set_item('migration_path', $migration_path);
6479
$status = $this->cmd->__invoke('migration', 'Test_of_generate_migration');
6580
$this->assertEquals(Status::FAILURE, $status);
66-
81+
6782
$this->stderr->rewind();
6883
$actual = $this->stderr->fread(8192);
6984
$this->assertContains("Can't write to ", $actual);

0 commit comments

Comments
 (0)