forked from jamband/yii2-schemadump
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SchemaDumpController.php
273 lines (228 loc) · 7.63 KB
/
SchemaDumpController.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
<?php
/*
* This file is part of yii2-schemadump.
*
* (c) Tomoki Morita <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace jamband\schemadump;
use Yii;
use yii\console\Exception;
use yii\console\Controller;
use yii\db\Connection;
/**
* Generate the migration code from database schema.
*/
class SchemaDumpController extends Controller
{
/**
* @inheritdoc
*/
public $defaultAction = 'create';
/**
* @var string a migration table name
*/
public $migrationTable = 'migration';
/**
* @var Connection|string the DB connection object or the application component ID of the DB connection.
*/
public $db = 'db';
/**
* @var array the column types
* @see \yii\db\Schema
*/
private $type = [
'pk' => 'Schema::TYPE_PK',
'bigpk' => 'Schema::TYPE_BIGPK',
'string' => 'Schema::TYPE_STRING',
'text' => 'Schema::TYPE_TEXT',
'smallint' => 'Schema::TYPE_SMALLINT',
'integer' => 'Schema::TYPE_INTEGER',
'bigint' => 'Schema::TYPE_BIGINT',
'float' => 'Schema::TYPE_FLOAT',
'decimal' => 'Schema::TYPE_DECIMAL',
'datetime' => 'Schema::TYPE_DATETIME',
'timestamp' => 'Schema::TYPE_TIMESTAMP',
'time' => 'Schema::TYPE_TIME',
'date' => 'Schema::TYPE_DATE',
'binary' => 'Schema::TYPE_BINARY',
'boolean' => 'Schema::TYPE_BOOLEAN',
'money' => 'Schema::TYPE_MONEY',
];
/**
* @inheritdoc
*/
public function options($actionID)
{
return array_merge(
parent::options($actionID),
['migrationTable', 'db']
);
}
/**
* @inheritdoc
*/
public function beforeAction($action)
{
if (parent::beforeAction($action)) {
if (is_string($this->db)) {
$this->db = Yii::$app->get($this->db);
}
if (!$this->db instanceof Connection) {
throw new Exception("The 'db' option must refer to the application component ID of a DB connection.");
}
return true;
}
return false;
}
/**
* Generates the 'createTable' code.
* @param string $schema the schema of the tables. Defaults to empty string, meaning the current or default schema name.
* @return integer the status of the action execution
*/
public function actionCreate($schema = '')
{
$stdout = '';
$tables = $this->db->schema->getTableSchemas($schema);
$offset = 0;
foreach ($tables as $table) {
if ($table->name === $this->migrationTable) {
continue;
}
$stdout .= "// $table->name\n";
$stdout .= "\$this->createTable('{{%$table->name}}', [\n";
foreach ($table->columns as $column) {
$stdout .= " '$column->name' => {$this->getSchemaType($column)} . \"{$this->otherDefinition($column)}\",\n";
}
if (!empty($table->primaryKey)) {
if (count($table->primaryKey) >= 2) {
$stdout .= " 'PRIMARY KEY (" . implode(', ', $table->primaryKey) . ")',\n";
} elseif (false === strpos($stdout, $this->type['pk'], $offset) && false === strpos($stdout, $this->type['bigpk'], $offset)) {
$stdout .= " 'PRIMARY KEY ({$table->primaryKey[0]})',\n";
}
}
$stdout .= "], \$this->tableOptions);\n\n";
$offset = mb_strlen($stdout, Yii::$app->charset);
}
foreach ($tables as $table) {
$stdout .= $this->generateForeignKey($table);
}
$this->stdout(strtr($stdout, [
' . ""' => '',
'" . "' => '',
]));
}
/**
* Generates the 'dropTable' code.
* @param string $schema the schema of the tables. Defaults to empty string, meaning the current or default schema name.
* @return integer the status of the action execution
*/
public function actionDrop($schema = '')
{
$stdout = '';
$tables = $this->db->schema->getTableSchemas($schema);
foreach ($tables as $table) {
if ($table->name === $this->migrationTable) {
continue;
}
$stdout .= "\$this->dropTable('{{%$table->name}}');";
if (!empty($table->foreignKeys)) {
$stdout .= " // fk: ";
foreach ($table->foreignKeys as $fk) {
foreach ($fk as $k => $v) {
if ($k === 0) {
continue;
}
$stdout .= "$k, ";
}
}
$stdout = substr($stdout, 0, -2);
}
$stdout .= "\n";
}
$this->stdout($stdout);
}
/**
* Returns the schema type.
* @param ColumnSchema[] $column
* @return string the schema type
*/
private function getSchemaType($column)
{
if ($column->autoIncrement && !$column->unsigned) {
return ($column->type === 'bigint') ? $this->type['bigpk'] : $this->type['pk'];
}
if ($column->dbType === 'tinyint(1)') {
return $this->type['boolean'];
}
if ($column->enumValues !== null) {
return "\"$column->dbType\"";
}
return $this->type[$column->type];
}
/**
* Returns the other definition.
* @param ColumnSchema[] $column
* @return string the other definition
*/
private function otherDefinition($column)
{
$definition = '';
if (
($column->size !== null && !$column->autoIncrement && $column->dbType !== 'tinyint(1)') ||
($column->size !== null && $column->unsigned)
) {
$definition .= "($column->size)";
}
if ($column->unsigned) {
$definition .= ' UNSIGNED';
}
if ($column->allowNull) {
$definition .= ' NULL';
} elseif (!$column->autoIncrement) {
$definition .= ' NOT NULL';
} elseif ($column->autoIncrement && $column->unsigned) {
$definition .= ' NOT NULL AUTO_INCREMENT';
}
if ($column->defaultValue instanceof \yii\db\Expression) {
$definition .= " DEFAULT $column->defaultValue";
} elseif ($column->defaultValue !== null) {
$definition .= " DEFAULT '$column->defaultValue'";
}
if ($column->comment !== '') {
$definition .= " COMMENT '$column->comment'";
}
return $definition;
}
/**
* Generates foreign key definition.
* @param TableSchema[] $table
* @return string foreign key definition
*/
private function generateForeignKey($table)
{
$stdout = '';
$foreignKeys = $table->foreignKeys;
if (empty($foreignKeys)) {
return $stdout;
}
$stdout = "// fk: $table->name\n";
foreach ($foreignKeys as $fk) {
$refTable = '';
$refColumns = '';
$columns = '';
foreach ($fk as $k => $v) {
if ($k === 0) {
$refTable = $v;
} else {
$columns = $k;
$refColumns = $v;
}
}
$stdout .= "\$this->addForeignKey('fk_{$table->name}_{$columns}', '{{%$table->name}}', '$columns', '{{%$refTable}}', '$refColumns');\n";
}
return "$stdout\n";
}
}