-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathModuleMigrateController.php
More file actions
executable file
·282 lines (256 loc) · 8.35 KB
/
Copy pathModuleMigrateController.php
File metadata and controls
executable file
·282 lines (256 loc) · 8.35 KB
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
<?php
/**
* ModuleMigrateController class file
* @copyright Copyright (c) 2014 Galament
* @license http://www.yiiframework.com/license/
*/
namespace bariew\moduleMigration;
use yii\console\Application;
use yii\console\controllers\MigrateController;
use yii\console\Exception;
use Yii;
/**
* Runs migrations from module /migrations folder.
*
* @author Pavel Bariev <bariew@yandex.ru>
*/
class ModuleMigrateController extends MigrateController
{
/**
* @var array module base paths
*/
public $allMigrationPaths = [];
/**
* @var array paths to migrations like [path => migrationName]
*/
public $migrationFiles = [];
public $dumpTemplateFile = '@bariew/moduleMigration/dumpTemplate.php';
protected $sourceMigrationPath = null;
/**
* @inheritdoc
*/
public function beforeAction($action)
{
if (!parent::beforeAction($action)) {
return false;
}
if ($action->id !== 'create' && is_object($this->db->schemaCache)) {
$this->db->schemaCache->flush();
}
$this->sourceMigrationPath = $this->migrationPath;
if (is_array($this->migrationPath)) {
$this->allMigrationPaths = array_merge($this->migrationPath, $this->allMigrationPaths);
} else {
$this->allMigrationPaths[0] = $this->migrationPath;
}
$this->attachModuleMigrations();
$this->setMigrationFiles();
return true;
}
/**
* @inheritdoc
*/
protected function getNewMigrations()
{
$result = [];
foreach ($this->allMigrationPaths as $paths) {
if (!is_array($paths)) {
$paths = [$paths];
}
foreach ($paths as $path) {
$this->migrationPath = $path;
if (!file_exists($path)) {
continue;
}
$result = array_merge($result, parent::getNewMigrations());
}
}
$this->migrationPath = $this->sourceMigrationPath;
sort($result);
return $result;
}
/**
* gets path to migration file
* @param string $name migration name
* @param bool|string $path module migrations base path
* @return string path to migration file
*/
protected function getMigrationFile($name, $path = false)
{
$path = $path ? $path : $this->migrationPath;
return $path . DIRECTORY_SEPARATOR . $name . '.php';
}
/**
* @inheritdoc
*/
protected function createMigration($class)
{
if (!$file = array_search($class, $this->migrationFiles)) {
return false;
}
require_once($file);
return new $class(['db' => $this->db]);
}
/**
* creates $allMigrationPaths attribute from module base paths
*/
protected function attachModuleMigrations()
{
foreach (Yii::$app->modules as $name => $config) {
$basePath = Yii::$app->getModule($name)->basePath;
$path = $basePath . DIRECTORY_SEPARATOR. 'migrations';
if (isset($this->migrationPath[0]) && $this->migrationPath[0] == $path) {
continue;
}
if (file_exists($path) && !is_file($path)) {
$this->allMigrationPaths[$name] = $path;
}
}
}
/**
* Creates $migrationFiles array
* @return array list of migrations like [path=>migrationName]
*/
protected function setMigrationFiles()
{
$result = [];
foreach ($this->allMigrationPaths as $paths) {
if (!is_array($paths)) {
$paths = [$paths];
}
foreach ($paths as $path) {
if (!file_exists($path) || is_file($path)) {
continue;
}
$handle = opendir($path);
while (($file = readdir($handle)) !== false) {
if ($file === '.' || $file === '..') {
continue;
}
$filePath = $path . DIRECTORY_SEPARATOR . $file;
if (preg_match('/^(m(\d{6}_\d{6})_.*?)\.php$/', $file, $matches) && is_file($filePath)) {
$result[$filePath] = $matches[1];
}
}
closedir($handle);
}
}
return $this->migrationFiles = $result;
}
/**
* Migrates current module up.
* @param string $module module name.
* @param string|integer $limit migrations limit.
*/
public function actionModuleUp($module, $limit = 'all')
{
$this->setModuleMigrationPaths($module);
parent::actionUp($limit);
}
/**
* Migrates current module down.
* @param string $module module name.
* @param string|integer $limit migrations limit.
*/
public function actionModuleDown($module, $limit = 'all')
{
$this->setModuleMigrationPaths($module);
parent::actionDown($limit);
}
/**
* Generate any table data migration into @app/migrations folder
* @param string $table name of table to dump
* @param int $remove truncate table before migrate/up
* @throws Exception if no data found
*/
public function actionDataDump($table, $remove = 1)
{
$className = 'm' . gmdate('ymd_His') . '_' . $table . '_dump';
if (!$data = $this->db->createCommand("SELECT * FROM {{{$table}}}")->queryAll()) {
throw new Exception("No data found");
}
$columns = DbHelper::dataColumns($data);
$sql = DbHelper::insertUpdate($table, $columns, $data)->getSql();
$file = $this->migrationPath . DIRECTORY_SEPARATOR . $className . '.php';
$content = $this->renderFile(Yii::getAlias($this->dumpTemplateFile), compact(
'className', 'remove', 'sql', 'table'
));
file_put_contents($file, $content);
echo "New migration created successfully.\n";
}
/**
* Creates a new migration.
*
* This command creates a new migration using the available migration template.
* After using this command, developers should modify the created migration
* skeleton by filling up the actual migration logic.
*
* ~~~
* yii migrate/create create_user_table
* yii migrate/create create_user_table module_name
* ~~~
*
* @param string $name the name of the new migration. This should only contain
* letters, digits and/or underscores.
* @param string $module name of module for the new migration. Module must exist
* and contain 'migrations' path
* @throws Exception if the name argument is invalid.
*/
public function actionCreate($name, $module = null)
{
if (!empty($module)) {
if (empty($this->allMigrationPaths[$module])) {
throw new Exception("Module $module does not exist or does not contains 'migrations' directory");
}
$this->migrationPath = $this->allMigrationPaths[$module];
}
parent::actionCreate($name);
}
/**
* Sets modules array - leaves only module migrations.
* @param string $module module name.
*/
protected function setModuleMigrationPaths($module)
{
$paths = [Yii::getAlias('@app/runtime/tmp')];
if (isset($this->allMigrationPaths[$module])) {
$paths[$module] = $this->allMigrationPaths[$module];
}
$this->allMigrationPaths = $paths;
$this->setMigrationFiles();
}
/**
* @inheritdoc
*/
protected function getMigrationHistory($limit)
{
$history = parent::getMigrationHistory($limit);
foreach ($history as $name => $time) {
if (!$this->migrationExists($name)) {
unset($history[$name]);
}
}
return $history;
}
/**
* Checks if a migration exists
* @param string $name the name of the migration to check for.
* @return bool
*/
protected function migrationExists($name)
{
return in_array($name, $this->migrationFiles);
}
public function stderr($string)
{
return Yii::$app instanceof Application
? parent::stderr($string)
: true;
}
public function stdout($string)
{
return Yii::$app instanceof Application
? parent::stdout($string)
: true;
}
}