mirror of
https://gitee.com/ulthon/ulthon_admin.git
synced 2026-07-08 19:12:48 +08:00
feat(同步命令): 添加忽略表功能并优化表名处理
This commit is contained in:
@@ -5,6 +5,7 @@ return [
|
|||||||
'ignore_tables' => [
|
'ignore_tables' => [
|
||||||
'migrations',
|
'migrations',
|
||||||
'phinxlog',
|
'phinxlog',
|
||||||
|
'debug_log',
|
||||||
],
|
],
|
||||||
// 备份中间前缀
|
// 备份中间前缀
|
||||||
'backup_prefix' => 'backup',
|
'backup_prefix' => 'backup',
|
||||||
|
|||||||
@@ -6,8 +6,10 @@ use think\console\Command;
|
|||||||
use think\console\Input;
|
use think\console\Input;
|
||||||
use think\console\input\Option;
|
use think\console\input\Option;
|
||||||
use think\console\Output;
|
use think\console\Output;
|
||||||
use think\facade\App;
|
use think\facade\Config;
|
||||||
use app\common\service\scheme\SchemeToDbService;
|
use app\common\service\scheme\SchemeToDbService;
|
||||||
|
use app\common\scheme\attribute\Table;
|
||||||
|
use ReflectionClass;
|
||||||
|
|
||||||
class Sync extends Command
|
class Sync extends Command
|
||||||
{
|
{
|
||||||
@@ -25,6 +27,9 @@ class Sync extends Command
|
|||||||
|
|
||||||
$service = new SchemeToDbService();
|
$service = new SchemeToDbService();
|
||||||
$schemeDir = app()->getAppPath() . 'admin/scheme/';
|
$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)) {
|
if (!is_dir($schemeDir)) {
|
||||||
$output->writeln("<error>Scheme directory not found: $schemeDir</error>");
|
$output->writeln("<error>Scheme directory not found: $schemeDir</error>");
|
||||||
@@ -37,6 +42,12 @@ class Sync extends Command
|
|||||||
$className = 'app\\admin\\scheme\\' . basename($file, '.php');
|
$className = 'app\\admin\\scheme\\' . basename($file, '.php');
|
||||||
|
|
||||||
if (class_exists($className)) {
|
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...");
|
$output->writeln("Syncing $className...");
|
||||||
try {
|
try {
|
||||||
$backup = $service->sync($className, $skipData);
|
$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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user