'sqlite', 'database' => base_path('database/database.sqlite'), 'prefix' => '', ]); $this->info("🚀 Starting transfer from SQLite to MySQL..."); // 讀取 SQLite 資料庫的所有資料表 $sqliteTables = DB::connection('sqlite')->select(" SELECT name FROM sqlite_master WHERE type='table' AND name NOT LIKE 'sqlite_%'; "); if (empty($sqliteTables)) { $this->error("❌ No tables found in SQLite database."); return 1; } // 取得 .env 中指定的 MySQL 連線名稱 $mysqlConnection = config('database.default'); // 默認會是 'mysql',如果 .env 修改會自動更新 foreach ($sqliteTables as $tableObj) { $table = $tableObj->name; // 忽略 Laravel 內部 migration 表 if ($table === 'migrations') { continue; } $this->info("📦 Transferring table: {$table}"); try { // 從 SQLite 資料庫讀取資料 $records = DB::connection('sqlite')->table($table)->get(); if ($records->isEmpty()) { $this->warn("⚠️ Table {$table} has no data."); continue; } // 資料分批處理,避免一次插入過多資料造成效能問題 $chunks = $records->chunk(500); foreach ($chunks as $chunk) { DB::connection($mysqlConnection)->table($table)->insert( $chunk->map(fn ($row) => (array) $row)->toArray() ); } $this->info("✅ Done: {$table}"); } catch (\Exception $e) { $this->error("❌ Failed to transfer {$table}: " . $e->getMessage()); } } $this->info("🎉 Transfer complete!"); return 0; } }