mirror of
https://gitee.com/ulthon/ulthon_admin.git
synced 2026-07-01 15:32:48 +08:00
feat: 优化迁移文件路径命令,直接使用sql功能;
This commit is contained in:
@@ -6,7 +6,9 @@ namespace base\common\command\admin;
|
||||
|
||||
use think\console\Command;
|
||||
use think\console\Input;
|
||||
use think\console\input\Option;
|
||||
use think\console\Output;
|
||||
use think\facade\Db;
|
||||
|
||||
class MigrateFileDataBase extends Command
|
||||
{
|
||||
@@ -40,6 +42,7 @@ class MigrateFileDataBase extends Command
|
||||
{
|
||||
// 指令配置
|
||||
$this->setName('admin:migrate:file:data')
|
||||
->addOption('exec', null, Option::VALUE_NONE, '确认执行')
|
||||
->setDescription('将数据库中的文件地址从老地址更新到新地址,只处理数据库数据,不会实际迁移文件');
|
||||
}
|
||||
|
||||
@@ -93,31 +96,38 @@ class MigrateFileDataBase extends Command
|
||||
}
|
||||
}
|
||||
|
||||
$field_map = array_filter($field_map);
|
||||
$from_domain_list = $this->getFromDomainList();
|
||||
$to_domain = $this->toDomain;
|
||||
|
||||
$exec = $input->hasOption('exec');
|
||||
if(!$exec) {
|
||||
$output->writeln("输出模式,生成即将使用的sql,但不执行。");
|
||||
$output->writeln("需要确认执行: php think admin:migrate:file:data --exec");
|
||||
}
|
||||
|
||||
$field_map = array_filter($field_map);
|
||||
foreach ($field_map as $model_class => $field_list) {
|
||||
$output->writeln("开始处理 {$model_class}");
|
||||
$model_cursor = $model_class::cursor();
|
||||
foreach ($model_cursor as $modle_item) {
|
||||
foreach ($field_list as $field_name) {
|
||||
$field_value = $modle_item->{$field_name};
|
||||
$new_field_value = $this->replaceDomain($field_value);
|
||||
if ($field_value != $new_field_value) {
|
||||
$modle_item->{$field_name} = $new_field_value;
|
||||
$modle_item->save();
|
||||
$output->writeln("{$model_class} - {$field_name} - {$field_value} => {$new_field_value}");
|
||||
$output->writeln("# process model {$model_class}");
|
||||
foreach ($field_list as $field_name) {
|
||||
$output->writeln("# update field: {$field_name}");
|
||||
foreach ($from_domain_list as $domain) {
|
||||
$result = $model_class::whereLike($field_name, "%$domain%")
|
||||
->fetchSql(!$exec)
|
||||
->update([
|
||||
$field_name => Db::raw("REPLACE({$field_name}, '{$domain}', '{$to_domain}')"),
|
||||
]);
|
||||
if($exec){
|
||||
$output->writeln("exec affect rows: $result");
|
||||
}else{
|
||||
$output->writeln($result);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private function replaceDomain($field_value)
|
||||
private function getFromDomainList()
|
||||
{
|
||||
if (empty($field_value)) {
|
||||
return $field_value;
|
||||
}
|
||||
|
||||
$from_domain_list = [];
|
||||
foreach ($this->fromDomain as $from_domain) {
|
||||
if (!str_starts_with($from_domain, 'http://') && !str_starts_with($from_domain, 'https://')) {
|
||||
@@ -128,9 +138,7 @@ class MigrateFileDataBase extends Command
|
||||
}
|
||||
}
|
||||
|
||||
$to_domain = $this->toDomain;
|
||||
$field_value = str_replace($from_domain_list, $to_domain, $field_value);
|
||||
|
||||
return $field_value;
|
||||
return $from_domain_list;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user