info('开始生成配置表数据库备份文件...'); // 确保输出目录存在 $outputDir = dirname($this->outFile); if (!File::exists($outputDir)) { File::makeDirectory($outputDir, 0755, true); $this->info("创建输出目录: {$outputDir}"); } $sqlContent = ''; $sqlContent .= "-- ******************************************************************\n"; $sqlContent .= "-- 配置表数据库备份文件\n"; $sqlContent .= "-- 生成时间: " . now()->toDateTimeString() . "\n"; $sqlContent .= "-- 警告: 此文件由系统自动生成,禁止修改!\n"; $sqlContent .= "-- ******************************************************************\n\n"; $totalTables = 0; $totalRecords = 0; // 遍历每个模型 foreach ($this->modelList as $modelClass) { try { $this->info("处理模型: {$modelClass}"); // 实例化模型 $model = new $modelClass(); $connection = $model->getConnection(); $tablePrefix = $connection->getTablePrefix(); $tableName = $tablePrefix . $model->getTable(); $this->info(" 表名: {$tableName}"); // 获取表的创建语句 $createTableResult = $connection->select("SHOW CREATE TABLE `{$tableName}`"); if (empty($createTableResult)) { $this->error(" 无法获取表 {$tableName} 的创建语句"); continue; } $createTableSQL = $createTableResult[0]->{'Create Table'}; // 移除当前自增值 $createTableSQL = preg_replace('/\s+AUTO_INCREMENT=\d+/', '', $createTableSQL); // 添加表的创建语句 $sqlContent .= "-- ==========================================\n"; $sqlContent .= "-- 表: {$tableName}\n"; $sqlContent .= "-- 模型: {$modelClass}\n"; $sqlContent .= "-- ==========================================\n\n"; $sqlContent .= "DROP TABLE IF EXISTS `{$tableName}`;\n"; $sqlContent .= "{$createTableSQL};\n\n"; // 获取表数据 $records = $connection->table($model->getTable())->get(); $recordCount = $records->count(); $this->info(" 记录数: {$recordCount}"); if ($recordCount > 0) { // 生成INSERT语句 $sqlContent .= "-- 数据插入\n"; // 获取字段名 $firstRecord = (array)$records->first(); $columns = array_keys($firstRecord); $columnList = '`' . implode('`, `', $columns) . '`'; $sqlContent .= "INSERT INTO `{$tableName}` ({$columnList}) VALUES\n"; $values = []; foreach ($records as $record) { $recordArray = (array)$record; $escapedValues = array_map(function ($value) { if ($value === null) { return 'NULL'; } elseif (is_numeric($value)) { return $value; } else { return "'" . addslashes($value) . "'"; } }, $recordArray); $values[] = '(' . implode(', ', $escapedValues) . ')'; } $sqlContent .= implode(",\n", $values) . ";\n\n"; } else { $sqlContent .= "-- 该表无数据记录\n\n"; } $totalTables++; $totalRecords += $recordCount; } catch (\Exception $e) { $this->error("处理模型 {$modelClass} 时出错: " . $e->getMessage()); continue; } } // 添加统计信息 $sqlContent .= "-- ==========================================\n"; $sqlContent .= "-- 备份统计\n"; $sqlContent .= "-- 总表数: {$totalTables}\n"; $sqlContent .= "-- 总记录数: {$totalRecords}\n"; $sqlContent .= "-- 生成完成时间: " . now()->toDateTimeString() . "\n"; $sqlContent .= "-- ==========================================\n"; // 写入文件 File::put($this->outFile, $sqlContent); $this->info("配置表数据库备份文件生成完成!"); $this->info("输出文件: {$this->outFile}"); $this->info("处理表数: {$totalTables}"); $this->info("总记录数: {$totalRecords}"); $this->info("文件大小: " . $this->formatBytes(File::size($this->outFile))); return self::SUCCESS; } /** * 格式化字节大小 * * @param int $bytes * @return string */ private function formatBytes(int $bytes): string { $units = [ 'B', 'KB', 'MB', 'GB' ]; $bytes = max($bytes, 0); $pow = floor(($bytes ? log($bytes) : 0) / log(1024)); $pow = min($pow, count($units) - 1); $bytes /= 1 << 10 * $pow; return round($bytes, 2) . ' ' . $units[$pow]; } }