DiffDb.php
4.8 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
145
146
147
148
149
150
151
152
153
154
155
156
157
<?php
namespace App\Console\Commands;
use App\Helper\Arr;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\DB;
use Symfony\Component\Process\Process;
class DiffDb extends Command
{
protected $signature = 'project:diff_db';
/**
* The console command description.
*
* @var string
*/
protected $description = '对比数据库结构';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
public function handle()
{
//C端
// $process = new Process(['git', 'pull']);
// $process->run();
// dump($process->getExitCodeText());
// dump($process->getExitCode());
// dump($process->getErrorOutput());
// $output = explode(PHP_EOL, $process->getOutput());
// dump($output);
// exit;
$custom_mysql_config = [
'database.connections.custom_mysql.host' => '43.154.15.250',
'database.connections.custom_mysql.port' => '6063',
'database.connections.custom_mysql.database' => 'globalso_v6',
'database.connections.custom_mysql.username' => 'globalso_v6',
'database.connections.custom_mysql.password' => 'PFxFdzj4ha6w7T3j',
];
config($custom_mysql_config);
$this->output->writeln("开始进行数据表对比!");
$tablesSource = DB::select("show tables");
$tablesRemote = DB::connection('custom_mysql')->select("show tables");
$tablesSource = array_map(function($item){
return Arr::first($item);
}, $tablesSource);
$tablesRemote = array_map(function($item){
return Arr::first($item);
}, $tablesRemote);
$tablesNotInRemote = [];
$tablesInRemote = [];
foreach ($tablesSource as $t) {
if (!in_array($t, $tablesRemote)) {
$tablesNotInRemote[] = $t;
} else {
$tablesInRemote[] = $t;
}
}
//print reports
echo "本地存在,但是不在custom_mysql中的表:" . PHP_EOL;
echo ">>>>>>==================================<<<<<<" . PHP_EOL;
foreach ($tablesNotInRemote as $t) {
echo $t . PHP_EOL;
}
echo ">>>>>>==================================<<<<<<" . PHP_EOL . PHP_EOL . PHP_EOL;
echo "本地与custom_mysql结构不一致的表:" . PHP_EOL;
echo ">>>>>>==================================<<<<<<" . PHP_EOL;
$only127 = $onlyRemote = $modify127 = [];
foreach ($tablesInRemote as $t) {
$columns127 = DB::select("show columns from `{$t}`");
foreach ($columns127 as &$item){
$item = get_object_vars($item);
}
$columnsRemote = DB::connection('custom_mysql')->select("show columns from `{$t}`");
foreach ($columnsRemote as &$item){
$item = get_object_vars($item);
}
$fields127 = $fieldsRemote = [];
foreach ($columns127 as $v) {
$fields127[$v['Field']] = $v;
}
foreach ($columnsRemote as $v) {
$fieldsRemote[$v['Field']] = $v;
}
foreach ($fields127 as $f => $column) {
if (!isset($fieldsRemote[$f])) {
$only127[$t][] = $f;
} else if ($column !== $fieldsRemote[$f]) {
dump($column);
dump($fieldsRemote[$f]);
$modify127[$t][] = $f;
}
}
foreach ($fieldsRemote as $f => $column) {
if (!isset($fields127[$f])) {
$onlyRemote[$t][] = $f;
}
}
}
if (!empty($only127)) {
echo "只本地存在:" . PHP_EOL;
foreach ($only127 as $t => $columns) {
echo $t . ":";
foreach ($columns as $field) {
echo $field . "\t";
}
echo PHP_EOL;
}
}
if (!empty($onlyRemote)) {
echo "只custom_mysql存在:" . PHP_EOL;
foreach ($onlyRemote as $t => $columns) {
echo $t . ":";
foreach ($columns as $field) {
echo $field . "\t";
}
echo PHP_EOL;
}
}
if (!empty($modify127)) {
echo "本地更新:" . PHP_EOL;
foreach ($modify127 as $t => $columns) {
echo $t . ":";
foreach ($columns as $field) {
echo $field . "\t";
}
echo PHP_EOL;
}
}
echo ">>>>>>==================================<<<<<<" . PHP_EOL . PHP_EOL . PHP_EOL;
}
}