feat(scheme): 增强 Scheme 与数据库同步机制并添加严格校验

This commit is contained in:
augushong
2026-01-12 12:37:37 +08:00
parent 2f7ec93f89
commit ee40374732
5 changed files with 612 additions and 128 deletions

View File

@@ -5,24 +5,28 @@ namespace base\common\command\scheme;
use think\console\Command;
use think\console\Input;
use think\console\input\Argument;
use think\console\input\Option;
use think\console\Output;
use think\facade\Db;
use think\facade\Config;
use app\common\service\scheme\DbToSchemeService;
use app\common\service\scheme\SchemeToDbService;
class Make extends Command
{
protected function configure()
{
$this->setName('scheme:make')
->addOption('table', 't', Option::VALUE_REQUIRED, "The table name (without prefix)")
->addArgument('table', Argument::OPTIONAL, "The table name (without prefix)")
->setDescription('Generate Scheme class from Database table');
}
protected function execute(Input $input, Output $output)
{
$table = $input->getArgument('table');
$table = $input->getOption('table') ?: $input->getArgument('table');
$service = new DbToSchemeService();
$compare = new SchemeToDbService();
$tables = [];
if ($table) {
@@ -62,6 +66,18 @@ class Make extends Command
if (preg_match('/class\s+(\w+)/', $code, $matches)) {
$className = $matches[1];
$path = app()->getAppPath() . 'admin/scheme/' . $className . '.php';
if (is_file($path)) {
require_once $path;
$schemeClass = 'app\\admin\\scheme\\' . $className;
if (class_exists($schemeClass)) {
$diffs = $compare->diff($schemeClass);
if (empty($diffs)) {
$output->writeln("<info>Skipping (no schema changes): $path</info>");
continue;
}
}
}
// 确保目录存在
if (!is_dir(dirname($path))) {