forked from Hzhihua/yii2-dump
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMigration.php
177 lines (151 loc) · 5.3 KB
/
Migration.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
<?php
/**
* @Author: Hzhihua
* @Date: 17-9-7 12:18
* @Email [email protected]
*/
namespace io4n\dump;
use Yii;
use yii\db\Exception;
use yii\helpers\Console;
use io4n\dump\models\Output;
/**
* Migration class file.
* all migration file generated extends this file
* @property \yii\db\Transaction $_transaction
* @Author Hzhihua <[email protected]>
*/
class Migration extends \yii\db\Migration
{
/**
* enter 换行符号
*/
const ENTER = "\n";
/**
* @var string table additional options
*/
public $tableOptions = ' CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE=InnoDB ';
/**
* @var array record which sql run successfully
*/
protected $runSuccess = [];
/**
* @var \yii\db\Transaction save transaction for insert table data
*/
protected $_transaction = null;
/**
* @inheritdoc
*/
public function init()
{
parent::init();
if ($this->db->driverName === 'mysql') {
// https://stackoverflow.com/questions/766809/whats-the-difference-between-utf8-general-ci-and-utf8-unicode-ci
// use utf8mb4 may cause some errors that "Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes" for "ADD UNIQUE INDEX"
// you can use " ENGINE=InnoDB CHARACTER SET=utf8mb4 COLLATE=utf8mb4_unicode_ci " for mysql >= 5.7
$controllerMap = Yii::$app->controllerMap;
foreach ($controllerMap as $key => $value) {
if (
isset($value['tableOptions']) &&
isset($value['class']) &&
$value['class'] === 'io4n\dump\DumpController'
) {
$tableOptions = sprintf(
' %s ',
$controllerMap[$key]['tableOptions']
);
break;
}
}
isset($tableOptions) || $this->tableOptions = ' ENGINE=InnoDB CHARACTER SET=utf8 COLLATE=utf8_unicode_ci ';
} else {
throw new Exception('Sorry, we only support mysql database.');
}
}
/**
* @return bool return true if applying success or throw new exception of db
* @throws \yii\db\Exception
*/
public function up()
{
try {
Output::stdout('*** running safeUp' . self::ENTER, 0, Console::FG_YELLOW);
$this->safeUp();
} catch (Exception $e) {
try {
Output::stdout(self::ENTER . '*** running safeDown' . self::ENTER, 0, Console::FG_YELLOW);
$this->safeDown();
} catch (Exception $_e) {
}
Output::stdout(self::ENTER . '*** Error: ', 1, Console::FG_RED);
throw new Exception($e->getMessage(), $e->errorInfo, 1);
}
return null;
}
/**
* 添加自动增长
* @param string $table 表名{{%tableName}}
* @param string $column 字段名称
* @param string $columnType 字段类型
* @param boolean $property 字段属性
* @param int $auto_increment 自动增长字段的启始值
* @return array
*/
public function addAutoIncrement($table, $column, $columnType, $property = null, $auto_increment = 0)
{
$auto_increment = (int) $auto_increment;
$sql = $this->db->getQueryBuilder()->alterColumn($table, $column, $columnType);
(null !== $property) && $sql .= " $property ";
$sql .= "NOT NULL AUTO_INCREMENT";
(0 !== $auto_increment) && $sql .= ", AUTO_INCREMENT = {$auto_increment}";
$time = $this->beginCommand($sql);
$this->db->createCommand()->setSql($sql)->execute();
$this->endCommand($time);
return [
'table_name' => $table,
'column_name' => $column,
'column_type' => $columnType,
'property' => $property,
'auto_increment' => $auto_increment,
];
}
/**
* 删除自动增长
* @param string $table 表名{{%tableName}}
* @param string $column 字段名称
* @param string $columnType 字段类型
* @param boolean $property 字段属性
*/
public function dropAutoIncrement($table, $column, $columnType, $property = null)
{
$tip = "drop auto_increment on {$table}";
$sql = $this->db->getQueryBuilder()->alterColumn($table, $column, $columnType);
(null !== $property) && $sql .= " $property ";
$sql .= "NOT NULL";
$time = $this->beginCommand($tip);
$this->db->createCommand()->setSql($sql)->execute();
$this->endCommand($time);
}
/**
* 删除主键
* @param string $name
* @param string $table
*/
public function dropPrimaryKey($name, $table)
{
$tip = "drop primary key on {$table}";
$sql = 'ALTER TABLE ' . $this->db->quoteTableName($table)
. ' DROP PRIMARY KEY';
$time = $this->beginCommand($tip);
$this->db->createCommand()->setSql($sql)->execute();
$this->endCommand($time);
}
public function tinyint($length = null)
{
return $this->getDb()->getSchema()->createColumnSchemaBuilder('tinyint', $length);
}
public function longtext()
{
return $this->getDb()->getSchema()->createColumnSchemaBuilder('longtext');
}
}