diff --git a/app/Console/Commands/ExportSqlite.php b/app/Console/Commands/ExportSqlite.php index 942c8a7..7d33245 100644 --- a/app/Console/Commands/ExportSqlite.php +++ b/app/Console/Commands/ExportSqlite.php @@ -37,7 +37,7 @@ class ExportSqlite extends Command $this->info("[Export] 開始匯出資料類型: {$type}"); try { - if (!in_array($type, ['song', 'user', 'all'])) { + if (!in_array($type, ['song', 'user', 'FavoriteSongs', 'all'])) { $this->error('[Export] 無效的 type,請使用:song、user 或 all'); return 1; } diff --git a/app/Console/Commands/TransferSqliteToMysql.php b/app/Console/Commands/TransferSqliteToMysql.php index 8e34924..3191b17 100644 --- a/app/Console/Commands/TransferSqliteToMysql.php +++ b/app/Console/Commands/TransferSqliteToMysql.php @@ -8,23 +8,33 @@ use Illuminate\Support\Facades\Config; class TransferSqliteToMysql extends Command { - protected $signature = 'transfer:sqlite-to-mysql'; + protected $signature = 'transfer:sqlite {path : SQLite 相對路徑(例:sqlite/song.sqlite)} '; protected $description = 'Transfer all data from SQLite to MySQL, optionally truncating tables first.'; public function handle(): int { - // ✅ 自訂 SQLite 連線路徑(請修改為你實際的檔案位置) - Config::set('database.connections.sqlite', [ - 'driver' => 'sqlite', - 'database' => base_path('database/database.sqlite'), - 'prefix' => '', - ]); + $path = ltrim($this->argument('path'), '/'); + $this->info("[Transfer] 開始轉移 SQLite 資料:{$path}"); + if (!file_exists($path)) { + $this->error("[Transfer] 找不到 SQLite 檔案:{$path}"); + return 1; + } + + // ✅ 動態產生唯一 connection 名稱 + $connectionName = 'sqlite_' . md5($path . microtime()); + + config(["database.connections.{$connectionName}" => [ + 'driver' => 'sqlite', + 'database' => $path, + 'prefix' => '', + ]]); + $this->info("🚀 Starting transfer from SQLite to MySQL..."); // 讀取 SQLite 資料庫的所有資料表 - $sqliteTables = DB::connection('sqlite')->select(" + $sqliteTables = DB::connection($connectionName)->select(" SELECT name FROM sqlite_master WHERE type='table' AND name NOT LIKE 'sqlite_%'; "); @@ -49,7 +59,7 @@ class TransferSqliteToMysql extends Command try { // 用 cursor 來避免一次性佔用過多記憶體 - $rows = DB::connection('sqlite')->table($table)->cursor(); + $rows = DB::connection($connectionName)->table($table)->cursor(); $buffer = []; $count = 0; diff --git a/database/User.data b/database/User.data new file mode 100644 index 0000000..55c187f Binary files /dev/null and b/database/User.data differ diff --git a/開發手冊.ini b/開發手冊.ini index 0a10598..646d6dd 100644 --- a/開發手冊.ini +++ b/開發手冊.ini @@ -118,8 +118,8 @@ php artisan migrate php artisan migrate:rollback php artisan migrate -php artisan transfer:sqlite-to-mysql - +php artisan transfer:sqlite database/User.data +php artisan transfer:sqlite database/database.sqlite composer require laravel/sanctum php artisan vendor:publish --provider="Laravel\Sanctum\SanctumServiceProvider"