滙入 Sqlite config 設定異常問題修正
檔案傳輸後刪除資料 20250527
This commit is contained in:
parent
049118ad68
commit
cd099bce76
@ -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
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
@ -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);
|
||||
|
||||
}
|
||||
|
@ -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}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -13,9 +13,9 @@ class SqliteExportService
|
||||
{
|
||||
protected string $connection;
|
||||
|
||||
public function __construct()
|
||||
public function __construct(string $connection)
|
||||
{
|
||||
$this->connection = 'tempsqlite';
|
||||
$this->connection = $connection;
|
||||
}
|
||||
|
||||
/**
|
||||
|
Loading…
x
Reference in New Issue
Block a user