|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Migratio\Resource\PullManager; |
|
4
|
|
|
|
|
5
|
|
|
use Migratio\Resource\BaseManager; |
|
6
|
|
|
use Resta\Exception\FileNotFoundException; |
|
7
|
|
|
|
|
8
|
|
|
class Pulling extends BaseManager |
|
9
|
|
|
{ |
|
10
|
|
|
/** |
|
11
|
|
|
* @return void|mixed |
|
12
|
|
|
* |
|
13
|
|
|
* @throws FileNotFoundException |
|
14
|
|
|
*/ |
|
15
|
|
|
public function get() |
|
16
|
|
|
{ |
|
17
|
|
|
if(!isset($this->config['paths'][0])){ |
|
18
|
|
|
exit(); |
|
|
|
|
|
|
19
|
|
|
} |
|
20
|
|
|
|
|
21
|
|
|
$arguments = $this->schema->getArguments(); |
|
22
|
|
|
|
|
23
|
|
|
$directory = $this->config['paths'][0]; |
|
24
|
|
|
$dbtables = $this->schema->getConnection()->showTables(); |
|
25
|
|
|
|
|
26
|
|
|
$migrations = $this->tableFilters(); |
|
27
|
|
|
|
|
28
|
|
|
$list = []; |
|
29
|
|
|
|
|
30
|
|
|
foreach ($migrations as $table=>$item){ |
|
31
|
|
|
$list[] = strtolower($table); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
if(isset($arguments['only'])){ |
|
35
|
|
|
$dbtables = $this->getOnly($dbtables); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
echo 'Migrations Pull Tasks:'; |
|
39
|
|
|
echo PHP_EOL; |
|
40
|
|
|
echo 'Toplam : '.count($dbtables).' Table'; |
|
41
|
|
|
echo PHP_EOL; |
|
42
|
|
|
|
|
43
|
|
|
foreach ($dbtables as $dbtablekey=>$dbtable){ |
|
44
|
|
|
|
|
45
|
|
|
$dbtablekey = $dbtablekey+1; |
|
46
|
|
|
|
|
47
|
|
|
$informations = $this->tableInformations($dbtable); |
|
48
|
|
|
|
|
49
|
|
|
$dbtable = ucfirst($dbtable); |
|
50
|
|
|
$makeDirectory = $directory.''.DIRECTORY_SEPARATOR.''.$dbtable; |
|
51
|
|
|
|
|
52
|
|
|
if(!file_exists($makeDirectory)){ |
|
53
|
|
|
files()->makeDirectory($makeDirectory,0755,true); |
|
54
|
|
|
$exist=false; |
|
55
|
|
|
} |
|
56
|
|
|
else{ |
|
57
|
|
|
$exist=true; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
$migrationName = time().'_'.$dbtable.''; |
|
61
|
|
|
|
|
62
|
|
|
if($exist===false){ |
|
63
|
|
|
|
|
64
|
|
|
$content = $this->getContentFile($this->getStubPath().''.DIRECTORY_SEPARATOR.'pullCreate.stub',[ |
|
65
|
|
|
'className' => $dbtable, |
|
66
|
|
|
'informations' => $informations |
|
67
|
|
|
]); |
|
68
|
|
|
|
|
69
|
|
|
$contentResult = files()->put($makeDirectory.''.DIRECTORY_SEPARATOR.''.$migrationName.'.php',$content); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
if(substr($dbtable,-1)=='s'){ |
|
73
|
|
|
app()->command('model create','model:'.strtolower(substr($dbtable,0,-1)).' table:'.$dbtable); |
|
|
|
|
|
|
74
|
|
|
} |
|
75
|
|
|
else{ |
|
76
|
|
|
app()->command('model create','model:'.strtolower($dbtable).' table:'.$dbtable); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
if(isset($contentResult) && $contentResult!==false){ |
|
80
|
|
|
echo $dbtablekey.'- '.$migrationName.' ---> Ok'; |
|
81
|
|
|
} |
|
82
|
|
|
elseif($exist){ |
|
83
|
|
|
echo $dbtablekey.'- '.$migrationName.' ---> (Already Exist)'; |
|
84
|
|
|
} |
|
85
|
|
|
else{ |
|
86
|
|
|
echo $dbtablekey.'- '.$migrationName.' ---> Fail'; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
echo PHP_EOL; |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* get table informations |
|
95
|
|
|
* |
|
96
|
|
|
* @param $table |
|
97
|
|
|
* @return string |
|
98
|
|
|
*/ |
|
99
|
|
|
private function tableInformations($table) |
|
100
|
|
|
{ |
|
101
|
|
|
$foreignKeys = $this->schema->getConnection()->getForeignKeys($table); |
|
102
|
|
|
|
|
103
|
|
|
$columns = $this->schema->getConnection()->showColumnsFrom($table); |
|
104
|
|
|
|
|
105
|
|
|
$status = $this->schema->getConnection()->getTableStatus($table); |
|
106
|
|
|
|
|
107
|
|
|
$indexes = $this->schema->getConnection()->showIndexes($table); |
|
108
|
|
|
$multipleIndexes = $this->getMultipleIndex($indexes); |
|
109
|
|
|
|
|
110
|
|
|
|
|
111
|
|
|
$list = []; |
|
112
|
|
|
|
|
113
|
|
|
foreach ($columns as $key=>$data){ |
|
114
|
|
|
|
|
115
|
|
|
$data['Type'] = rtrim(str_replace('unsigned','',$data['Type'])); |
|
116
|
|
|
|
|
117
|
|
|
$field = $data['Field']; |
|
118
|
|
|
$list[] = '$wizard->name(\''.$field.'\')'; |
|
119
|
|
|
$list[] = '->'.$this->getColumnTransformers($data['Type']).''; |
|
120
|
|
|
|
|
121
|
|
|
//default block |
|
122
|
|
|
if(!is_null($data['Default'])){ |
|
123
|
|
|
$default = $data['Default']; |
|
124
|
|
|
$list[] = '->default(\''.$default.'\')'; |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
$getIndex = $this->getIndexInformation($indexes,$data['Field']); |
|
128
|
|
|
|
|
129
|
|
|
//unique block |
|
130
|
|
|
if($getIndex['Non_unique']=='0' && $getIndex['Key_name']!=='PRIMARY'){ |
|
131
|
|
|
$indexName = $getIndex['Key_name']; |
|
132
|
|
|
$list[] = '->unique(\''.$indexName.'\')'; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
//index block |
|
136
|
|
|
if($getIndex['Non_unique']=='1' && $getIndex['Key_name']!=='PRIMARY'){ |
|
137
|
|
|
$columnName = $getIndex['Column_name']; |
|
|
|
|
|
|
138
|
|
|
$indexName = $getIndex['Key_name']; |
|
139
|
|
|
|
|
140
|
|
|
if(count($multipleIndexes[$indexName])==1){ |
|
141
|
|
|
$list[] = '->index(\''.$indexName.'\')'; |
|
142
|
|
|
} |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
//comment block |
|
146
|
|
|
if(strlen($data['Comment'])>0){ |
|
147
|
|
|
$comment = $data['Comment']; |
|
148
|
|
|
$list[] = '->comment(\''.$comment.'\')'; |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
//auto increment block |
|
152
|
|
|
if($data['Extra']=='auto_increment'){ |
|
153
|
|
|
$list[] = '->auto_increment()'; |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
$list[] = '; |
|
157
|
|
|
'; |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
//table collation |
|
161
|
|
|
$charset = $this->schema->getConnection()->getCharsetForCollation(''.$status['Collation'].''); |
|
162
|
|
|
$list[] = '$wizard->table()->collation(\''.$charset.'\'); |
|
163
|
|
|
'; |
|
164
|
|
|
|
|
165
|
|
|
//table indexes |
|
166
|
|
|
foreach ($multipleIndexes as $indexName=>$values) { |
|
167
|
|
|
if(count($values)>1){ |
|
168
|
|
|
$values = '\''.implode('\',\'',$values).'\''; |
|
169
|
|
|
$list[] = ' $wizard->table()->indexes(\''.$indexName.'\',['.$values.']); |
|
170
|
|
|
'; |
|
171
|
|
|
} |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
if(count($foreignKeys)){ |
|
175
|
|
|
|
|
176
|
|
|
$constraintName = $foreignKeys['CONSTRAINT_NAME']; |
|
177
|
|
|
$key = $foreignKeys['COLUMN_NAME']; |
|
178
|
|
|
$referenceTable = $foreignKeys['REFERENCED_TABLE_NAME']; |
|
179
|
|
|
$referenceColumn = $foreignKeys['REFERENCED_COLUMN_NAME']; |
|
180
|
|
|
|
|
181
|
|
|
if($foreignKeys['UPDATE_RULE']=='RESTRICT' && $foreignKeys['DELETE_RULE']=='RESTRICT'){ |
|
182
|
|
|
$list[] = ' $wizard->table()->foreign()->constraint(\''.$constraintName.'\')->key(\''.$key.'\')->references(\''.$referenceTable.'\',\''.$referenceColumn.'\'); |
|
183
|
|
|
'; |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
if($foreignKeys['UPDATE_RULE']!=='RESTRICT' && $foreignKeys['DELETE_RULE']=='RESTRICT'){ |
|
187
|
|
|
$rule = $foreignKeys['UPDATE_RULE']; |
|
188
|
|
|
$list[] = ' $wizard->table()->foreign()->constraint(\''.$constraintName.'\')->key(\''.$key.'\')->references(\''.$referenceTable.'\',\''.$referenceColumn.'\')->onUpdate()->'.strtolower($rule).'(); |
|
189
|
|
|
'; |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
if($foreignKeys['UPDATE_RULE']=='RESTRICT' && $foreignKeys['DELETE_RULE']!=='RESTRICT'){ |
|
193
|
|
|
$rule = $foreignKeys['DELETE_RULE']; |
|
194
|
|
|
$list[] = ' $wizard->table()->foreign()->constraint(\''.$constraintName.'\')->key(\''.$key.'\')->references(\''.$referenceTable.'\',\''.$referenceColumn.'\')->onDelete()->'.strtolower($rule).'(); |
|
195
|
|
|
'; |
|
196
|
|
|
} |
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
|
|
return implode('',$list); |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
/** |
|
203
|
|
|
* get column transformers |
|
204
|
|
|
* |
|
205
|
|
|
* @param $column |
|
206
|
|
|
* @return string |
|
207
|
|
|
*/ |
|
208
|
|
|
private function getColumnTransformers($column) |
|
209
|
|
|
{ |
|
210
|
|
|
if($column=='datetime'){ |
|
211
|
|
|
return 'datetime()'; |
|
212
|
|
|
} |
|
213
|
|
|
elseif($column=='longtext'){ |
|
214
|
|
|
return 'longtext()'; |
|
215
|
|
|
} |
|
216
|
|
|
elseif($column=='date'){ |
|
217
|
|
|
return 'date()'; |
|
218
|
|
|
} |
|
219
|
|
|
elseif($column=='text'){ |
|
220
|
|
|
return 'text()'; |
|
221
|
|
|
} |
|
222
|
|
|
elseif($column=='timestamp'){ |
|
223
|
|
|
return 'timestamp()'; |
|
224
|
|
|
} |
|
225
|
|
|
elseif($column=='mediumint'){ |
|
226
|
|
|
return 'mediumint()'; |
|
227
|
|
|
} |
|
228
|
|
|
elseif($column=='tinyint'){ |
|
229
|
|
|
return 'tinyint()'; |
|
230
|
|
|
} |
|
231
|
|
|
elseif($column=='float'){ |
|
232
|
|
|
return 'float()'; |
|
233
|
|
|
} |
|
234
|
|
|
elseif($column=='mediumtext'){ |
|
235
|
|
|
return 'mediumtext()'; |
|
236
|
|
|
} |
|
237
|
|
|
elseif($column=='mediumblob'){ |
|
238
|
|
|
return 'mediumblob()'; |
|
239
|
|
|
} |
|
240
|
|
|
elseif($column=='blob'){ |
|
241
|
|
|
return 'blob()'; |
|
242
|
|
|
} |
|
243
|
|
|
elseif(preg_match('@enum.*\((.*?)\)@',$column,$enum)){ |
|
244
|
|
|
return 'enum(['.$enum[1].'])'; |
|
245
|
|
|
} |
|
246
|
|
|
else{ |
|
247
|
|
|
return $column; |
|
248
|
|
|
} |
|
249
|
|
|
} |
|
250
|
|
|
|
|
251
|
|
|
/** |
|
252
|
|
|
* get index information |
|
253
|
|
|
* |
|
254
|
|
|
* @param $index |
|
255
|
|
|
* @param $field |
|
256
|
|
|
* @return void|mixed |
|
257
|
|
|
*/ |
|
258
|
|
|
private function getIndexInformation($index,$field) |
|
259
|
|
|
{ |
|
260
|
|
|
foreach ($index as $key=>$item) { |
|
261
|
|
|
|
|
262
|
|
|
if($item['Column_name'] == $field){ |
|
263
|
|
|
return $index[$key]; |
|
264
|
|
|
} |
|
265
|
|
|
} |
|
266
|
|
|
|
|
267
|
|
|
return null; |
|
268
|
|
|
} |
|
269
|
|
|
|
|
270
|
|
|
/** |
|
271
|
|
|
* get only tables |
|
272
|
|
|
* |
|
273
|
|
|
* @param $tables |
|
274
|
|
|
* @return array |
|
275
|
|
|
*/ |
|
276
|
|
|
private function getOnly($tables) |
|
277
|
|
|
{ |
|
278
|
|
|
$arguments = $this->schema->getArguments(); |
|
279
|
|
|
|
|
280
|
|
|
$only = explode(',',$arguments['only']); |
|
281
|
|
|
|
|
282
|
|
|
$list = []; |
|
283
|
|
|
|
|
284
|
|
|
foreach($only as $table){ |
|
285
|
|
|
|
|
286
|
|
|
if(in_array($table,$tables) || in_array($table = strtolower($table),$tables)){ |
|
287
|
|
|
$list[] = $table; |
|
288
|
|
|
} |
|
289
|
|
|
} |
|
290
|
|
|
|
|
291
|
|
|
return $list; |
|
292
|
|
|
} |
|
293
|
|
|
|
|
294
|
|
|
/** |
|
295
|
|
|
* get index information |
|
296
|
|
|
* |
|
297
|
|
|
* @param $index |
|
298
|
|
|
* @param $field |
|
299
|
|
|
* @return void|mixed |
|
300
|
|
|
*/ |
|
301
|
|
|
private function getMultipleIndex($index) |
|
302
|
|
|
{ |
|
303
|
|
|
$list = []; |
|
304
|
|
|
foreach ($index as $key=>$item) { |
|
305
|
|
|
if($item['Non_unique']==1 && $item['Key_name']!=='PRIMARY'){ |
|
306
|
|
|
$list[$item['Key_name']][] = $item['Column_name']; |
|
307
|
|
|
} |
|
308
|
|
|
} |
|
309
|
|
|
|
|
310
|
|
|
return $list; |
|
311
|
|
|
} |
|
312
|
|
|
} |
In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.