-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmysqlCompare.php
More file actions
144 lines (123 loc) · 4.13 KB
/
mysqlCompare.php
File metadata and controls
144 lines (123 loc) · 4.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
<?php
class MysqlCompare {
public $dbinfo;
public $db1;
public $db2;
function __construct() {
$this->dbinfo = parse_ini_file("db.settings.ini", 'db1');
try {
$this->db1 = new PDO('mysql:host='.$this->dbinfo['db1']['host'].';dbname='.$this->dbinfo['db1']['dbname'], $this->dbinfo['db1']['username'], $this->dbinfo['db1']['password'], array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));
} catch(Exception $e) {
print "There was a problem connecting to DB1. Please check the settings within db.settings.ini.";
die();
}
try {
$this->db2 = new PDO('mysql:host='.$this->dbinfo['db2']['host'].';dbname='.$this->dbinfo['db2']['dbname'], $this->dbinfo['db2']['username'], $this->dbinfo['db2']['password'], array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));
} catch(Exception $e) {
print "There was a problem connecting to DB2. Please check the settings within db.settings.ini.";
die();
}
}
function getDBOne() {
try {
$stmt = $this->db1->query("SHOW tables FROM ".$this->dbinfo['db1']['dbname']);
$tables = $stmt->fetchAll(PDO::FETCH_COLUMN);
$dbase_one = array();
foreach($tables as $table) {
$fields = $this->db1->query("SHOW FIELDS FROM ".$table);
$table_fields = $fields->fetchAll(PDO::FETCH_ASSOC);
$dbase_one[$table] = $table_fields;
}
return $dbase_one;
} catch(Exception $e) {
print "There was a problem running the query on your DB1:<br><br>";
print $e->getMessage();
die();
}
}
function getDBTwo() {
try {
$stmt = $this->db2->query("SHOW tables FROM ".$this->dbinfo['db2']['dbname']);
$tables = $stmt->fetchAll(PDO::FETCH_COLUMN);
$dbase_two = array();
foreach($tables as $table) {
$fields = $this->db2->query("SHOW FIELDS FROM ".$table);
$table_fields = $fields->fetchAll(PDO::FETCH_ASSOC);
$dbase_two[$table] = $table_fields;
}
return $dbase_two;
} catch(Exception $e) {
print "There was a problem running the query on your DB2:<br><br>";
print $e->getMessage();
die();
}
}
function getDBNameOne() {
return $this->dbinfo['db1']['dbname'];
}
function getDBNameTwo() {
return $this->dbinfo['db2']['dbname'];
}
function compareDatabases() {
try {
$one = $this->getDBOne();
$two = $this->getDBTwo();
// TABLE DIFFS
$diffOne = array_diff(array_keys($one), array_keys($two));
$diffTwo = array_diff(array_keys($two), array_keys($one));
$intOne = array();
$intTwo = array();
// LOOP THROUGH ONE CHECKING DIFFERENCES ON TWO
$retOne = array();
foreach($one as $key=>$val) {
if(!isset($two[$key])) {
$retOne[$key]['class'] = 'diff';
} else {
$retOne[$key]['class'] = null;
foreach($val as $k=>$v) {
$retOne[$key][$k]['name'] = $v['Field'];
if(!isset($two[$key][$k]) || (isset($two[$key][$k]) && $two[$key][$k]['Field'] != $v['Field'])) {
$retOne[$key][$k]['class'] = 'diff';
} else {
$retOne[$key][$k]['class'] = null;
foreach($v as $x=>$y) {
if($two[$key][$k][$x] != $y) {
$retOne[$key][$k][$x] = array('val'=>$y, 'class'=>'diff');
} else {
$retOne[$key][$k][$x] = array('val'=>$y, 'class'=>null);
}
}
}
}
}
}
$retTwo = array();
foreach($two as $key=>$val) {
if(!isset($one[$key])) {
$retTwo[$key]['class'] = 'diff';
} else {
$retTwo[$key]['class'] = null;
foreach($val as $k=>$v) {
$retTwo[$key][$k]['name'] = $v['Field'];
if(!isset($one[$key][$k]) || (isset($one[$key][$k]) && $one[$key][$k]['Field'] != $v['Field'])) {
$retTwo[$key][$k]['class'] = 'diff';
} else {
$retTwo[$key][$k]['class'] = null;
foreach($v as $x=>$y) {
if($one[$key][$k][$x] != $y) {
$retTwo[$key][$k][$x] = array('val'=>$y, 'class'=>'diff');
} else {
$retTwo[$key][$k][$x] = array('val'=>$y, 'class'=>null);
}
}
}
}
}
}
return array($retOne, $retTwo);
} catch(Exception $e) {
print $e->getMessage();
die();
}
}
}