Skip to content

Commit e1168c8

Browse files
committed
generate command support sequential type
1 parent f6efafc commit e1168c8

4 files changed

Lines changed: 116 additions & 13 deletions

File tree

src/Command/Generate/Migration.php

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ class Migration extends Command
2525
public function __construct(Context $context, Stdio $stdio, CI_Controller $ci)
2626
{
2727
parent::__construct($context, $stdio, $ci);
28-
$this->load->library('migration');
2928
$this->load->config('migration');
3029
}
3130

@@ -49,15 +48,26 @@ public function __invoke($type, $classname)
4948
$migration_type = $this->config->item('migration_type');
5049

5150
if ($migration_type === 'sequential') {
52-
$files = $this->migration->find_migrations();
53-
if (empty($files)) {
54-
$version = 0;
55-
} else {
56-
end($files);
57-
$version = key($files);
51+
52+
$migrations = array();
53+
54+
// fax max version
55+
foreach (glob($migration_path . '*_*.php') as $file) {
56+
$name = basename($file, '.php');
57+
58+
if (preg_match('/^\d{3}_(\w+)$/', $name)) {
59+
$number = sscanf($name, '%[0-9]+', $number) ? $number : '0';
60+
$migrations[] = $number;
61+
}
62+
}
63+
64+
$version = 0;
65+
66+
if ($migrations !== []) {
67+
$version = max($migrations);
5868

5969
// check max version
60-
if ($version == 999) {
70+
if ((int)$version === 999) {
6171
$this->stdio->errln(
6272
"<<red>>The version number is out of range<<reset>>"
6373
);
@@ -69,8 +79,6 @@ public function __invoke($type, $classname)
6979
$file_path = $migration_path . date('YmdHis') . '_' . $classname . '.php';
7080
}
7181

72-
$file_path = $migration_path . date('YmdHis') . '_' . $classname . '.php';
73-
7482
// check file exist
7583
if (file_exists($file_path)) {
7684
$this->stdio->errln(
@@ -83,8 +91,7 @@ public function __invoke($type, $classname)
8391
foreach (glob($migration_path . '*_*.php') as $file) {
8492
$name = basename($file, '.php');
8593

86-
// use date('YmdHis') so...
87-
if (preg_match('/^\d{14}_(\w+)$/', $name, $match)) {
94+
if (preg_match($migration_type === 'timestamp' ? '/^\d{14}_(\w+)$/' : '/^\d{3}_(\w+)$/', $name, $match)) {
8895
if (strtolower($match[1]) === strtolower($classname)) {
8996
$this->stdio->errln(
9097
"<<red>>The Class \"$classname\" already exists<<reset>>"

tests/Command/GenerateTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ public function test_migration_generate_class_exist()
6464
$status = $this->cmd->__invoke('migration', 'Test_of_generate_migration');
6565
$status = $this->cmd->__invoke('migration', 'Test_of_generate_migration');
6666
$this->assertEquals(Status::FAILURE, $status);
67+
$status = $this->cmd->__invoke('migration', 'Test_of_Generate_Migration');
68+
$this->assertEquals(Status::FAILURE, $status);
6769

6870
foreach (glob($migration_path . '*_Test_of_generate_migration.php') as $file) {
6971
unlink($file);
@@ -79,7 +81,7 @@ public function test_migration_generate_sequential()
7981
$this->assertEquals(Status::SUCCESS, $status);
8082

8183
foreach (glob($migration_path . '*_Test_of_generate_migration.php') as $file) {
82-
$this->assertContains('001_Test_of_generate_migration', $file);
84+
$this->assertContains('003_Test_of_generate_migration', $file);
8385
unlink($file);
8486
}
8587
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
3+
/**
4+
* Migration: Create_bbs
5+
*
6+
* Created by: Cli for CodeIgniter <https://github.com/kenjis/codeigniter-cli>
7+
* Created on: 2015/04/29 08:10:27
8+
*/
9+
class Migration_Create_bbs extends CI_Migration {
10+
11+
public function up() {
12+
$this->dbforge->add_field([
13+
'id' => [
14+
'type' => 'INT',
15+
'constraint' => 11,
16+
'auto_increment' => TRUE
17+
],
18+
'name' => [
19+
'type' => 'VARCHAR',
20+
'constraint' => '64',
21+
],
22+
'email' => [
23+
'type' => 'VARCHAR',
24+
'constraint' => '64',
25+
'null' => TRUE,
26+
],
27+
'subject' => [
28+
'type' => 'VARCHAR',
29+
'constraint' => '128',
30+
'null' => TRUE,
31+
],
32+
'body' => [
33+
'type' => 'TEXT',
34+
'null' => TRUE,
35+
],
36+
'password' => [
37+
'type' => 'VARCHAR',
38+
'constraint' => '32',
39+
'null' => TRUE,
40+
],
41+
'ip_address' => [
42+
'type' => 'VARCHAR',
43+
'constraint' => '39',
44+
'null' => TRUE,
45+
],
46+
]);
47+
48+
$this->dbforge->add_key('id', TRUE);
49+
$this->dbforge->create_table('bbs');
50+
}
51+
52+
public function down() {
53+
$this->dbforge->drop_table('bbs');
54+
}
55+
56+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
/**
4+
* Migration: Create_captcha
5+
*
6+
* Created by: Cli for CodeIgniter <https://github.com/kenjis/codeigniter-cli>
7+
* Created on: 2015/04/29 10:38:53
8+
*/
9+
class Migration_Create_captcha extends CI_Migration {
10+
11+
public function up() {
12+
$this->dbforge->add_field([
13+
'captcha_id' => [
14+
'type' => 'BIGINT',
15+
'constraint' => 13,
16+
'unsigned' => TRUE,
17+
'auto_increment' => TRUE
18+
],
19+
'captcha_time' => [
20+
'type' => 'INT',
21+
'constraint' => 10,
22+
'unsigned' => TRUE,
23+
],
24+
'word' => [
25+
'type' => 'VARCHAR',
26+
'constraint' => '20',
27+
],
28+
]);
29+
$this->dbforge->add_key('captcha_id', TRUE);
30+
$this->dbforge->add_key('word');
31+
$this->dbforge->create_table('captcha');
32+
}
33+
34+
public function down() {
35+
$this->dbforge->drop_table('captcha');
36+
}
37+
38+
}

0 commit comments

Comments
 (0)