調整 TransferSqliteToMysql 改成 指定檔案 20250606
This commit is contained in:
parent
77bb35f95a
commit
2502873d95
@ -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;
|
||||
}
|
||||
|
@ -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;
|
||||
|
BIN
database/User.data
Normal file
BIN
database/User.data
Normal file
Binary file not shown.
4
開發手冊.ini
4
開發手冊.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"
|
||||
|
Loading…
x
Reference in New Issue
Block a user