81 lines
2.5 KiB
PHP
81 lines
2.5 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;
|
|
|
|
public const EXIT_SHARED_NOT_CONFIGURED = 13;
|
|
|
|
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'] === 'bootstrap') {
|
|
$this->components->warn('当前仍使用本机临时引导库,尚未接入团队共享系统数据库;本次没有测试远程连接。');
|
|
$this->line('下一步:在“系统设置 → 数据库管理 → 系统数据库”中输入一次负责人提供的系统数据库密码。');
|
|
|
|
return self::EXIT_SHARED_NOT_CONFIGURED;
|
|
}
|
|
|
|
if ($settings['mode'] !== 'shared' || ! config('upstreams.stored_configuration.enabled', false)) {
|
|
return $this->failed(
|
|
'共享配置未完整启用,请通过系统数据库连接页面重新保存;不要仅手工修改运行模式。',
|
|
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;
|
|
}
|
|
}
|