-
Notifications
You must be signed in to change notification settings - Fork 1
/
EMigrateCodegenCommand.php
206 lines (181 loc) · 4.44 KB
/
EMigrateCodegenCommand.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
<?php
Yii::import('system.cli.commands.MigrateCommand');
class EMigrateCodegenCommand extends MigrateCommand
{
public $templatePartiallyImplementedFile;
public $createTableOptions = 'ENGINE=InnoDB CHARSET=utf8';
public function actionCreate($args)
{
if(isset($args[0]))
{
$name=$args[0];
}
else
{
$this->usageError('Please provide the name of the new migration.');
}
$data = $this->parseMigrationName($name);
$name = 'm'.gmdate('ymd_His').'_'.$name;
$file = $this->migrationPath.DIRECTORY_SEPARATOR.$name.'.php';
//it's standart command
if (! $data)
{
$content=strtr($this->getTemplate(), array('{ClassName}'=>$name));
}
//it's partially implemented command
else
{
$this->addCommandLineParams($data, $args);
$template = $this->getPartiallyImplementedTemplate();
$content = strtr($template, array(
'{ClassName}' => $name,
'{CodeForUp}' => $this->getCodeForUp($data),
'{CodeForDown}' => $this->getCodeForDown($data)
));
}
if($this->confirm("Create new migration '$file'?"))
{
file_put_contents($file, $content);
echo "New migration created successfully.\n";
}
}
protected function parseMigrationName($name)
{
//it's CREATE TABLE or DROP TABLE
preg_match('/^(create|drop)_table_(.+)$/i', $name, $matches);
if ($matches)
{
return array(
'command' => $matches[1] == 'create' ? 'createTable' : 'dropTable',
'tableName' => $matches[2],
);
}
//it's ADD COLUMN or DROP COLUMN
preg_match('/^(add|drop)_column_(.+)_in_(.+)$/i', $name, $matches);
if ($matches)
{
return array(
'command' => $matches[1] == 'add' ? 'addColumn' : 'dropColumn',
'columnName' => $matches[2],
'tableName' => $matches[3],
);
}
return array();
}
protected function addCommandLineParams(&$data, $args)
{
switch ($data['command']) {
case 'createTable':
$data['options'] = isset($args[1]) ? $args[1] : $this->createTableOptions;
break;
case 'addColumn':
$data['type'] = isset($args[1]) ? $args[1] : '';
break;
}
}
protected function getPartiallyImplementedTemplate()
{
if($this->templatePartiallyImplementedFile!==null)
{
return file_get_contents(Yii::getPathOfAlias($this->templateFile).'.php');
}
else
{
return
'<?php
class {ClassName} extends CDbMigration
{
public function up()
{
{CodeForUp}
}
public function down()
{
{CodeForDown}
}
/*
// Use safeUp/safeDown to do migration with transaction
public function safeUp()
{
}
public function safeDown()
{
}
*/
}
';
}
}
protected function getCodeForUp($data)
{
switch ($data['command']) {
case 'createTable':
return $this->getCodeForCreateTable($data);
break;
case 'dropTable':
return $this->getCodeForDropTable($data);
break;
case 'addColumn':
return $this->getCodeForAddColumn($data);
break;
case 'dropColumn':
return $this->getCodeForDropColumn($data);
break;
}
return '';
}
protected function getCodeForDown($data)
{
switch ($data['command']) {
case 'createTable':
return $this->getCodeForDropTable($data);
break;
case 'dropTable':
return $this->getCodeForCreateTable($data);
break;
case 'addColumn':
return $this->getCodeForDropColumn($data);
break;
case 'dropColumn':
return $this->getCodeForAddColumn($data);
break;
}
return '';
}
protected function getCodeForCreateTable($data)
{
$createTableTemplate =
'$this->createTable(\'{tableName}\', array(
), {options});';
return strtr($createTableTemplate, array(
'{tableName}' => $data['tableName'],
'{options}' => $data['options'] == 'null'
? $data['options']
: "'".$data['options']."'"
));
}
protected function getCodeForDropTable($data)
{
$dropTableTemplate = '$this->dropTable(\'{tableName}\');';
return strtr($dropTableTemplate, array(
'{tableName}' => $data['tableName']
));
}
protected function getCodeForAddColumn($data)
{
$addColumnTemplate = '$this->addColumn(\'{tableName}\', \'{columnName}\', \'{type}\');';
return strtr($addColumnTemplate, array(
'{tableName}' => $data['tableName'],
'{columnName}' => $data['columnName'],
'{type}' => $data['type']
));
}
protected function getCodeForDropColumn($data)
{
$dropColumnTemplate = '$this->dropColumn(\'{tableName}\', \'{columnName}\');';
return strtr($dropColumnTemplate, array(
'{tableName}' => $data['tableName'],
'{columnName}' => $data['columnName'],
));
}
}