From ec757f2e9f0e2b422844d47c36278cbc6674a898 Mon Sep 17 00:00:00 2001 From: augushong Date: Fri, 9 Jan 2026 22:09:56 +0800 Subject: [PATCH] =?UTF-8?q?feat(=E5=90=8C=E6=AD=A5=E5=91=BD=E4=BB=A4):=20?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=BF=BD=E7=95=A5=E8=A1=A8=E5=8A=9F=E8=83=BD?= =?UTF-8?q?=E5=B9=B6=E4=BC=98=E5=8C=96=E8=A1=A8=E5=90=8D=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config/scheme.php | 1 + extend/base/common/command/scheme/Sync.php | 43 +++++++++++++++++++++- 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/config/scheme.php b/config/scheme.php index 83d4d06..7d1dca9 100644 --- a/config/scheme.php +++ b/config/scheme.php @@ -5,6 +5,7 @@ return [ 'ignore_tables' => [ 'migrations', 'phinxlog', + 'debug_log', ], // 备份中间前缀 'backup_prefix' => 'backup', diff --git a/extend/base/common/command/scheme/Sync.php b/extend/base/common/command/scheme/Sync.php index 1baa143..bb91533 100644 --- a/extend/base/common/command/scheme/Sync.php +++ b/extend/base/common/command/scheme/Sync.php @@ -6,8 +6,10 @@ use think\console\Command; use think\console\Input; use think\console\input\Option; use think\console\Output; -use think\facade\App; +use think\facade\Config; use app\common\service\scheme\SchemeToDbService; +use app\common\scheme\attribute\Table; +use ReflectionClass; class Sync extends Command { @@ -25,6 +27,9 @@ class Sync extends Command $service = new SchemeToDbService(); $schemeDir = app()->getAppPath() . 'admin/scheme/'; + $ignoreTables = Config::get('scheme.ignore_tables', []); + $connection = Config::get('database.default', 'mysql'); + $prefix = Config::get('database.connections.' . $connection . '.prefix', ''); if (!is_dir($schemeDir)) { $output->writeln("Scheme directory not found: $schemeDir"); @@ -37,6 +42,12 @@ class Sync extends Command $className = 'app\\admin\\scheme\\' . basename($file, '.php'); if (class_exists($className)) { + $tableName = $this->getTableNameFromScheme($className); + if (!empty($tableName) && $this->isIgnoredTable($tableName, $ignoreTables, $prefix)) { + $output->writeln("Skipping $className (ignored table: $tableName)"); + continue; + } + $output->writeln("Syncing $className..."); try { $backup = $service->sync($className, $skipData); @@ -50,4 +61,34 @@ class Sync extends Command } } } + + protected function getTableNameFromScheme(string $className): string + { + try { + $ref = new ReflectionClass($className); + $tableAttrs = $ref->getAttributes(Table::class); + if (empty($tableAttrs)) { + return ''; + } + + $tableAttr = $tableAttrs[0]->newInstance(); + return (string)($tableAttr->name ?? ''); + } catch (\Throwable $e) { + return ''; + } + } + + protected function isIgnoredTable(string $tableName, array $ignoreTables, string $prefix): bool + { + if (empty($ignoreTables)) { + return false; + } + + $shortName = $tableName; + if ($prefix && str_starts_with($tableName, $prefix)) { + $shortName = substr($tableName, strlen($prefix)); + } + + return in_array($shortName, $ignoreTables, true) || in_array($tableName, $ignoreTables, true); + } }