調整 TransferSqliteToMysql 改成 指定檔案 20250606

This commit is contained in:
allen.yan 2025-06-06 17:43:24 +08:00
parent 77bb35f95a
commit 2502873d95
4 changed files with 22 additions and 12 deletions

View File

@ -37,7 +37,7 @@ class ExportSqlite extends Command
$this->info("[Export] 開始匯出資料類型: {$type}"); $this->info("[Export] 開始匯出資料類型: {$type}");
try { try {
if (!in_array($type, ['song', 'user', 'all'])) { if (!in_array($type, ['song', 'user', 'FavoriteSongs', 'all'])) {
$this->error('[Export] 無效的 type請使用song、user 或 all'); $this->error('[Export] 無效的 type請使用song、user 或 all');
return 1; return 1;
} }

View File

@ -8,23 +8,33 @@ use Illuminate\Support\Facades\Config;
class TransferSqliteToMysql extends Command 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.'; protected $description = 'Transfer all data from SQLite to MySQL, optionally truncating tables first.';
public function handle(): int public function handle(): int
{ {
// ✅ 自訂 SQLite 連線路徑(請修改為你實際的檔案位置) $path = ltrim($this->argument('path'), '/');
Config::set('database.connections.sqlite', [ $this->info("[Transfer] 開始轉移 SQLite 資料:{$path}");
'driver' => 'sqlite',
'database' => base_path('database/database.sqlite'),
'prefix' => '',
]);
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..."); $this->info("🚀 Starting transfer from SQLite to MySQL...");
// 讀取 SQLite 資料庫的所有資料表 // 讀取 SQLite 資料庫的所有資料表
$sqliteTables = DB::connection('sqlite')->select(" $sqliteTables = DB::connection($connectionName)->select("
SELECT name FROM sqlite_master SELECT name FROM sqlite_master
WHERE type='table' AND name NOT LIKE 'sqlite_%'; WHERE type='table' AND name NOT LIKE 'sqlite_%';
"); ");
@ -49,7 +59,7 @@ class TransferSqliteToMysql extends Command
try { try {
// 用 cursor 來避免一次性佔用過多記憶體 // 用 cursor 來避免一次性佔用過多記憶體
$rows = DB::connection('sqlite')->table($table)->cursor(); $rows = DB::connection($connectionName)->table($table)->cursor();
$buffer = []; $buffer = [];
$count = 0; $count = 0;

BIN
database/User.data Normal file

Binary file not shown.

View File

@ -118,8 +118,8 @@ php artisan migrate
php artisan migrate:rollback php artisan migrate:rollback
php artisan migrate 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 composer require laravel/sanctum
php artisan vendor:publish --provider="Laravel\Sanctum\SanctumServiceProvider" php artisan vendor:publish --provider="Laravel\Sanctum\SanctumServiceProvider"