CaiWuXiTong/app/Console/Commands/VerifySystemDatabase.php

76 lines
2.1 KiB
PHP

<?php
namespace App\Console\Commands;
use App\Utilities\SystemDatabaseConfiguration;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use Throwable;
class VerifySystemDatabase extends Command
{
public const EXIT_CONFIG_NOT_LOADED = 10;
public const EXIT_DATABASE_CONNECTION_FAILED = 11;
public const EXIT_SYSTEM_TABLE_VALIDATION_FAILED = 12;
protected $signature = 'system-database:verify';
protected $description = '在新的 Laravel 进程中验证当前共享系统数据库';
public function handle(SystemDatabaseConfiguration $configuration): int
{
try {
$settings = $configuration->current();
} catch (Throwable) {
return $this->failed(
'新配置未被 Laravel 加载。',
self::EXIT_CONFIG_NOT_LOADED
);
}
if (
$settings['mode'] !== 'shared'
|| ! config('upstreams.stored_configuration.enabled', false)
|| config('upstreams.local_configuration.enabled', false)
) {
return $this->failed(
'新配置未被 Laravel 加载。',
self::EXIT_CONFIG_NOT_LOADED
);
}
try {
DB::purge((string) $settings['connection']);
DB::reconnect((string) $settings['connection']);
DB::selectOne('SELECT 1');
} catch (Throwable) {
return $this->failed(
'Laravel 数据库连接失败。',
self::EXIT_DATABASE_CONNECTION_FAILED
);
}
try {
$configuration->probe($settings);
} catch (Throwable) {
return $this->failed(
'系统表验证失败。',
self::EXIT_SYSTEM_TABLE_VALIDATION_FAILED
);
}
$this->components->info('共享系统数据库已通过应用级验证。');
return self::SUCCESS;
}
private function failed(string $message, int $exitCode): int
{
$this->components->error($message);
return $exitCode;
}
}