|
1
|
|
|
<?php |
|
2
|
|
|
namespace Elimuswift\DbExporter; |
|
3
|
|
|
|
|
4
|
|
|
use Config; |
|
5
|
|
|
use File; |
|
6
|
|
|
use Illuminate\Support\Str; |
|
7
|
|
|
use Elimuswift\DbExporter\Exceptions\InvalidDatabaseException; |
|
8
|
|
|
|
|
9
|
|
|
|
|
10
|
|
|
class DbMigrations extends DbExporter |
|
11
|
|
|
{ |
|
12
|
|
|
|
|
13
|
|
|
|
|
14
|
|
|
protected $schema; |
|
15
|
|
|
|
|
16
|
|
|
protected $customDb = false; |
|
17
|
|
|
|
|
18
|
|
|
public static $filePath; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Set the database name |
|
22
|
|
|
* @param String $database |
|
23
|
|
|
* @throw InvalidDatabaseException |
|
24
|
|
|
*/ |
|
25
|
|
|
public function __construct($database) |
|
26
|
|
|
{ |
|
27
|
|
|
if (empty($database)) { |
|
28
|
|
|
throw new InvalidDatabaseException('No database set in app/config/database.php'); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
$this->database = $database; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Write the prepared migration to a file |
|
36
|
|
|
*/ |
|
37
|
|
|
public function write() |
|
38
|
|
|
{ |
|
39
|
|
|
// Check if convert method was called before |
|
40
|
|
|
// If not, call it on default DB |
|
41
|
|
|
if (!$this->customDb) { |
|
42
|
|
|
$this->convert(); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
$schema = $this->compile(); |
|
46
|
|
|
$filename = date('Y_m_d_His') . "_create_" . $this->database . "_database.php"; |
|
47
|
|
|
static::$filePath = Config::get('db-exporter.export_path.migrations') . "{$filename}"; |
|
48
|
|
|
|
|
49
|
|
|
file_put_contents(static::$filePath, $schema); |
|
50
|
|
|
|
|
51
|
|
|
return static::$filePath; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Convert the database to migrations |
|
56
|
|
|
* If none is given, use de DB from condig/database.php |
|
57
|
|
|
* @param null $database |
|
58
|
|
|
* @return $this |
|
59
|
|
|
*/ |
|
60
|
|
|
public function convert($database = null) |
|
61
|
|
|
{ |
|
62
|
|
|
if (!is_null($database)) { |
|
63
|
|
|
$this->database = $database; |
|
64
|
|
|
$this->customDb = true; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
$tables = $this->getTables(); |
|
68
|
|
|
|
|
69
|
|
|
// Loop over the tables |
|
70
|
|
|
foreach ($tables as $key => $value) { |
|
71
|
|
|
// Do not export the ignored tables |
|
72
|
|
|
if (in_array($value['table_name'], self::$ignore)) { |
|
73
|
|
|
continue; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
$down = "Schema::drop('{$value['table_name']}');"; |
|
77
|
|
|
$up = "Schema::create('{$value['table_name']}', function(Blueprint $" . "table) {\n"; |
|
78
|
|
|
|
|
79
|
|
|
|
|
80
|
|
|
$tableDescribes = $this->getTableDescribes($value['table_name']); |
|
81
|
|
|
// Loop over the tables fields |
|
82
|
|
|
foreach ($tableDescribes as $values) { |
|
83
|
|
|
$para = strpos($values->Type, '('); |
|
84
|
|
|
$type = $para > -1 ? substr($values->Type, 0, $para) : $values->Type; |
|
85
|
|
|
$numbers = ""; |
|
86
|
|
|
$nullable = $values->Null == "NO" ? "" : "->nullable()"; |
|
87
|
|
|
$default = empty($values->Default) ? "" : "->default(\"{$values->Default}\")"; |
|
88
|
|
|
$unsigned = strpos($values->Type, "unsigned") === false ? '': '->unsigned()'; |
|
89
|
|
|
|
|
90
|
|
|
if (in_array($type, ['var', 'varchar', 'enum', 'decimal', 'float'])) { |
|
91
|
|
|
$para = strpos($values->Type, '('); |
|
92
|
|
|
$opt = ", " . substr($values->Type, $para + 1, -1); |
|
93
|
|
|
$numbers = $type== 'enum' ? ', array(' . $opt . ')' : $opt; |
|
94
|
|
|
} |
|
95
|
|
|
$method = $this->columnType($type); |
|
96
|
|
|
if ($values->Key == 'PRI') { |
|
97
|
|
|
$method = 'increments'; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
$up .= " $" . "table->{$method}('{$values->Field}'{$numbers}){$nullable}{$default}{$unsigned};\n"; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
$tableIndexes = $this->getTableIndexes($value['table_name']); |
|
104
|
|
|
if (!is_null($tableIndexes) && count($tableIndexes)){ |
|
105
|
|
|
foreach ($tableIndexes as $index) { |
|
106
|
|
|
if(Str::endsWith($index['Key_name'], '_index')) { |
|
107
|
|
|
$up .= ' $' . "table->index('" . $index['Key_name'] . "');\n"; |
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
$up .= " });\n\n"; |
|
113
|
|
|
$Constraint = $ConstraintDown = ""; |
|
114
|
|
|
/** |
|
115
|
|
|
* @var array $tableConstraints |
|
116
|
|
|
*/ |
|
117
|
|
|
$tableConstraints = $this->getTableConstraints($value['table_name']); |
|
118
|
|
|
if (!is_null($tableConstraints) && count($tableConstraints)) { |
|
119
|
|
|
$Constraint = $ConstraintDown = " |
|
120
|
|
|
Schema::table('{$value['table_name']}', function(Blueprint $" . "table) {\n"; |
|
121
|
|
|
$tables = []; |
|
122
|
|
|
foreach ($tableConstraints as $foreign) { |
|
123
|
|
|
if (!in_array($foreign->Field, $tables)) { |
|
124
|
|
|
$ConstraintDown .= ' $' . "table->dropForeign('" . $foreign->Field . "');\n"; |
|
125
|
|
|
$Constraint .= ' $' . "table->foreign('" . $foreign->Field . "')->references('" . $foreign->References . "')->on('" . $foreign->ON . "')->onDelete('" . $foreign->onDelete . "');\n"; |
|
126
|
|
|
$tables[$foreign->Field] = $foreign->Field; |
|
127
|
|
|
} |
|
128
|
|
|
} |
|
129
|
|
|
$Constraint .= " });\n\n"; |
|
130
|
|
|
$ConstraintDown .= " });\n\n"; |
|
131
|
|
|
} |
|
132
|
|
|
$this->schema[$value['table_name']] = array( |
|
133
|
|
|
'up' => $up, |
|
134
|
|
|
'constraint' => $Constraint, |
|
135
|
|
|
'constraint_down' => $ConstraintDown, |
|
136
|
|
|
'down' => $down |
|
137
|
|
|
); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
return $this; |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
public function columnType($type) |
|
144
|
|
|
{ |
|
145
|
|
|
$columns = ['int'=> 'integer','smallint' => 'smallInteger','bigint' => 'bigInteger','char '=>'string', 'varchar' => 'string','float' => 'float','double' => 'double','decimal' => 'decimal','tinyint' => 'boolean','date' => 'date','timestamp' => 'timestamp','datetime' => 'dateTime','longtext' => 'longText','mediumtext' => 'mediumText','text' => 'text','longblob' => 'binary' ,'blob' => 'binary','enum' => 'enum']; |
|
146
|
|
|
return array_key_exists($type, $columns) ? $columns[$type] : ''; |
|
147
|
|
|
|
|
148
|
|
|
|
|
149
|
|
|
|
|
150
|
|
|
} |
|
151
|
|
|
/** |
|
152
|
|
|
* Compile the migration into the base migration file |
|
153
|
|
|
* TODO use a template with seacrh&replace |
|
154
|
|
|
* @return string |
|
155
|
|
|
*/ |
|
156
|
|
|
protected function compile() |
|
157
|
|
|
{ |
|
158
|
|
|
$upSchema = ""; |
|
159
|
|
|
$downSchema = ""; |
|
160
|
|
|
$upConstraint = ""; |
|
161
|
|
|
$downConstraint = ""; |
|
162
|
|
|
|
|
163
|
|
|
// prevent of failure when no table |
|
164
|
|
|
if (!is_null($this->schema) && count($this->schema)) { |
|
165
|
|
|
foreach ($this->schema as $name => $values) { |
|
166
|
|
|
// check again for ignored tables |
|
167
|
|
|
if (in_array($name, self::$ignore)) { |
|
168
|
|
|
continue; |
|
169
|
|
|
} |
|
170
|
|
|
$upSchema .= " |
|
171
|
|
|
/** |
|
172
|
|
|
* Table: {$name} |
|
173
|
|
|
*/ |
|
174
|
|
|
{$values['up']}"; |
|
175
|
|
|
$upConstraint.=" |
|
176
|
|
|
{$values['constraint']}"; |
|
177
|
|
|
$downConstraint.=" |
|
178
|
|
|
{$values['constraint_down']}"; |
|
179
|
|
|
|
|
180
|
|
|
$downSchema .= " |
|
181
|
|
|
{$values['down']}"; |
|
182
|
|
|
} |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
// Grab the template |
|
186
|
|
|
$template = File::get(__DIR__ . '/templates/migration.txt'); |
|
187
|
|
|
|
|
188
|
|
|
// Replace the classname |
|
189
|
|
|
$template = str_replace('{{name}}', "Create" . Str::camel(Str::title($this->database)) . "Database", $template); |
|
190
|
|
|
|
|
191
|
|
|
// Replace the up and down values |
|
192
|
|
|
$template = str_replace('{{up}}', $upSchema, $template); |
|
193
|
|
|
$template = str_replace('{{down}}', $downSchema, $template); |
|
194
|
|
|
$template = str_replace('{{upCon}}', $upConstraint, $template); |
|
195
|
|
|
$template = str_replace('{{downCon}}', $downConstraint, $template); |
|
196
|
|
|
|
|
197
|
|
|
return $template; |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
} |
|
201
|
|
|
|