From 2502873d954591906c05dea50ed9ebdab54fa519 Mon Sep 17 00:00:00 2001 From: "allen.yan" Date: Fri, 6 Jun 2025 17:43:24 +0800 Subject: [PATCH] =?UTF-8?q?=E8=AA=BF=E6=95=B4=20TransferSqliteToMysql=20?= =?UTF-8?q?=E6=94=B9=E6=88=90=20=E6=8C=87=E5=AE=9A=E6=AA=94=E6=A1=88=20202?= =?UTF-8?q?50606?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Console/Commands/ExportSqlite.php | 2 +- .../Commands/TransferSqliteToMysql.php | 28 ++++++++++++------ database/User.data | Bin 0 -> 77824 bytes 開發手冊.ini | 4 +-- 4 files changed, 22 insertions(+), 12 deletions(-) create mode 100644 database/User.data 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 0000000000000000000000000000000000000000..55c187fb946cd460b14576ba3c90fd09b05a0641 GIT binary patch literal 77824 zcmeI*O>EoN0S9n?NiuCq&xsSL-40ch1c|4_p}s81Sh2dc^WoZY9NYP58wiw0*{mhX z7Nx{Cx&d;q9fqFT(|Q=XQ%~Dr!wy?p>@ci{4Hz&i*cVXjP+-HbbpwhO7_iG8A0=6q z=~QYmz-|6S!pD#I_}=@GkK~g{X>noJsOq6Yxl&TAp*T0h@jUljD8zBxG5UX){%;Ne zIw3V@XvnusA2fN48#RCG_sw!g#b>y{azOOI<$reYje-C4e?xw=@8iC6z9sM1e4^*9 zmlOXgeLy2&fWYAqs81jD1=mu1y=Iu2zF8`3dNI4ITG@5IQZg*dD4SN+S!!sFiR#w6 zo;^7F@Z|OBiKXe#m8(xpJLuVUXGqmuzW+b~b;gPyE<~BP3?2LCkvNZRQVOI4Oy%I9Z)sR^$7WWn>bXTq9S5=qQ z?rGO6MoFz~h1T?~eOa}(yuZ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!JT$7_nMK>z{}fB*y_009U<00Izz00a)30Q&!j dZI7Tl5P$##AOHafKmY;|fB*y_0D;FZ@IQp}vLgTh literal 0 HcmV?d00001 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"