@@ -35,8 +35,11 @@ class MysqlStructSync
35
35
36
36
private $ remove_auto_increment = false ;
37
37
38
-
39
-
38
+ /**
39
+ * @Author : 9rax.dev@gmail.com
40
+ * @DateTime: 2019年8月30日 19:31
41
+ * @var array
42
+ */
40
43
static $ advance = [
41
44
'VIEW ' => ["SELECT TABLE_NAME as Name FROM information_schema.VIEWS WHERE TABLE_SCHEMA='#' " , 'Create View ' ],
42
45
'TRIGGER ' => ["SELECT TRIGGER_NAME as Name FROM information_schema.TRIGGERS WHERE TRIGGER_SCHEMA='#' " , 'SQL Original Statement ' ],
@@ -54,11 +57,12 @@ class MysqlStructSync
54
57
public function __construct ($ old_database_config , $ new_database_config )
55
58
{
56
59
57
- $ this ->self_connection = new \Mysqli ($ old_database_config ['host ' ],
60
+ $ this ->self_connection = new \Mysqli (
61
+ $ old_database_config ['host ' ],
58
62
$ old_database_config ['username ' ],
59
63
$ old_database_config ['passwd ' ],
60
64
$ old_database_config ['dbname ' ],
61
- $ old_database_config ['port ' ]
65
+ isset ( $ old_database_config ['port ' ])? $ old_database_config [ ' port ' ]: 3306
62
66
);
63
67
64
68
@@ -72,7 +76,7 @@ public function __construct($old_database_config, $new_database_config)
72
76
$ new_database_config ['username ' ],
73
77
$ new_database_config ['passwd ' ],
74
78
$ new_database_config ['dbname ' ],
75
- $ new_database_config ['port ' ]
79
+ isset ( $ new_database_config ['port ' ])? $ new_database_config [ ' port ' ]: 3306
76
80
);
77
81
78
82
@@ -108,13 +112,12 @@ function baseDiff()
108
112
$ this ->self_database_struct = $ this ->getStructure ($ this ->self_connection );
109
113
$ this ->refer_database_struct = $ this ->getStructure ($ this ->refer_connection );
110
114
111
-
112
115
$ res ['ADD_TABLE ' ] = array_diff ($ this ->refer_database_struct ['tables ' ], $ this ->self_database_struct ['tables ' ]);
113
116
$ res ['DROP_TABLE ' ] = array_diff ($ this ->self_database_struct ['tables ' ], $ this ->refer_database_struct ['tables ' ]);
114
117
115
-
116
- $ develop_columns = array_intersect_assoc ($ this ->refer_database_struct ['columns ' ], $ this ->self_database_struct ['columns ' ]);
117
- $ self_columns = array_intersect_assoc ($ this ->self_database_struct ['columns ' ], $ this ->refer_database_struct ['columns ' ]);
118
+ //array_intersect_assoc will report notice error
119
+ $ develop_columns = self :: _array_intersect_assoc ($ this ->refer_database_struct ['columns ' ], $ this ->self_database_struct ['columns ' ]);
120
+ $ self_columns = self :: _array_intersect_assoc ($ this ->self_database_struct ['columns ' ], $ this ->refer_database_struct ['columns ' ]);
118
121
119
122
120
123
if ($ develop_columns ) {
@@ -145,6 +148,30 @@ function baseDiff()
145
148
146
149
}
147
150
151
+ /**
152
+ * array_intersect_assoc
153
+ * @return mixed
154
+ * @Author : 9rax.dev@gmail.com
155
+ * @DateTime: 2019年8月30日 19:27
156
+ */
157
+ static function _array_intersect_assoc () {
158
+
159
+ $ args = func_get_args ();
160
+ $ res = $ args [0 ];
161
+
162
+ for ($ i =1 ;$ i <count ($ args );$ i ++) {
163
+ if (!is_array ($ args [$ i ])) {continue ;}
164
+
165
+ foreach ($ res as $ key => $ data ) {
166
+ if ( (!array_key_exists ($ key , $ args [$ i ])) || ( (isset ($ args [$ i ][$ key ])) && ($ args [$ i ][$ key ] !== $ res [$ key ]) ) ) {
167
+ unset($ res [$ key ]);
168
+ }
169
+ }
170
+ }
171
+
172
+ return $ res ;
173
+ }
174
+
148
175
149
176
/**
150
177
* advanceDiff
@@ -163,15 +190,14 @@ function advanceDiff()
163
190
$ sql = str_replace ('# ' , $ this ->$ db , $ list_sql [0 ]);
164
191
$ connect = $ this ->$ conn ->query ($ sql );
165
192
$ res = $ connect ->fetch_all (MYSQLI_ASSOC );
166
-
167
- foreach ($ res as $ row ) {
168
- $ show_create_conn = $ this ->$ conn ->query ('SHOW CREATE ' . $ type . '' . $ row ['Name ' ]);
169
- //p( $show_create_conn->fetch_assoc());
170
- $ arr [ $ type ][ $ who ][ $ row [ ' Name ' ]] = preg_replace ( ' /DEFINER=[^\s]*/ ' , '' , $ show_create_conn -> fetch_assoc ()[ $ list_sql [ 1 ]]);
193
+ if ( $ res ){
194
+ foreach ($ res as $ row ) {
195
+ $ show_create_conn = $ this ->$ conn ->query ('SHOW CREATE ' . $ type . '' . $ row ['Name ' ]);
196
+ $ arr [ $ type ][ $ who ][ $ row [ ' Name ' ]] = preg_replace ( ' /DEFINER=[^\s]*/ ' , '' , $ show_create_conn ->fetch_assoc ()[ $ list_sql [ 1 ]] );
197
+ }
171
198
}
172
-
173
- $ diff ['ADD_ ' . $ type ] = self ::array_diff_assoc_recursive ($ arr [$ type ]['refer ' ], $ arr [$ type ]['self ' ]);
174
- $ diff ['DROP_ ' . $ type ] = self ::array_diff_assoc_recursive ($ arr [$ type ]['self ' ], $ arr [$ type ]['refer ' ]);
199
+ $ diff ['ADD_ ' . $ type ] = self ::array_diff_assoc_recursive (isset ($ arr [$ type ]['refer ' ])?$ arr [$ type ]['refer ' ]:[], isset ($ arr [$ type ]['self ' ])?$ arr [$ type ]['self ' ]:[]);
200
+ $ diff ['DROP_ ' . $ type ] = self ::array_diff_assoc_recursive (isset ($ arr [$ type ]['self ' ])?$ arr [$ type ]['self ' ]:[], isset ($ arr [$ type ]['refer ' ])?$ arr [$ type ]['refer ' ]:[]);
175
201
}
176
202
}
177
203
@@ -193,7 +219,6 @@ function getDiffSql()
193
219
return $ this ->diff_sql ;
194
220
}
195
221
196
-
197
222
/**
198
223
* getExecuteSql
199
224
*
@@ -215,7 +240,6 @@ function getExecuteSql($arr, $type)
215
240
continue ;
216
241
}
217
242
218
-
219
243
if (in_array ($ type , ['ADD_VIEW ' , 'DROP_VIEW ' , 'ADD_TRIGGER ' , 'DROP_TRIGGER ' , 'ADD_EVENT ' , 'DROP_EVENT ' , 'ADD_FUNCTION ' , 'DROP_FUNCTION ' , 'ADD_PROCEDURE ' , 'DROP_PROCEDURE ' ])) {
220
244
221
245
$ sql = strpos ($ type , 'ADD ' ) !== false ? $ rows : str_replace ('_ ' , '' , $ rows ) . '' . $ table ;
@@ -225,7 +249,6 @@ function getExecuteSql($arr, $type)
225
249
continue ;
226
250
}
227
251
228
-
229
252
foreach ($ rows as $ key => $ val ) {
230
253
switch ($ type ) {
231
254
case 'MODIFY_FIELD ' :
@@ -305,21 +328,21 @@ static function array_diff_assoc_recursive($array1, $array2, $exclude = [])
305
328
{
306
329
$ ret = array ();
307
330
331
+ if ($ array1 ){
332
+ foreach ($ array1 as $ k => $ v ) {
333
+ if ($ exclude && in_array ($ k , $ exclude )) {
334
+ continue ;
335
+ }
336
+ if (!isset ($ array2 [$ k ])) $ ret [$ k ] = $ v ;
337
+ else if (is_array ($ v ) && is_array ($ array2 [$ k ])) $ ret [$ k ] = self ::array_diff_assoc_recursive ($ v , $ array2 [$ k ]);
338
+ else if ($ v != $ array2 [$ k ]) $ ret [$ k ] = $ v ;
339
+ else {
340
+ unset($ array1 [$ k ]);
341
+ }
308
342
309
- foreach ($ array1 as $ k => $ v ) {
310
-
311
- if ($ exclude && in_array ($ k , $ exclude )) {
312
- continue ;
313
- }
314
-
315
- if (!isset ($ array2 [$ k ])) $ ret [$ k ] = $ v ;
316
- else if (is_array ($ v ) && is_array ($ array2 [$ k ])) $ ret [$ k ] = self ::array_diff_assoc_recursive ($ v , $ array2 [$ k ]);
317
- else if ($ v != $ array2 [$ k ]) $ ret [$ k ] = $ v ;
318
- else {
319
- unset($ array1 [$ k ]);
320
343
}
321
-
322
344
}
345
+
323
346
return array_filter ($ ret );
324
347
}
325
348
@@ -339,7 +362,6 @@ function execute()
339
362
340
363
if ($ diff_sqls ) {
341
364
342
-
343
365
$ add_tables =isset ($ diff_sqls ['ADD_TABLE ' ])?$ diff_sqls ['ADD_TABLE ' ]:null ;
344
366
345
367
if ($ add_tables ){
@@ -422,7 +444,7 @@ function manuallySelectUpdates()
422
444
/**
423
445
* getStructure
424
446
*
425
- * @param $resource
447
+ * @param \Mysqli $resource
426
448
*
427
449
* @return array
428
450
* @Author : 9rax.dev@gmail.com
@@ -458,11 +480,9 @@ private function getStructure($resource)
458
480
459
481
$ constraints [$ row ['Name ' ]] = $ consrt ;
460
482
461
-
462
483
$ show_create [$ row ['Name ' ]] = $ this ->remove_auto_increment ?preg_replace ('/AUTO_INCREMENT=[^\s]*/ ' ,'' ,$ sql ['Create Table ' ]):$ sql ['Create Table ' ];
463
484
464
485
$ tables [] = $ row ['Name ' ];
465
-
466
486
}
467
487
468
488
ksort ($ alert_columns );
0 commit comments