-
Notifications
You must be signed in to change notification settings - Fork 17
/
EMigrateCommand.php
516 lines (462 loc) · 14.9 KB
/
EMigrateCommand.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
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
<?php
/**
* EMigrateCommand manages the database migrations.
*
* This class is an extension to yiis db migration command.
*
* It adds the following features:
* - module support
* you can create migrations in different modules
* so you are able to disable modules and also having their
* database tables removed/never set up
* yiic migrate to m000000_000000 --module=examplemodule
*
* - module dependencies (planned, not yet implemented)
*
* @link http://www.yiiframework.com/extension/extended-database-migration/
* @link http://www.yiiframework.com/doc/guide/1.1/en/database.migration
* @author Carsten Brandt <mail@cebe.cc>
* @version 0.8.0-dev
*/
Yii::import('system.cli.commands.MigrateCommand');
require_once(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'EDbMigration.php');
/**
* EMigrateCommand manages the database migrations.
*
* @property array|null $modulePaths list of all modules
* @property array $enabledModulePaths list of all enabled modules
* @property array $disabledModules list of all disabled modules names
*
* @author Carsten Brandt <mail@cebe.cc>
* @version 0.7.1
*/
class EMigrateCommand extends MigrateCommand
{
/**
* @var string|null the current module(s) to use for current command (comma separated list)
* defaults to null which means all modules are used
* examples:
* --module=core
* --module=core,user,admin
*/
public $module;
/**
* @var string the application core is handled as a module named 'core' by default
*/
public $applicationModuleName = 'core';
/**
* @var string delimiter for modulename and migration name for display
*/
public $moduleDelimiter = ': ';
/**
* @var string subdirectory to use for migrations in Yii alias format
* this is only used if you do not set modulePath explicitly {@see setModulePaths()}
*/
public $migrationSubPath = 'migrations';
/**
* @var array|null list of all modules
* @see getModulePaths()
* @see setModulePaths()
*/
private $_modulePaths = null;
private $_runModulePaths = null; // modules for current run
/**
* @var array
* @see getDisabledModules()
* @see setDisabledModules()
*/
private $_disabledModules = array();
/**
* @return array list of all modules
*/
public function getModulePaths()
{
if ($this->_modulePaths === null) {
$this->_modulePaths = array();
foreach(Yii::app()->modules as $module => $config) {
if (is_array($config)) {
$alias = 'application.modules.' . $module . '.' . ltrim($this->migrationSubPath, '.');
if (isset($config['class'])) {
Yii::setPathOfAlias(
$alias,
dirname(Yii::getPathOfAlias($config['class'])) . '/' .
str_replace('.', '/', ltrim($this->migrationSubPath, '.'))
);
} elseif (isset($config['basePath'])) {
Yii::setPathOfAlias(
$alias,
$config['basePath'] . '/' .
str_replace('.', '/', ltrim($this->migrationSubPath, '.'))
);
}
$this->_modulePaths[$module] = $alias;
$path = Yii::getPathOfAlias($alias);
if ($path === false || !is_dir($path)) {
$this->_disabledModules[] = $module;
}
} else {
$this->_modulePaths[$config] = 'application.modules.' . $config . '.' . ltrim($this->migrationSubPath, '.');
}
}
}
// add a pseudo-module 'core'
$this->_modulePaths[$this->applicationModuleName] = $this->migrationPath;
$path = Yii::getPathOfAlias($this->migrationPath);
if ($path === false || !is_dir($path)) {
$this->_disabledModules[] = $this->applicationModuleName;
}
return $this->_modulePaths;
}
/**
* @var array|null list of all modules
* If set to null, which is default, yii applications module config will be used
* If modules are taken from yii application config, all entries will be
* 'moduleName' => 'application.modules.<moduleName>.migrations',
* You can change the subpath name by setting {@see migrationSubPath} which is 'migrations' per default.
* If 'class' or 'basePath' are set in module config the above path alias is
* adjusted to class/basePath with {@see Yii::setPathOfAlias()}.
*
* example:
* array(
* 'moduleName' => 'application.modules.moduleName.db.migrations',
* )
*/
public function setModulePaths($modulePaths)
{
$this->_modulePaths = $modulePaths;
}
/**
* @return array list of all disabled modules names
*/
public function getDisabledModules()
{
// make sure modules are initialized
$this->getModulePaths();
foreach($this->_disabledModules as $module) {
if (!array_key_exists($module, $this->modulePaths)) {
unset($this->_disabledModules[$module]);
}
}
return array_unique($this->_disabledModules);
}
/**
* @param array $modules list of all disabled modules names
* you can add modules here to temporarily disable them
* array(
* 'examplemodule1',
* 'examplemodule2',
* ...
* )
*/
public function setDisabledModules($modules)
{
$this->_disabledModules = is_array($modules) ? $modules : array();
}
/**
* @return array list of all enabled modules
*/
public function getEnabledModulePaths()
{
$modules = $this->getModulePaths();
foreach($this->getDisabledModules() as $module) {
unset($modules[$module]);
}
return $modules;
}
/**
* prepare paths before any action
*
* @param $action
* @param $params
* @return bool
*/
public function beforeAction($action, $params)
{
$tmpMigrationPath = $this->migrationPath;
$this->migrationPath = 'application';
if (parent::beforeAction($action, $params)) {
$this->migrationPath = $tmpMigrationPath;
echo "extended with EMigrateCommand by cebe <mail@cebe.cc>\n\n";
echo "Active database component (connectionString):\n ".Yii::app()->{$this->connectionID}->connectionString."\n\n";
// check --module parameter
if ($action == 'create' && !is_null($this->module)) {
$this->usageError('create command can not be called with --module parameter!');
}
if (!is_null($this->module) && !is_string($this->module)) {
$this->usageError('parameter --module must be a comma seperated list of modules or a single module name!');
}
// inform user about disabled modules
if (!empty($this->disabledModules)) {
echo "The following modules are disabled: " . implode(', ', $this->disabledModules) . "\n";
}
// only add modules that are desired by command
$modules = false;
if ($this->module !== null) {
$modules = explode(',', $this->module);
// error if specified module does not exist
foreach ($modules as $module) {
if (in_array($module, $this->disabledModules)) {
echo "\nError: module '$module' is disabled!\n\n";
exit(1);
}
if (!isset($this->enabledModulePaths[$module])) {
echo "\nError: module '$module' is not available!\n\n";
exit(1);
}
}
echo "Current call is limited to module" . (count($modules)>1 ? "s" : "") . ": " . implode(', ', $modules) . "\n";
}
echo "\n";
// initialize modules
foreach($this->getEnabledModulePaths() as $module => $pathAlias) {
if ($modules === false || in_array($module, $modules)) {
Yii::import($pathAlias . '.*');
$this->_runModulePaths[$module] = $pathAlias;
}
}
return true;
}
return false;
}
public function actionCreate($args)
{
// if module is given adjust path
if (count($args)==2) {
if (!isset($this->modulePaths[$args[0]])) {
echo "\nError: module '{$args[0]}' is not available!\n\n";
return 1;
}
$this->migrationPath = Yii::getPathOfAlias($this->modulePaths[$args[0]]);
$args = array($args[1]);
} else {
$this->migrationPath = Yii::getPathOfAlias($this->modulePaths[$this->applicationModuleName]);
}
if (!is_dir($this->migrationPath)) {
echo "\nError: '{$this->migrationPath}' does not exist or is not a directory!\n\n";
return 1;
}
return parent::actionCreate($args);
}
public function actionUp($args)
{
$this->_scopeAddModule = true;
$exitCode = parent::actionUp($args);
$this->_scopeAddModule = false;
return $exitCode;
}
public function actionDown($args)
{
$this->_scopeAddModule = true;
$exitCode = parent::actionDown($args);
$this->_scopeAddModule = false;
return $exitCode;
}
public function actionTo($args)
{
$this->_scopeAddModule = false;
$exitCode = parent::actionTo($args);
$this->_scopeAddModule = true;
return $exitCode;
}
public function actionMark($args)
{
// migrations that need to be updated after command
$migrations = $this->getNewMigrations();
// run mark action
$this->_scopeAddModule = false;
$exitCode = parent::actionMark($args);
$this->_scopeAddModule = true;
// update migration table with modules
/** @var CDbCommand $command */
$command = $this->getDbConnection()->createCommand()
->select('version')
->from($this->migrationTable)
->where('module IS NULL');
foreach($command->queryColumn() as $version) {
$module = null;
foreach($migrations as $migration) {
list($module, $migration) = explode($this->moduleDelimiter, $migration);
if ($migration == $version) {
break;
}
}
$this->ensureBaseMigration($module);
$this->getDbConnection()->createCommand()->update(
$this->migrationTable,
array('module' => $module),
'version=:version',
array(':version' => $version)
);
}
return $exitCode;
}
protected function instantiateMigration($class)
{
require_once($class.'.php');
$migration=new $class;
$migration->setDbConnection($this->getDbConnection());
if ($migration instanceof EDbMigration) {
/** @var EDbMigration $migration */
$migration->setCommand($this);
}
return $migration;
}
// set to not add modules when getHistory is called for getNewMigrations
private $_scopeNewMigrations = false;
private $_scopeAddModule = true;
protected function getNewMigrations()
{
$this->_scopeNewMigrations = true;
$migrations = array();
// get new migrations for all new modules
foreach($this->_runModulePaths as $module => $path)
{
$this->migrationPath = Yii::getPathOfAlias($path);
foreach(parent::getNewMigrations() as $migration) {
if ($this->_scopeAddModule) {
$migrations[$migration] = $module.$this->moduleDelimiter.$migration;
} else {
$migrations[$migration] = $migration;
}
}
}
$this->_scopeNewMigrations = false;
ksort($migrations);
return array_values($migrations);
}
protected function getMigrationHistory($limit)
{
/** @var CDbConnection $db */
$db=$this->getDbConnection();
// avoid getTable trying to hit a db cache and die in endless loop
$db->schemaCachingDuration = 0;
Yii::app()->coreMessages->cacheID = false;
if ($db->schema->getTable($this->migrationTable)===null)
{
echo 'Creating migration history table "'.$this->migrationTable.'"...';
$db->createCommand()->createTable($this->migrationTable, array(
'version'=>'string NOT NULL PRIMARY KEY',
'apply_time'=>'integer',
'module'=>'VARCHAR(32)',
));
echo "done.\n";
}
if ($this->_scopeNewMigrations || !$this->_scopeAddModule) {
$select = "version AS version_name, apply_time";
$params = array();
} else {
/*
* switch concat functions for different db systems
* please let me know if your system is not switched
* correctly here. File a bug here:
* https://github.com/yiiext/migrate-command/issues
*/
switch ($db->getDriverName())
{
case 'mysql':
$select = "CONCAT(module, :delimiter, version) AS version_name, apply_time";
break;
case 'mssql': // http://msdn.microsoft.com/en-us/library/aa276862%28v=sql.80%29.aspx
case 'sqlsrv':
case 'cubrid': // http://www.cubrid.org/manual/840/en/Concatenation%20Operator
$select = "(module + :delimiter + version) AS version_name, apply_time";
break;
default: // SQL-ANSI default: sqlite, firebird, ibm, informix, oci, pgsql, sqlite, sqlite2
// not sure what to do with odbc
$select = "(module || :delimiter || version) AS version_name, apply_time";
}
$params = array(':delimiter' => $this->moduleDelimiter);
}
$command = $db->createCommand()
->select($select)
->from($this->migrationTable)
->order('version DESC')
->limit($limit);
if (!is_null($this->module)) {
$criteria = new CDbCriteria();
$criteria->addInCondition('module', explode(',', $this->module));
$command->where = $criteria->condition;
$params += $criteria->params;
}
return CHtml::listData($command->queryAll(true, $params), 'version_name', 'apply_time');
}
/**
* create base migration for module if none exists
*
* @param $module
* @return void
*/
protected function ensureBaseMigration($module)
{
$baseName = self::BASE_MIGRATION . '_' . $module;
/** @var CDbConnection $db */
$db = $this->getDbConnection();
if (!$db->createCommand()->select('version')
->from($this->migrationTable)
->where('module=:module AND version=:version')
->queryRow(true, array(':module'=>$module, 'version'=>$baseName)))
{
$db->createCommand()->insert(
$this->migrationTable,
array(
'version'=>$baseName,
'apply_time'=>time(),
'module'=>$module,
)
);
}
}
protected function migrateUp($class)
{
$module = $this->applicationModuleName;
// remove module if given
if (($pos = mb_strpos($class, $this->moduleDelimiter)) !== false) {
$module = mb_substr($class, 0, $pos);
$class = mb_substr($class, $pos + mb_strlen($this->moduleDelimiter));
}
$this->ensureBaseMigration($module);
if (mb_strpos($class, self::BASE_MIGRATION) === 0) {
return;
}
if (($ret = parent::migrateUp($class)) !== false) {
// add module information to migration table
$this->getDbConnection()->createCommand()->update(
$this->migrationTable,
array('module'=>$module),
'version=:version',
array(':version' => $class)
);
}
return $ret;
}
protected function migrateDown($class)
{
// remove module if given
if (($pos = mb_strpos($class, $this->moduleDelimiter)) !== false) {
$class = mb_substr($class, $pos + mb_strlen($this->moduleDelimiter));
}
if (mb_strpos($class, self::BASE_MIGRATION) !== 0) {
return parent::migrateDown($class);
}
}
public function getHelp()
{
return parent::getHelp() . <<<EOD
EXTENDED USAGE EXAMPLES (with modules)
for every action except create you can specify the modules to use
with the parameter --module=<modulenames>
where <modulenames> is a comma seperated list of module names (or a single name)
* yiic migrate create modulename create_user_table
Creates a new migration named 'create_user_table' in module 'modulename'.
all other commands work exactly as described above.
EOD;
}
protected function getTemplate()
{
if ($this->templateFile!==null) {
return parent::getTemplate();
} else {
return str_replace('CDbMigration', 'EDbMigration', parent::getTemplate());
}
}
}