|
|
|
<?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;
|
|
|
|
}
|
|
|
|
} |
...
|
...
|
|