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
|
|
|
|
73
|
|
|
|
74
|
|
|
if(isset($contentResult) && $contentResult!==false){ |
75
|
|
|
|
76
|
|
|
$this->schema->getConnection()->generateEntity($dbtable); |
77
|
|
|
|
78
|
|
|
if(substr($dbtable,-1)=='s'){ |
79
|
|
|
app()->command('model create','model:'.strtolower(substr($dbtable,0,-1)).' table:'.$dbtable.' entity:'.$dbtable); |
|
|
|
|
80
|
|
|
} |
81
|
|
|
else{ |
82
|
|
|
app()->command('model create','model:'.strtolower($dbtable).' table:'.$dbtable.' entity:'.$dbtable); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
echo $dbtablekey.'- '.$migrationName.' ---> Ok'; |
86
|
|
|
} |
87
|
|
|
elseif($exist){ |
88
|
|
|
echo $dbtablekey.'- '.$migrationName.' ---> (Already Exist)'; |
89
|
|
|
} |
90
|
|
|
else{ |
91
|
|
|
echo $dbtablekey.'- '.$migrationName.' ---> Fail'; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
echo PHP_EOL; |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* get table informations |
100
|
|
|
* |
101
|
|
|
* @param $table |
102
|
|
|
* @return string |
103
|
|
|
*/ |
104
|
|
|
private function tableInformations($table) |
105
|
|
|
{ |
106
|
|
|
$foreignKeys = $this->schema->getConnection()->getForeignKeys($table); |
107
|
|
|
|
108
|
|
|
$columns = $this->schema->getConnection()->showColumnsFrom($table); |
109
|
|
|
|
110
|
|
|
$status = $this->schema->getConnection()->getTableStatus($table); |
111
|
|
|
|
112
|
|
|
$indexes = $this->schema->getConnection()->showIndexes($table); |
113
|
|
|
$multipleIndexes = $this->getMultipleIndex($indexes); |
114
|
|
|
|
115
|
|
|
|
116
|
|
|
$list = []; |
117
|
|
|
|
118
|
|
|
foreach ($columns as $key=>$data){ |
119
|
|
|
|
120
|
|
|
$data['Type'] = rtrim(str_replace('unsigned','',$data['Type'])); |
121
|
|
|
|
122
|
|
|
$field = $data['Field']; |
123
|
|
|
$list[] = '$wizard->name(\''.$field.'\')'; |
124
|
|
|
$list[] = '->'.$this->getColumnTransformers($data['Type']).''; |
125
|
|
|
|
126
|
|
|
//default block |
127
|
|
|
if(!is_null($data['Default'])){ |
128
|
|
|
$default = $data['Default']; |
129
|
|
|
$list[] = '->default(\''.$default.'\')'; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
$getIndex = $this->getIndexInformation($indexes,$data['Field']); |
133
|
|
|
|
134
|
|
|
//unique block |
135
|
|
|
if($getIndex['Non_unique']=='0' && $getIndex['Key_name']!=='PRIMARY'){ |
136
|
|
|
$indexName = $getIndex['Key_name']; |
137
|
|
|
$list[] = '->unique(\''.$indexName.'\')'; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
//index block |
141
|
|
|
if($getIndex['Non_unique']=='1' && $getIndex['Key_name']!=='PRIMARY'){ |
142
|
|
|
$columnName = $getIndex['Column_name']; |
|
|
|
|
143
|
|
|
$indexName = $getIndex['Key_name']; |
144
|
|
|
|
145
|
|
|
if(count($multipleIndexes[$indexName])==1){ |
146
|
|
|
$list[] = '->index(\''.$indexName.'\')'; |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
//comment block |
151
|
|
|
if(strlen($data['Comment'])>0){ |
152
|
|
|
$comment = $data['Comment']; |
153
|
|
|
$list[] = '->comment(\''.$comment.'\')'; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
//auto increment block |
157
|
|
|
if($data['Extra']=='auto_increment'){ |
158
|
|
|
$list[] = '->auto_increment()'; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
$list[] = '; |
162
|
|
|
'; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
//table collation |
166
|
|
|
$charset = $this->schema->getConnection()->getCharsetForCollation(''.$status['Collation'].''); |
167
|
|
|
$list[] = '$wizard->table()->collation(\''.$charset.'\'); |
168
|
|
|
'; |
169
|
|
|
|
170
|
|
|
//table indexes |
171
|
|
|
foreach ($multipleIndexes as $indexName=>$values) { |
172
|
|
|
if(count($values)>1){ |
173
|
|
|
$values = '\''.implode('\',\'',$values).'\''; |
174
|
|
|
$list[] = ' $wizard->table()->indexes(\''.$indexName.'\',['.$values.']); |
175
|
|
|
'; |
176
|
|
|
} |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
if(count($foreignKeys)){ |
180
|
|
|
|
181
|
|
|
$constraintName = $foreignKeys['CONSTRAINT_NAME']; |
182
|
|
|
$key = $foreignKeys['COLUMN_NAME']; |
183
|
|
|
$referenceTable = $foreignKeys['REFERENCED_TABLE_NAME']; |
184
|
|
|
$referenceColumn = $foreignKeys['REFERENCED_COLUMN_NAME']; |
185
|
|
|
|
186
|
|
|
if($foreignKeys['UPDATE_RULE']=='RESTRICT' && $foreignKeys['DELETE_RULE']=='RESTRICT'){ |
187
|
|
|
$list[] = ' $wizard->table()->foreign()->constraint(\''.$constraintName.'\')->key(\''.$key.'\')->references(\''.$referenceTable.'\',\''.$referenceColumn.'\'); |
188
|
|
|
'; |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
if($foreignKeys['UPDATE_RULE']!=='RESTRICT' && $foreignKeys['DELETE_RULE']=='RESTRICT'){ |
192
|
|
|
$rule = $foreignKeys['UPDATE_RULE']; |
193
|
|
|
$list[] = ' $wizard->table()->foreign()->constraint(\''.$constraintName.'\')->key(\''.$key.'\')->references(\''.$referenceTable.'\',\''.$referenceColumn.'\')->onUpdate()->'.strtolower($rule).'(); |
194
|
|
|
'; |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
if($foreignKeys['UPDATE_RULE']=='RESTRICT' && $foreignKeys['DELETE_RULE']!=='RESTRICT'){ |
198
|
|
|
$rule = $foreignKeys['DELETE_RULE']; |
199
|
|
|
$list[] = ' $wizard->table()->foreign()->constraint(\''.$constraintName.'\')->key(\''.$key.'\')->references(\''.$referenceTable.'\',\''.$referenceColumn.'\')->onDelete()->'.strtolower($rule).'(); |
200
|
|
|
'; |
201
|
|
|
} |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
return implode('',$list); |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* get column transformers |
209
|
|
|
* |
210
|
|
|
* @param $column |
211
|
|
|
* @return string |
212
|
|
|
*/ |
213
|
|
|
private function getColumnTransformers($column) |
214
|
|
|
{ |
215
|
|
|
if($column=='datetime'){ |
216
|
|
|
return 'datetime()'; |
217
|
|
|
} |
218
|
|
|
elseif($column=='longtext'){ |
219
|
|
|
return 'longtext()'; |
220
|
|
|
} |
221
|
|
|
elseif($column=='date'){ |
222
|
|
|
return 'date()'; |
223
|
|
|
} |
224
|
|
|
elseif($column=='text'){ |
225
|
|
|
return 'text()'; |
226
|
|
|
} |
227
|
|
|
elseif($column=='timestamp'){ |
228
|
|
|
return 'timestamp()'; |
229
|
|
|
} |
230
|
|
|
elseif($column=='mediumint'){ |
231
|
|
|
return 'mediumint()'; |
232
|
|
|
} |
233
|
|
|
elseif($column=='tinyint'){ |
234
|
|
|
return 'tinyint()'; |
235
|
|
|
} |
236
|
|
|
elseif($column=='float'){ |
237
|
|
|
return 'float()'; |
238
|
|
|
} |
239
|
|
|
elseif($column=='mediumtext'){ |
240
|
|
|
return 'mediumtext()'; |
241
|
|
|
} |
242
|
|
|
elseif($column=='mediumblob'){ |
243
|
|
|
return 'mediumblob()'; |
244
|
|
|
} |
245
|
|
|
elseif($column=='blob'){ |
246
|
|
|
return 'blob()'; |
247
|
|
|
} |
248
|
|
|
elseif(preg_match('@enum.*\((.*?)\)@',$column,$enum)){ |
249
|
|
|
return 'enum(['.$enum[1].'])'; |
250
|
|
|
} |
251
|
|
|
else{ |
252
|
|
|
return $column; |
253
|
|
|
} |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
/** |
257
|
|
|
* get index information |
258
|
|
|
* |
259
|
|
|
* @param $index |
260
|
|
|
* @param $field |
261
|
|
|
* @return void|mixed |
262
|
|
|
*/ |
263
|
|
|
private function getIndexInformation($index,$field) |
264
|
|
|
{ |
265
|
|
|
foreach ($index as $key=>$item) { |
266
|
|
|
|
267
|
|
|
if($item['Column_name'] == $field){ |
268
|
|
|
return $index[$key]; |
269
|
|
|
} |
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
return null; |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
/** |
276
|
|
|
* get only tables |
277
|
|
|
* |
278
|
|
|
* @param $tables |
279
|
|
|
* @return array |
280
|
|
|
*/ |
281
|
|
|
private function getOnly($tables) |
282
|
|
|
{ |
283
|
|
|
$arguments = $this->schema->getArguments(); |
284
|
|
|
|
285
|
|
|
$only = explode(',',$arguments['only']); |
286
|
|
|
|
287
|
|
|
$list = []; |
288
|
|
|
|
289
|
|
|
foreach($only as $table){ |
290
|
|
|
|
291
|
|
|
if(in_array($table,$tables) || in_array($table = strtolower($table),$tables)){ |
292
|
|
|
$list[] = $table; |
293
|
|
|
} |
294
|
|
|
} |
295
|
|
|
|
296
|
|
|
return $list; |
297
|
|
|
} |
298
|
|
|
|
299
|
|
|
/** |
300
|
|
|
* get index information |
301
|
|
|
* |
302
|
|
|
* @param $index |
303
|
|
|
* @param $field |
304
|
|
|
* @return void|mixed |
305
|
|
|
*/ |
306
|
|
|
private function getMultipleIndex($index) |
307
|
|
|
{ |
308
|
|
|
$list = []; |
309
|
|
|
foreach ($index as $key=>$item) { |
310
|
|
|
if($item['Non_unique']==1 && $item['Key_name']!=='PRIMARY'){ |
311
|
|
|
$list[$item['Key_name']][] = $item['Column_name']; |
312
|
|
|
} |
313
|
|
|
} |
314
|
|
|
|
315
|
|
|
return $list; |
316
|
|
|
} |
317
|
|
|
} |
In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.