-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathConsoleController.php
60 lines (55 loc) · 1.79 KB
/
ConsoleController.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
<?php
namespace e282486518\migration;
use Yii;
use yii\console\Exception;
use yii\helpers\Console;
use yii\helpers\ArrayHelper;
use yii\console\controllers\MigrateController;
//use e282486518\migration\components\MigrateCreate;
/**
* This is just an example.
*/
class ConsoleController extends MigrateController
{
/**
* Creates a new migration. php yii migrate/backup all
*
* @param string $name
* @throws Exception if the name argument is invalid.
*/
public function actionBackup($name){
/* 所有数据表 */
$alltables = Yii::$app->db->createCommand('SHOW TABLE STATUS')->queryAll();
$alltables = array_map('array_change_key_case', $alltables);
$alltables = ArrayHelper::getColumn($alltables, 'name');
$name = trim($name,',');
if ($name == 'all') {
/* 备份所有数据 */
$tables = $alltables;
} else if(strpos($name, ',')){
/* 备份部分数据表 */
$tables = explode(',', $name);
} else {
/* 备份一个数据表 */
$tables = [$name];
}
/* 检查表是否存在 */
foreach ($tables as $table) {
if (!in_array($table,$alltables)) {
$this->stdout($table." table no find ...\n", Console::FG_RED);
die();
}
}
/* 创建migration */
foreach ($tables as $table) {
//$migrate = new MigrateCreate();
$migrate = Yii::createObject([
'class' => 'e282486518\migration\components\MigrateCreate',
'migrationPath' => '@app/migrations'
]);
$migrate->create($table);
unset($migrate);
}
$this->stdout("backup success.\n", Console::FG_GREEN);
}
}