's3_access_key', 'group' => 'upload', 'value' => '填你的', 'remark' => 'S3公钥', 'sort' => 0, ], [ 'name' => 's3_secret_key', 'group' => 'upload', 'value' => '填你的', 'remark' => 'S3私钥', 'sort' => 0, ], [ 'name' => 's3_region', 'group' => 'upload', 'value' => '填你的', 'remark' => 'S3地域', 'sort' => 0, ], [ 'name' => 's3_bucket', 'group' => 'upload', 'value' => '填你的', 'remark' => 'S3空间名称', 'sort' => 0, ], [ 'name' => 's3_endpoint', 'group' => 'upload', 'value' => '', 'remark' => 'S3自定义端点(兼容MinIO等,留空用AWS官方)', 'sort' => 0, ], [ 'name' => 's3_path_style', 'group' => 'upload', 'value' => '0', 'remark' => 'S3路径风格(1开启,MinIO建议)', 'sort' => 0, ], [ 'name' => 's3_domain', 'group' => 'upload', 'value' => '填你的', 'remark' => 'S3访问域名', 'sort' => 0, ], [ 'name' => 'webdav_url', 'group' => 'upload', 'value' => '填你的', 'remark' => 'WebDAV服务地址', 'sort' => 0, ], [ 'name' => 'webdav_username', 'group' => 'upload', 'value' => '填你的', 'remark' => 'WebDAV账户', 'sort' => 0, ], [ 'name' => 'webdav_password', 'group' => 'upload', 'value' => '填你的', 'remark' => 'WebDAV密码', 'sort' => 0, ], [ 'name' => 'webdav_prefix', 'group' => 'upload', 'value' => '', 'remark' => 'WebDAV存储前缀', 'sort' => 0, ], [ 'name' => 'webdav_domain', 'group' => 'upload', 'value' => '填你的', 'remark' => 'WebDAV访问域名', 'sort' => 0, ], ]; /** * upload_allow_type 待追加的上传方式(追加合并,不覆盖已有值). */ public $allowTypeAppend = ['s3', 'webdav']; public function update() { $this->output->writeln('更新代码'); $this->output->info('v2.5.0:S3 / WebDAV 存储支持存量升级(补齐 12 条 sysconfig + upload_allow_type 追加 s3、webdav)'); // 表前缀从默认连接取(database.default 指向的连接),不能写死 connections.mysql $prefix = config('database.connections.' . config('database.default') . '.prefix', 'ul_'); $configTable = $prefix . 'system_config'; // 确认表存在(防御性检查) $tablesExist = Db::query( 'SELECT TABLE_NAME FROM information_schema.TABLES WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = ?', [$configTable] ); if (empty($tablesExist)) { $this->output->warning("表 {$configTable} 不存在,跳过数据补齐。请先执行 scheme:sync 或 migrate:run 初始化表结构。"); return; } // ==================== 新配置幂等补齐(check-then-insert)==================== $configAdded = 0; $configSkipped = 0; foreach ($this->newConfigs as $cfg) { // 幂等:用 (group, name) 检查存在性 $exists = Db::name('system_config') ->where('group', $cfg['group']) ->where('name', $cfg['name']) ->find(); if ($exists) { $this->output->writeln(" - 配置 {$cfg['group']}.{$cfg['name']} 已存在,跳过"); ++$configSkipped; continue; } Db::name('system_config')->insert($cfg); $this->output->writeln(" - 新增配置 {$cfg['group']}.{$cfg['name']} 完成"); ++$configAdded; } // ==================== upload_allow_type 追加合并(不覆盖用户自定义值)==================== $typesAppended = []; $allowRow = Db::name('system_config') ->where('group', 'upload') ->where('name', 'upload_allow_type') ->find(); if (!$allowRow) { // 不存在则插入完整默认值(与种子 adminInitData/SystemConfig.php 一致) Db::name('system_config')->insert([ 'name' => 'upload_allow_type', 'group' => 'upload', 'value' => 'local_public,alioss,qnoss,txcos,s3,webdav', 'remark' => '可用的上传文件方式', 'sort' => 0, ]); $this->output->writeln(' - 新增配置 upload.upload_allow_type 完成(默认全集)'); ++$configAdded; } else { // 追加合并:过滤空串项后逐个 append,保持原顺序与用户自定义值不动 $types = array_values(array_filter(explode(',', (string) $allowRow['value']), function ($item) { return $item !== ''; })); foreach ($this->allowTypeAppend as $type) { if (!in_array($type, $types, true)) { $types[] = $type; $typesAppended[] = $type; } } if (!empty($typesAppended)) { Db::name('system_config') ->where('id', $allowRow['id']) ->update(['value' => implode(',', $types)]); $this->output->writeln(' - 配置 upload.upload_allow_type 追加 ' . implode(',', $typesAppended) . ' 完成:' . implode(',', $types)); } else { $this->output->writeln(' - 配置 upload.upload_allow_type 已含 ' . implode(',', $this->allowTypeAppend) . ',跳过'); } } // ==================== 清理 sysconfig 缓存 ==================== // 脚本直改 DB 绕过了 sysconfig() 的缓存失效逻辑(tag=sysconfig,TTL 3600s), // 不清理则表单 in: 校验在缓存过期前会拒绝新上传方式 Cache::tag('sysconfig')->clear(); $this->output->writeln(' - 已清理 sysconfig 缓存(tag=sysconfig)'); $summary = "本次新增 {$configAdded} 条配置"; if (!empty($typesAppended)) { $summary .= ',upload_allow_type 追加 ' . implode(',', $typesAppended); } $summary .= ",跳过 {$configSkipped} 条已存在"; $this->output->info($summary); $this->output->writeln('更新代码完成'); } }