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
|
|
|
$directory = $this->config['paths'][0]; |
18
|
|
|
$dbtables = $this->schema->getConnection()->showTables(); |
19
|
|
|
$migrations = $this->tableFilters(); |
20
|
|
|
|
21
|
|
|
$list = []; |
22
|
|
|
|
23
|
|
|
foreach ($migrations as $table=>$item){ |
24
|
|
|
$list[] = strtolower($table); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
foreach ($dbtables as $dbtable){ |
28
|
|
|
//if(!in_array($dbtable,$list)){ |
29
|
|
|
|
30
|
|
|
$informations = $this->tableInformations($dbtable); |
31
|
|
|
|
32
|
|
|
$dbtable = ucfirst($dbtable); |
33
|
|
|
$makeDirectory = $directory.''.DIRECTORY_SEPARATOR.''.$dbtable; |
34
|
|
|
files()->makeDirectory($makeDirectory,0755,true); |
35
|
|
|
|
36
|
|
|
$migrationName = time().'_'.$dbtable.''; |
37
|
|
|
|
38
|
|
|
$content = $this->getContentFile($this->getStubPath().''.DIRECTORY_SEPARATOR.'pullCreate.stub',[ |
39
|
|
|
'className' => $dbtable, |
40
|
|
|
'informations' => $informations |
41
|
|
|
]); |
42
|
|
|
|
43
|
|
|
files()->put($makeDirectory.''.DIRECTORY_SEPARATOR.''.$migrationName.'.php',$content); |
44
|
|
|
|
45
|
|
|
/**if(substr($dbtable,-1)=='s'){ |
46
|
|
|
app()->command('model create','model:'.strtolower(substr($dbtable,0,-1))); |
47
|
|
|
} |
48
|
|
|
else{ |
49
|
|
|
app()->command('model create','model:'.strtolower($dbtable)); |
50
|
|
|
}**/ |
51
|
|
|
|
52
|
|
|
//} |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* get table informations |
58
|
|
|
* |
59
|
|
|
* @param $table |
60
|
|
|
* @return string |
61
|
|
|
*/ |
62
|
|
|
private function tableInformations($table) |
63
|
|
|
{ |
64
|
|
|
$foreignKeys = $this->schema->getConnection()->getForeignKeys($table); |
65
|
|
|
|
66
|
|
|
$columns = $this->schema->getConnection()->showColumnsFrom($table); |
67
|
|
|
|
68
|
|
|
$status = $this->schema->getConnection()->getTableStatus($table); |
69
|
|
|
|
70
|
|
|
$indexes = $this->schema->getConnection()->showIndexes($table); |
71
|
|
|
$multipleIndexes = $this->getMultipleIndex($indexes); |
72
|
|
|
|
73
|
|
|
|
74
|
|
|
$list = []; |
75
|
|
|
|
76
|
|
|
foreach ($columns as $key=>$data){ |
77
|
|
|
|
78
|
|
|
$data['Type'] = rtrim(str_replace('unsigned','',$data['Type'])); |
79
|
|
|
|
80
|
|
|
$field = $data['Field']; |
81
|
|
|
$list[] = '$wizard->name(\''.$field.'\')'; |
82
|
|
|
$list[] = '->'.$this->getColumnTransformers($data['Type']).''; |
83
|
|
|
|
84
|
|
|
//default block |
85
|
|
|
if(!is_null($data['Default'])){ |
86
|
|
|
$default = $data['Default']; |
87
|
|
|
$list[] = '->default(\''.$default.'\')'; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
$getIndex = $this->getIndexInformation($indexes,$data['Field']); |
91
|
|
|
|
92
|
|
|
//unique block |
93
|
|
|
if($getIndex['Non_unique']=='0' && $getIndex['Key_name']!=='PRIMARY'){ |
94
|
|
|
$indexName = $getIndex['Key_name']; |
95
|
|
|
$list[] = '->unique(\''.$indexName.'\')'; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
//index block |
99
|
|
|
if($getIndex['Non_unique']=='1' && $getIndex['Key_name']!=='PRIMARY'){ |
100
|
|
|
$columnName = $getIndex['Column_name']; |
|
|
|
|
101
|
|
|
$indexName = $getIndex['Key_name']; |
102
|
|
|
|
103
|
|
|
if(count($multipleIndexes[$indexName])==1){ |
104
|
|
|
$list[] = '->index(\''.$indexName.'\')'; |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
//comment block |
109
|
|
|
if(strlen($data['Comment'])>0){ |
110
|
|
|
$comment = $data['Comment']; |
111
|
|
|
$list[] = '->comment(\''.$comment.'\')'; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
//auto increment block |
115
|
|
|
if($data['Extra']=='auto_increment'){ |
116
|
|
|
$list[] = '->auto_increment()'; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
$list[] = '; |
120
|
|
|
'; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
//table collation |
124
|
|
|
$charset = $this->schema->getConnection()->getCharsetForCollation(''.$status['Collation'].''); |
125
|
|
|
$list[] = '$wizard->table()->collation(\''.$charset.'\'); |
126
|
|
|
'; |
127
|
|
|
|
128
|
|
|
//table indexes |
129
|
|
|
foreach ($multipleIndexes as $indexName=>$values) { |
130
|
|
|
if(count($values)>1){ |
131
|
|
|
$values = '\''.implode('\',\'',$values).'\''; |
132
|
|
|
$list[] = ' $wizard->table()->indexes(\''.$indexName.'\',['.$values.']); |
133
|
|
|
'; |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
if(count($foreignKeys)){ |
138
|
|
|
|
139
|
|
|
$constraintName = $foreignKeys['CONSTRAINT_NAME']; |
140
|
|
|
$key = $foreignKeys['COLUMN_NAME']; |
141
|
|
|
$referenceTable = $foreignKeys['REFERENCED_TABLE_NAME']; |
142
|
|
|
$referenceColumn = $foreignKeys['REFERENCED_COLUMN_NAME']; |
143
|
|
|
|
144
|
|
|
if($foreignKeys['UPDATE_RULE']=='RESTRICT' && $foreignKeys['DELETE_RULE']=='RESTRICT'){ |
145
|
|
|
$list[] = ' $wizard->table()->foreign()->constraint(\''.$constraintName.'\')->key(\''.$key.'\')->references(\''.$referenceTable.'\',\''.$referenceColumn.'\'); |
146
|
|
|
'; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
if($foreignKeys['UPDATE_RULE']!=='RESTRICT' && $foreignKeys['DELETE_RULE']=='RESTRICT'){ |
150
|
|
|
$rule = $foreignKeys['UPDATE_RULE']; |
151
|
|
|
$list[] = ' $wizard->table()->foreign()->constraint(\''.$constraintName.'\')->key(\''.$key.'\')->references(\''.$referenceTable.'\',\''.$referenceColumn.'\')->onUpdate()->'.strtolower($rule).'(); |
152
|
|
|
'; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
if($foreignKeys['UPDATE_RULE']=='RESTRICT' && $foreignKeys['DELETE_RULE']!=='RESTRICT'){ |
156
|
|
|
$rule = $foreignKeys['DELETE_RULE']; |
157
|
|
|
$list[] = ' $wizard->table()->foreign()->constraint(\''.$constraintName.'\')->key(\''.$key.'\')->references(\''.$referenceTable.'\',\''.$referenceColumn.'\')->onDelete()->'.strtolower($rule).'(); |
158
|
|
|
'; |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
return implode('',$list); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* get column transformers |
167
|
|
|
* |
168
|
|
|
* @param $column |
169
|
|
|
* @return string |
170
|
|
|
*/ |
171
|
|
|
private function getColumnTransformers($column) |
172
|
|
|
{ |
173
|
|
|
if($column=='datetime'){ |
174
|
|
|
return 'datetime()'; |
175
|
|
|
} |
176
|
|
|
elseif($column=='longtext'){ |
177
|
|
|
return 'longtext()'; |
178
|
|
|
} |
179
|
|
|
elseif($column=='date'){ |
180
|
|
|
return 'date()'; |
181
|
|
|
} |
182
|
|
|
elseif($column=='text'){ |
183
|
|
|
return 'text()'; |
184
|
|
|
} |
185
|
|
|
elseif($column=='timestamp'){ |
186
|
|
|
return 'timestamp'; |
187
|
|
|
} |
188
|
|
|
elseif($column=='mediumint'){ |
189
|
|
|
return 'mediumint()'; |
190
|
|
|
} |
191
|
|
|
elseif($column=='tinyint'){ |
192
|
|
|
return 'tinyint()'; |
193
|
|
|
} |
194
|
|
|
elseif($column=='float'){ |
195
|
|
|
return 'float()'; |
196
|
|
|
} |
197
|
|
|
elseif($column=='mediumtext'){ |
198
|
|
|
return 'mediumtext()'; |
199
|
|
|
} |
200
|
|
|
elseif($column=='mediumblob'){ |
201
|
|
|
return 'mediumblob()'; |
202
|
|
|
} |
203
|
|
|
elseif($column=='blob'){ |
204
|
|
|
return 'blob()'; |
205
|
|
|
} |
206
|
|
|
elseif(preg_match('@enum.*\((.*?)\)@',$column,$enum)){ |
207
|
|
|
return 'enum(['.$enum[1].'])'; |
208
|
|
|
} |
209
|
|
|
else{ |
210
|
|
|
return $column; |
211
|
|
|
} |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* get index information |
216
|
|
|
* |
217
|
|
|
* @param $index |
218
|
|
|
* @param $field |
219
|
|
|
* @return void|mixed |
220
|
|
|
*/ |
221
|
|
|
private function getIndexInformation($index,$field) |
222
|
|
|
{ |
223
|
|
|
foreach ($index as $key=>$item) { |
224
|
|
|
|
225
|
|
|
if($item['Column_name'] == $field){ |
226
|
|
|
return $index[$key]; |
227
|
|
|
} |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
return null; |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
/** |
234
|
|
|
* get index information |
235
|
|
|
* |
236
|
|
|
* @param $index |
237
|
|
|
* @param $field |
238
|
|
|
* @return void|mixed |
239
|
|
|
*/ |
240
|
|
|
private function getMultipleIndex($index) |
241
|
|
|
{ |
242
|
|
|
$list = []; |
243
|
|
|
foreach ($index as $key=>$item) { |
244
|
|
|
if($item['Non_unique']==1 && $item['Key_name']!=='PRIMARY'){ |
245
|
|
|
$list[$item['Key_name']][] = $item['Column_name']; |
246
|
|
|
} |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
return $list; |
250
|
|
|
} |
251
|
|
|
} |