-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathDbMigrations.php
More file actions
315 lines (274 loc) · 10.6 KB
/
DbMigrations.php
File metadata and controls
315 lines (274 loc) · 10.6 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
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
<?php
namespace Elimuswift\DbExporter;
use Config;
use File;
use Illuminate\Support\Str;
use Elimuswift\DbExporter\Exceptions\InvalidDatabaseException;
class DbMigrations extends DbExporter
{
/**
* Column data types.
*
* @var string
**/
protected $columns = [
'int' => 'integer',
'smallint' => 'smallInteger',
'bigint' => 'bigInteger',
'char ' => 'string',
'varchar' => 'string',
'float' => 'float',
'double' => 'double',
'decimal' => 'decimal',
'tinyint' => 'tinyInteger',
'date' => 'date',
'timestamp' => 'timestamp',
'datetime' => 'dateTime',
'longtext' => 'longText',
'mediumtext' => 'mediumText',
'text' => 'text',
'longblob' => 'binary',
'blob' => 'binary',
'enum' => 'enum',
'char' => 'char ',
'geometry' => 'geometry',
'time' => 'time',
'point' => 'point',
'polygon' => 'polygon',
'multipolygon' => 'muliPolygon',
'multilinestring' => 'multiLineString',
'mulitpoint' => 'multiPoint',
'mediumint' => 'mediumInteger',
'mac' => 'macAddress',
'json' => 'json',
'linestring' => 'lineString',
'geometrycollection' => 'geometryCollection',
'bool' => 'boolean',
'year' => 'year',
];
/**
* Primary key column types.
*
* @var array
**/
protected $primaryKeys = [
'bigint' => 'bigIncrements',
'int' => 'increments',
];
protected $schema;
protected $customDb = false;
public static $filePath;
protected $primaryKey;
protected $defaultLength;
protected $methodName;
/**
* File name for migration file.
*
* @var string
*/
public $filename;
/**
* Set the database name.
*
* @param string $database
* @throw InvalidDatabaseException
*/
public function __construct($database)
{
if (empty($database)) {
throw new InvalidDatabaseException('No database set in app/config/database.php');
}
$this->database = $database;
}
//end __construct()
/**
* Write the prepared migration to a file.
*/
public function write()
{
// Check if convert method was called before
// If not, call it on default DB
if (!$this->customDb) {
$this->convert();
}
$schema = $this->compile();
$absolutePath = Config::get('db-exporter.export_path.migrations');
$this->makePath($absolutePath);
$this->filename = date('Y_m_d_His').'_create_'.$this->database.'_database.php';
static::$filePath = $absolutePath."/{$this->filename}";
file_put_contents(static::$filePath, $schema);
return static::$filePath;
}
//end write()
/**
* Convert the database to migrations
* If none is given, use de DB from condig/database.php.
*
* @param null $database
*
* @return $this
*/
public function convert($database = null)
{
if (!is_null($database)) {
$this->database = $database;
$this->customDb = true;
}
$tables = $this->getTables();
// Loop over the tables
foreach ($tables as $key => $value) {
// Do not export the ignored tables
if (in_array($value['table_name'], static::$ignore)) {
continue;
}
$down = "Schema::dropIfExists('{$value['table_name']}');";
$up = "Schema::create('{$value['table_name']}', function(Blueprint $"."table) {\n";
$tableDescribes = $this->getTableDescribes($value['table_name']);
// Loop over the tables fields
foreach ($tableDescribes as $values) {
$para = strpos($values->Type, '(');
$type = $para > -1 ? substr($values->Type, 0, $para) : $values->Type;
$nullable = 'NO' == $values->Nullable ? '' : '->nullable()';
$default = empty($values->Default) || 'NULL' == $values->Default ? '' : "->default({$values->Default})";
$default = 'CURRENT_TIMESTAMP' == $values->Default || 'current_timestamp()' == $values->Default ? '->useCurrent()' : $default;
$unsigned = false === strpos($values->Type, 'unsigned') ? '' : '->unsigned()';
$this->hasDefaults($type, $values);
$this->methodName = $this->columnType($type);
if ('PRI' == $values->Key) {
$this->primaryKey = '->primary()';
if ($methodName = $this->columnType($values->Data_Type, 'primaryKeys') && 'auto_increment' == $values->Extra) {
$this->primaryKey = '->autoIncrement()';
}
}
$up .= ' $'."table->{$this->methodName}('{$values->Field}'{$this->defaultLength}){$this->primaryKey}{$nullable}{$default}{$unsigned};\n";
$this->unsetData();
}//end foreach
$tableIndexes = (array) $this->getTableIndexes($value['table_name']);
if (!is_null($tableIndexes) && count($tableIndexes)) {
foreach ($tableIndexes as $index) {
if (Str::endsWith(@$index['Key_name'], '_index')) {
$up .= ' $'."table->index('".$index['Column_name']."');\n";
}
}
}
$up .= " });\n\n";
$Constraint = $ConstraintDown = '';
/*
* @var array
*/
$tableConstraints = $this->getTableConstraints($value['table_name']);
if (!is_null($tableConstraints) && $tableConstraints->count()) {
$Constraint = $ConstraintDown = "
Schema::table('{$value['table_name']}', function(Blueprint $"."table) {\n";
$tables = [];
foreach ($tableConstraints as $foreign) {
if (!in_array($foreign->Field, $tables)) {
$field = "{$foreign->Table}_{$foreign->Field}_foreign";
$ConstraintDown .= ' $'."table->dropForeign('".$field."');\n";
$Constraint .= ' $'."table->foreign('".$foreign->Field."')->references('".$foreign->References."')->on('".$foreign->ON."')->onDelete('".$foreign->onDelete."');\n";
$tables[$foreign->Field] = $foreign->Field;
}
}
$Constraint .= " });\n\n";
$ConstraintDown .= " });\n\n";
}
$this->schema[$value['table_name']] = [
'up' => $up,
'constraint' => $Constraint,
'constraint_down' => $ConstraintDown,
'down' => $down,
];
}//end foreach
return $this;
}
//end convert()
public function columnType($type, $columns = 'columns', $method = '')
{
return array_key_exists($type, $this->{$columns}) ? $this->{$columns}[$type] : $method;
}
//end columnType()
/**
* Compile the migration into the base migration file
* TODO use a template with seacrh&replace.
*
* @return string
*/
protected function compile($null = null)
{
$upSchema = '';
$downSchema = '';
$upConstraint = '';
$downConstraint = '';
// prevent of failure when no table
if (!is_null($this->schema) && is_array($this->schema)) {
foreach ($this->schema as $name => $values) {
// check again for ignored tables
if (in_array($name, self::$ignore)) {
continue;
}
$upSchema .= "
/**
* Migration schema for table {$name}
*
*/
{$values['up']}";
$upConstraint .= "
{$values['constraint']}";
$downConstraint .= "
{$values['constraint_down']}";
$downSchema .= "
{$values['down']}";
}
}//end if
// Grab the template
$template = File::get(__DIR__.'/stubs/migration.stub');
// Replace the classname
$template = str_replace('{{name}}', 'Create'.ucfirst(Str::camel($this->database)).'Database', $template);
// Replace the up and down values
$template = str_replace('{{up}}', $upSchema, $template);
$template = str_replace('{{down}}', $downSchema, $template);
$template = str_replace('{{upCon}}', $upConstraint, $template);
$template = str_replace('{{downCon}}', $downConstraint, $template);
return $template;
}
/**
* summary.
*
* @author
*/
public function hasDefaults($type, $column)
{
if ($hasSize = strpos($column->Type, '(')) {
$values = substr($column->Type, ($hasSize + 1), -1);
switch ($type) {
case 'enum':
$this->defaultLength = ', array('.$values.')';
break;
case 'char':
case 'varchar':
case 'text':
case 'mediumtext':
case 'longtext':
$this->defaultLength = ', '.$column->Length;
break;
case 'double':
case 'float':
case 'decimal':
$this->defaultLength = ", $column->Precision, $column->Scale";
break;
}
}
}
/**
* summary.
*
* @author
*/
protected function unsetData()
{
$this->primaryKey = null;
$this->methodName = null;
$this->defaultLength = null;
}
//end compile()
}//end class