diff --git a/app/Console/Commands/ExportSqlite.php b/app/Console/Commands/ExportSqlite.php index a72cc02..942c8a7 100644 --- a/app/Console/Commands/ExportSqlite.php +++ b/app/Console/Commands/ExportSqlite.php @@ -5,6 +5,7 @@ namespace App\Console\Commands; use Illuminate\Console\Command; use App\Jobs\ExportSqliteUserJob; use App\Jobs\ExportSqliteSongJob; +use App\Jobs\ExportSqliteFavoriteJob; use Illuminate\Support\Facades\Bus; class ExportSqlite extends Command diff --git a/app/Jobs/ExportSqliteFavoriteJob.php b/app/Jobs/ExportSqliteFavoriteJob.php index 6a7072e..5e4c03c 100644 --- a/app/Jobs/ExportSqliteFavoriteJob.php +++ b/app/Jobs/ExportSqliteFavoriteJob.php @@ -32,14 +32,14 @@ class ExportSqliteFavoriteJob implements ShouldQueue if (!file_exists($sqlitePath)) { file_put_contents($sqlitePath, ''); } - - config(['database.connections.tempsqlite' => [ + $connectionName = 'tempsqlite_' . md5($sqlitePath . microtime()); + config(["database.connections.{$connectionName}" => [ 'driver' => 'sqlite', 'database' => $sqlitePath, 'prefix' => '', ]]); - $exporter = new SqliteExportService(); + $exporter = new SqliteExportService($connectionName); $exporter->exportMultiple([ 'FavoriteSongs' => [ 'query' => fn () => DB::table('FavoriteSongs'), @@ -57,6 +57,7 @@ class ExportSqliteFavoriteJob implements ShouldQueue ], ], ]); + DB::purge($connectionName); SendSqliteFileJob::dispatch($sqlitePath); } } diff --git a/app/Jobs/ExportSqliteSongJob.php b/app/Jobs/ExportSqliteSongJob.php index c2a19d6..c9f666b 100644 --- a/app/Jobs/ExportSqliteSongJob.php +++ b/app/Jobs/ExportSqliteSongJob.php @@ -35,15 +35,15 @@ class ExportSqliteSongJob implements ShouldQueue if (!file_exists($sqlitePath)) { file_put_contents($sqlitePath, ''); } - - config(['database.connections.tempsqlite' => [ + $connectionName = 'tempsqlite_' . md5($sqlitePath . microtime()); + config(["database.connections.{$connectionName}" => [ 'driver' => 'sqlite', 'database' => $sqlitePath, 'prefix' => '', ]]); - Schema::connection('tempsqlite')->dropIfExists('song_library_cache'); - Schema::connection('tempsqlite')->create('song_library_cache', function (Blueprint $table) { + Schema::connection($connectionName)->dropIfExists('song_library_cache'); + Schema::connection($connectionName)->create('song_library_cache', function (Blueprint $table) { $table->bigIncrements('song_id')->comment('歌曲編號'); $table->string('song_name')->nullable()->index()->comment('歌曲檔名'); $table->string('song_simplified')->nullable()->index()->comment('歌曲簡體'); @@ -71,7 +71,7 @@ class ExportSqliteSongJob implements ShouldQueue $totalInserted = 0; - Song::with(['artists', 'categories'])->chunk(500, function ($songs) use (&$totalInserted) + Song::with(['artists', 'categories'])->chunk(500, function ($songs) use (&$totalInserted, $connectionName) { $rows = []; @@ -110,13 +110,13 @@ class ExportSqliteSongJob implements ShouldQueue ]; } - collect($rows)->chunk(1000)->each(function ($chunk) use (&$totalInserted) { - DB::connection('tempsqlite')->table('song_library_cache')->insert($chunk->toArray()); + collect($rows)->chunk(1000)->each(function ($chunk) use (&$totalInserted, $connectionName) { + DB::connection($connectionName)->table('song_library_cache')->insert($chunk->toArray()); $totalInserted += $chunk->count(); }); }); - $exporter = new SqliteExportService(); + $exporter = new SqliteExportService($connectionName); $exporter->exportMultiple([ 'artists' => [ 'modelClass' => Artist::class, @@ -144,6 +144,7 @@ class ExportSqliteSongJob implements ShouldQueue ], ], ]); + DB::purge($connectionName); SendSqliteFileJob::dispatch($sqlitePath); } } diff --git a/app/Jobs/ExportSqliteUserJob.php b/app/Jobs/ExportSqliteUserJob.php index bac8e24..ec93cbc 100644 --- a/app/Jobs/ExportSqliteUserJob.php +++ b/app/Jobs/ExportSqliteUserJob.php @@ -35,14 +35,14 @@ class ExportSqliteUserJob implements ShouldQueue if (!file_exists($sqlitePath)) { file_put_contents($sqlitePath, ''); } - - config(['database.connections.tempsqlite' => [ + $connectionName = 'tempsqlite_' . md5($sqlitePath . microtime()); + config(["database.connections.{$connectionName}" => [ 'driver' => 'sqlite', 'database' => $sqlitePath, 'prefix' => '', ]]); - $exporter = new SqliteExportService(); + $exporter = new SqliteExportService($connectionName); $exporter->exportMultiple([ // --- users --- 'users' => [ @@ -199,6 +199,7 @@ class ExportSqliteUserJob implements ShouldQueue ], ], ]); + DB::purge($connectionName); SendSqliteFileJob::dispatch($sqlitePath); } diff --git a/app/Jobs/SendSqliteFileJob.php b/app/Jobs/SendSqliteFileJob.php index 8bf6db1..0fadb24 100644 --- a/app/Jobs/SendSqliteFileJob.php +++ b/app/Jobs/SendSqliteFileJob.php @@ -45,11 +45,20 @@ class SendSqliteFileJob implements ShouldQueue $response = $client->upload('/api/upload-sqlite', ['file' => $path]); if ($response->successful()) { - Log::info("✅ 檔案 {$this->filename} 傳送成功"); + Log::info("✅ $branch->name 檔案 {$path} 傳送成功"); } else { - Log::error("❌ 傳送失敗:HTTP {$response->status()}"); + Log::error("❌ $branch->name 傳送失敗:HTTP {$response->status()}"); Log::error($response->body()); } } + if (file_exists($path)) { + sleep(1); // 小延遲避免未立即釋放 + + if (@unlink($path)) { + Log::info("🧹 Temp SQLite file deleted: {$path}"); + } else { + Log::error("❌ 無法刪除 SQLite 檔案:{$path}"); + } + } } } \ No newline at end of file diff --git a/app/Services/SqliteExportService.php b/app/Services/SqliteExportService.php index f746fed..ea9290f 100644 --- a/app/Services/SqliteExportService.php +++ b/app/Services/SqliteExportService.php @@ -13,9 +13,9 @@ class SqliteExportService { protected string $connection; - public function __construct() + public function __construct(string $connection) { - $this->connection = 'tempsqlite'; + $this->connection = $connection; } /**