mirror of
https://gitee.com/ulthon/ulthon_admin.git
synced 2026-07-06 10:02:49 +08:00
feat: 发布智能体版
This commit is contained in:
@@ -2,23 +2,27 @@
|
||||
|
||||
namespace base\common\command\scheme;
|
||||
|
||||
use think\console\Command;
|
||||
use app\common\console\Command;
|
||||
use app\common\service\scheme\SchemeToDbService;
|
||||
use think\console\Input;
|
||||
use think\console\input\Option;
|
||||
use think\console\Input\Option;
|
||||
use think\console\Output;
|
||||
use ReflectionClass;
|
||||
use app\common\scheme\attribute\Table;
|
||||
use think\facade\Config;
|
||||
use think\facade\Db;
|
||||
use app\common\service\scheme\SchemeToDbService;
|
||||
use app\common\scheme\attribute\Table;
|
||||
use ReflectionClass;
|
||||
|
||||
/**
|
||||
* scheme:sync 命令基类
|
||||
*/
|
||||
class Sync extends Command
|
||||
{
|
||||
protected function configure()
|
||||
{
|
||||
parent::configure();
|
||||
|
||||
$this->setName('scheme:sync')
|
||||
->addOption('skip-data', null, Option::VALUE_NONE, 'Skip data migration')
|
||||
->addOption('force', null, Option::VALUE_NONE, 'Force execution without confirmation')
|
||||
->setDescription('Synchronize Scheme classes to Database');
|
||||
}
|
||||
|
||||
@@ -32,17 +36,20 @@ class Sync extends Command
|
||||
$connection = Config::get('database.default', 'mysql');
|
||||
$prefix = Config::get('database.connections.' . $connection . '.prefix', '');
|
||||
$backupPrefix = Config::get('scheme.backup_prefix', 'backup');
|
||||
|
||||
|
||||
if (!is_dir($schemeDir)) {
|
||||
$output->writeln("<error>Scheme directory not found: $schemeDir</error>");
|
||||
$error = "Scheme directory not found: $schemeDir";
|
||||
$output->writeln("<error>$error</error>");
|
||||
return;
|
||||
}
|
||||
|
||||
$pendingSync = [];
|
||||
$files = glob($schemeDir . '*.php');
|
||||
|
||||
foreach ($files as $file) {
|
||||
require_once $file;
|
||||
$className = 'app\\admin\\scheme\\' . basename($file, '.php');
|
||||
|
||||
|
||||
if (class_exists($className)) {
|
||||
$tableName = $this->getTableNameFromScheme($className);
|
||||
if (empty($tableName)) {
|
||||
@@ -64,30 +71,62 @@ class Sync extends Command
|
||||
try {
|
||||
$diffs = $service->diff($className);
|
||||
} catch (\Throwable $e) {
|
||||
$output->writeln("<error>Check failed for $className: " . $e->getMessage() . "</error>");
|
||||
$output->error("Check failed for $className: " . $e->getMessage());
|
||||
continue;
|
||||
}
|
||||
|
||||
if (count($diffs) === 1 && str_starts_with($diffs[0], '无法读取数据库表结构')) {
|
||||
$output->writeln("<error>Check failed for $className: {$diffs[0]}</error>");
|
||||
$output->error("Check failed for $className: {$diffs[0]}");
|
||||
continue;
|
||||
}
|
||||
|
||||
if (empty($diffs)) {
|
||||
$output->writeln("Skipping $className (no schema changes)");
|
||||
continue;
|
||||
}
|
||||
|
||||
$output->writeln("Syncing $className...");
|
||||
try {
|
||||
$backup = $service->sync($className, $skipData);
|
||||
$output->writeln("<info>Success!</info>");
|
||||
if ($backup) {
|
||||
$output->writeln("Backup created: $backup");
|
||||
}
|
||||
} catch (\Exception $e) {
|
||||
$output->writeln("<error>Failed: " . $e->getMessage() . "</error>");
|
||||
$pendingSync[] = [
|
||||
'className' => $className,
|
||||
'tableName' => $fullTableName,
|
||||
'diffs' => $diffs
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
if (empty($pendingSync)) {
|
||||
$output->writeln('<info>未检测到 Scheme 变更。</info>');
|
||||
return;
|
||||
}
|
||||
|
||||
// 显示所有待同步的变更
|
||||
$output->writeln('');
|
||||
$output->writeln('<comment>========================================</comment>');
|
||||
$output->writeln('<comment>即将执行以下 Scheme 变更:</comment>');
|
||||
$output->writeln('<comment>========================================</comment>');
|
||||
foreach ($pendingSync as $item) {
|
||||
$output->writeln("<comment>表名: {$item['tableName']} ({$item['className']})</comment>");
|
||||
foreach ($item['diffs'] as $line) {
|
||||
$output->writeln(" $line");
|
||||
}
|
||||
$output->writeln('');
|
||||
}
|
||||
|
||||
// 确认(全局 -ff 可跳过)
|
||||
if (!$output->confirm($input, '确认要将这些变更应用到数据库吗?', true)) {
|
||||
$output->comment('操作已取消。');
|
||||
return;
|
||||
}
|
||||
|
||||
// 执行同步
|
||||
foreach ($pendingSync as $item) {
|
||||
$output->writeln("正在同步 {$item['className']}...");
|
||||
try {
|
||||
$backup = $service->sync($item['className'], $skipData);
|
||||
$output->writeln("<info>成功!</info>");
|
||||
if ($backup) {
|
||||
$output->writeln("已创建备份: $backup");
|
||||
}
|
||||
} catch (\Exception $e) {
|
||||
$output->writeln("<error>失败: " . $e->getMessage() . "</error>");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user