mirror of
https://gitee.com/ulthon/ulthon_admin.git
synced 2026-07-06 10:02:49 +08:00
feat(scheme): 新增数据库表结构同步方案
This commit is contained in:
25
extend/base/common/command/scheme/Backup.php
Normal file
25
extend/base/common/command/scheme/Backup.php
Normal file
@@ -0,0 +1,25 @@
|
||||
<?php
|
||||
|
||||
namespace base\common\command\scheme;
|
||||
|
||||
use think\console\Command;
|
||||
use think\console\Input;
|
||||
use think\console\Output;
|
||||
use think\facade\Db;
|
||||
|
||||
class Backup extends Command
|
||||
{
|
||||
protected function configure()
|
||||
{
|
||||
$this->setName('scheme:backups')
|
||||
->setDescription('List all backup tables');
|
||||
}
|
||||
|
||||
protected function execute(Input $input, Output $output)
|
||||
{
|
||||
$tables = Db::query("SHOW TABLES LIKE 'ul_backup%'");
|
||||
foreach ($tables as $t) {
|
||||
$output->writeln(array_values($t)[0]);
|
||||
}
|
||||
}
|
||||
}
|
||||
73
extend/base/common/command/scheme/Make.php
Normal file
73
extend/base/common/command/scheme/Make.php
Normal file
@@ -0,0 +1,73 @@
|
||||
<?php
|
||||
|
||||
namespace base\common\command\scheme;
|
||||
|
||||
use think\console\Command;
|
||||
use think\console\Input;
|
||||
use think\console\input\Argument;
|
||||
use think\console\Output;
|
||||
use think\facade\Db;
|
||||
use think\facade\Config;
|
||||
use app\common\service\scheme\DbToSchemeService;
|
||||
|
||||
class Make extends Command
|
||||
{
|
||||
protected function configure()
|
||||
{
|
||||
$this->setName('scheme:make')
|
||||
->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');
|
||||
$service = new DbToSchemeService();
|
||||
|
||||
$tables = [];
|
||||
if ($table) {
|
||||
$tables[] = $table;
|
||||
} else {
|
||||
// 获取所有表
|
||||
$allTables = Db::getTables();
|
||||
// 过滤掉忽略的表
|
||||
$config = Config::get('scheme.ignore_tables', []);
|
||||
$prefix = Config::get('database.connections.mysql.prefix');
|
||||
|
||||
foreach ($allTables as $t) {
|
||||
// 如果有前缀,去除前缀后再判断
|
||||
$shortName = $t;
|
||||
if ($prefix && str_starts_with($t, $prefix)) {
|
||||
$shortName = substr($t, strlen($prefix));
|
||||
}
|
||||
|
||||
if (!in_array($shortName, $config) && !in_array($t, $config)) {
|
||||
$tables[] = $shortName;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($tables as $t) {
|
||||
$output->writeln("Processing table: $t");
|
||||
try {
|
||||
$code = $service->generate($t);
|
||||
|
||||
// 提取类名以确定文件名
|
||||
if (preg_match('/class\s+(\w+)/', $code, $matches)) {
|
||||
$className = $matches[1];
|
||||
$path = app()->getAppPath() . 'admin/scheme/' . $className . '.php';
|
||||
|
||||
// 确保目录存在
|
||||
if (!is_dir(dirname($path))) {
|
||||
mkdir(dirname($path), 0755, true);
|
||||
}
|
||||
|
||||
file_put_contents($path, $code);
|
||||
$output->writeln("<info>Generated: $path</info>");
|
||||
}
|
||||
} catch (\Exception $e) {
|
||||
$output->writeln("<error>Error processing $t: " . $e->getMessage() . "</error>");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
53
extend/base/common/command/scheme/Sync.php
Normal file
53
extend/base/common/command/scheme/Sync.php
Normal file
@@ -0,0 +1,53 @@
|
||||
<?php
|
||||
|
||||
namespace base\common\command\scheme;
|
||||
|
||||
use think\console\Command;
|
||||
use think\console\Input;
|
||||
use think\console\input\Option;
|
||||
use think\console\Output;
|
||||
use think\facade\App;
|
||||
use app\common\service\scheme\SchemeToDbService;
|
||||
|
||||
class Sync extends Command
|
||||
{
|
||||
protected function 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');
|
||||
}
|
||||
|
||||
protected function execute(Input $input, Output $output)
|
||||
{
|
||||
$skipData = $input->getOption('skip-data');
|
||||
|
||||
$service = new SchemeToDbService();
|
||||
$schemeDir = app()->getAppPath() . 'admin/scheme/';
|
||||
|
||||
if (!is_dir($schemeDir)) {
|
||||
$output->writeln("<error>Scheme directory not found: $schemeDir</error>");
|
||||
return;
|
||||
}
|
||||
|
||||
$files = glob($schemeDir . '*.php');
|
||||
foreach ($files as $file) {
|
||||
require_once $file;
|
||||
$className = 'app\\admin\\scheme\\' . basename($file, '.php');
|
||||
|
||||
if (class_exists($className)) {
|
||||
$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>");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user