Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit eccb210

Browse files
committed
fix notice error
1 parent 7c4d3b0 commit eccb210

File tree

3 files changed

+66
-48
lines changed

3 files changed

+66
-48
lines changed

‎MysqlStructSync.php‎

Lines changed: 56 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,11 @@ class MysqlStructSync
3535

3636
private $remove_auto_increment = false;
3737

38-
39-
38+
/**
39+
* @Author : 9rax.dev@gmail.com
40+
* @DateTime: 2019年8月30日 19:31
41+
* @var array
42+
*/
4043
static $advance = [
4144
'VIEW' => ["SELECT TABLE_NAME as Name FROM information_schema.VIEWS WHERE TABLE_SCHEMA='#'", 'Create View'],
4245
'TRIGGER' => ["SELECT TRIGGER_NAME as Name FROM information_schema.TRIGGERS WHERE TRIGGER_SCHEMA='#'", 'SQL Original Statement'],
@@ -54,11 +57,12 @@ class MysqlStructSync
5457
public function __construct($old_database_config, $new_database_config)
5558
{
5659

57-
$this->self_connection = new \Mysqli($old_database_config['host'],
60+
$this->self_connection = new \Mysqli(
61+
$old_database_config['host'],
5862
$old_database_config['username'],
5963
$old_database_config['passwd'],
6064
$old_database_config['dbname'],
61-
$old_database_config['port']
65+
isset($old_database_config['port'])?$old_database_config['port']:3306
6266
);
6367

6468

@@ -72,7 +76,7 @@ public function __construct($old_database_config, $new_database_config)
7276
$new_database_config['username'],
7377
$new_database_config['passwd'],
7478
$new_database_config['dbname'],
75-
$new_database_config['port']
79+
isset($new_database_config['port'])?$new_database_config['port']:3306
7680
);
7781

7882

@@ -108,13 +112,12 @@ function baseDiff()
108112
$this->self_database_struct = $this->getStructure($this->self_connection);
109113
$this->refer_database_struct = $this->getStructure($this->refer_connection);
110114

111-
112115
$res['ADD_TABLE'] = array_diff($this->refer_database_struct['tables'], $this->self_database_struct['tables']);
113116
$res['DROP_TABLE'] = array_diff($this->self_database_struct['tables'], $this->refer_database_struct['tables']);
114117

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']);
118121

119122

120123
if ($develop_columns) {
@@ -145,6 +148,30 @@ function baseDiff()
145148

146149
}
147150

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+
148175

149176
/**
150177
* advanceDiff
@@ -163,15 +190,14 @@ function advanceDiff()
163190
$sql = str_replace('#', $this->$db, $list_sql[0]);
164191
$connect = $this->$conn->query($sql);
165192
$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+
}
171198
}
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']:[]);
175201
}
176202
}
177203

@@ -193,7 +219,6 @@ function getDiffSql()
193219
return $this->diff_sql;
194220
}
195221

196-
197222
/**
198223
* getExecuteSql
199224
*
@@ -215,7 +240,6 @@ function getExecuteSql($arr, $type)
215240
continue;
216241
}
217242

218-
219243
if (in_array($type, ['ADD_VIEW', 'DROP_VIEW', 'ADD_TRIGGER', 'DROP_TRIGGER', 'ADD_EVENT', 'DROP_EVENT', 'ADD_FUNCTION', 'DROP_FUNCTION', 'ADD_PROCEDURE', 'DROP_PROCEDURE'])) {
220244

221245
$sql = strpos($type, 'ADD') !== false ? $rows : str_replace('_', '', $rows) . '' . $table;
@@ -225,7 +249,6 @@ function getExecuteSql($arr, $type)
225249
continue;
226250
}
227251

228-
229252
foreach ($rows as $key => $val) {
230253
switch ($type) {
231254
case 'MODIFY_FIELD':
@@ -305,21 +328,21 @@ static function array_diff_assoc_recursive($array1, $array2, $exclude = [])
305328
{
306329
$ret = array();
307330

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+
}
308342

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]);
320343
}
321-
322344
}
345+
323346
return array_filter($ret);
324347
}
325348

@@ -339,7 +362,6 @@ function execute()
339362

340363
if ($diff_sqls) {
341364

342-
343365
$add_tables=isset($diff_sqls['ADD_TABLE'])?$diff_sqls['ADD_TABLE']:null;
344366

345367
if($add_tables){
@@ -422,7 +444,7 @@ function manuallySelectUpdates()
422444
/**
423445
* getStructure
424446
*
425-
* @param $resource
447+
* @param \Mysqli $resource
426448
*
427449
* @return array
428450
* @Author : 9rax.dev@gmail.com
@@ -458,11 +480,9 @@ private function getStructure($resource)
458480

459481
$constraints[$row['Name']] = $consrt;
460482

461-
462483
$show_create[$row['Name']] = $this->remove_auto_increment?preg_replace('/AUTO_INCREMENT=[^\s]*/','',$sql['Create Table']):$sql['Create Table'];
463484

464485
$tables[] = $row['Name'];
465-
466486
}
467487

468488
ksort($alert_columns);

‎README.md‎

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,10 @@ composer require 9raxdev/mysql-struct-sync
3434

3535
include __DIR__.'/MysqlStructSync.php';
3636

37-
function p($arr)
38-
{
39-
echo '<pre>' . print_r($arr, true) . '</pre>';
40-
}
37+
38+
$local_database_config=['host'=>'127.0.0.1','username'=>'root','passwd'=>'root','dbname'=>'test','port'=>3306];
4139

42-
$local_database_config=['host'=>'127.0.0.1','username'=>'root','passwd'=>'root','dbname'=>'test'];
43-
44-
$develop_database_config=['host'=>'127.0.0.1','username'=>'root','passwd'=>'root','dbname'=>'sakila'];
40+
$develop_database_config=['host'=>'127.0.0.1','username'=>'root','passwd'=>'root','dbname'=>'sakila','port'=>3306];
4541

4642
//把local数据库结构更新为develop数据库结构
4743
$compare=new \linge\MysqlStructSync($local_database_config,$develop_database_config);
@@ -53,12 +49,12 @@ $compare->baseDiff(); //TABLE COLUMNS(ADD,DROP,MODIFY) CONSTRAINTS(PK,FK,index,
5349
$compare->advanceDiff(); //VIEW TRIGGER EVENT FUNCTION PROCEDURE (ADD,DROP)
5450

5551
$diff_sql=$compare->getDiffSql();
56-
//p($diff_sql);
52+
//print_r($diff_sql);
5753

5854
/*******************************************/
5955
//用法一:自动执行全部差异语句,更新结构
6056
//$execute_sql_stat=$compare->execute();
61-
//p($execute_sql_stat);
57+
//print_r($execute_sql_stat);
6258

6359

6460
//用法二:手动选择要执行的差异语句,记住:选择储存过程,函数等请确保数据库表已经同步

‎test.php‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
<?php
22

3+
error_reporting(E_ALL);
4+
35
include __DIR__.'/MysqlStructSync.php';
46

57
function p($arr)
68
{
79
echo '<pre>' . print_r($arr, true) . '</pre>';
810
}
911

10-
$local_database_config=['host'=>'127.0.0.1','username'=>'root','passwd'=>'root','dbname'=>'test'];
12+
$local_database_config=['host'=>'127.0.0.1','username'=>'root','passwd'=>'root','dbname'=>'old_database','port'=>3306];
1113

12-
$develop_database_config=['host'=>'127.0.0.1','username'=>'root','passwd'=>'root','dbname'=>'sakila'];
14+
$develop_database_config=['host'=>'127.0.0.1','username'=>'root','passwd'=>'root','dbname'=>'develop_database','port'=>3306];
1315

1416
//把local数据库结构更新为develop数据库结构
1517
$compare=new \linge\MysqlStructSync($local_database_config,$develop_database_config);
@@ -30,6 +32,6 @@ function p($arr)
3032

3133

3234
//用法二:手动选择要执行的差异语句,记住:选择储存过程,函数等请确保数据库表已经同步
33-
$compare->manuallySelectUpdates();
35+
$compare->manuallySelectUpdates();
3436

3537

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /