From 144aa499b6c100dcdadf6399069304746e4d8917 Mon Sep 17 00:00:00 2001 From: "allen.yan" Date: Wed, 28 May 2025 09:15:52 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B3=87=E6=96=99=E6=BB=99=E5=87=BA=E6=99=82?= =?UTF-8?q?=20DB=20=E9=80=A3=E7=B7=9A=20=E5=9B=9E=E6=94=B6=E8=AA=BF?= =?UTF-8?q?=E6=95=B4=20=E5=88=9D=E5=BB=BA=E8=B3=87=E6=96=99=E7=95=99?= =?UTF-8?q?=E4=B8=8BUser=20=E8=B3=87=E6=96=99=20=E5=82=99=E7=94=A8=2020250?= =?UTF-8?q?528?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Jobs/ExportSqliteBranchJob.php | 9 ++++----- app/Jobs/ExportSqliteFavoriteJob.php | 1 - app/Jobs/ExportSqliteUserJob.php | 15 ++++++++++++--- app/Services/SqliteExportService.php | 8 ++++++++ database/seeders/CreateAdminUserSeeder.php | 3 +++ 5 files changed, 27 insertions(+), 9 deletions(-) diff --git a/app/Jobs/ExportSqliteBranchJob.php b/app/Jobs/ExportSqliteBranchJob.php index 00d2bd5..b9b5fd7 100644 --- a/app/Jobs/ExportSqliteBranchJob.php +++ b/app/Jobs/ExportSqliteBranchJob.php @@ -35,16 +35,16 @@ class ExportSqliteBranchJob 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' => '', ]]); - $exportService = new SqliteExportService(); + $exporter = new SqliteExportService($connectionName); - $exportService->exportMultiple([ + $exporter->exportMultiple([ 'branches' => [ 'modelClass' => Branch::class, 'query' => fn () => Branch::where('id', $this->branchId)->get(), @@ -98,7 +98,6 @@ class ExportSqliteBranchJob implements ShouldQueue ], ] ]); - SendSqliteFileJob::dispatch($sqlitePath, $this->branchId); } } diff --git a/app/Jobs/ExportSqliteFavoriteJob.php b/app/Jobs/ExportSqliteFavoriteJob.php index 5e4c03c..b53573d 100644 --- a/app/Jobs/ExportSqliteFavoriteJob.php +++ b/app/Jobs/ExportSqliteFavoriteJob.php @@ -57,7 +57,6 @@ class ExportSqliteFavoriteJob implements ShouldQueue ], ], ]); - DB::purge($connectionName); SendSqliteFileJob::dispatch($sqlitePath); } } diff --git a/app/Jobs/ExportSqliteUserJob.php b/app/Jobs/ExportSqliteUserJob.php index ec93cbc..32899de 100644 --- a/app/Jobs/ExportSqliteUserJob.php +++ b/app/Jobs/ExportSqliteUserJob.php @@ -21,10 +21,19 @@ class ExportSqliteUserJob implements ShouldQueue use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; public $timeout = 600; // 可依資料量調整 timeout 秒數 + public bool $isSend; + public function __construct(bool $isSend = true){ + $this->isSend =$isSend ; + } public function handle() { - $sqlitePath = storage_path('app/database/tempUser.sqlite'); + if($this->isSend){ + $sqlitePath = storage_path('app/database/tempUser.sqlite'); + }else{ + $sqlitePath = 'database/tempUser.sqlite'; + } + // 確保資料夾存在 if (!file_exists(dirname($sqlitePath))) { @@ -199,8 +208,8 @@ class ExportSqliteUserJob implements ShouldQueue ], ], ]); - DB::purge($connectionName); - SendSqliteFileJob::dispatch($sqlitePath); + if($this->isSend) + SendSqliteFileJob::dispatch($sqlitePath); } } diff --git a/app/Services/SqliteExportService.php b/app/Services/SqliteExportService.php index ea9290f..9bbb032 100644 --- a/app/Services/SqliteExportService.php +++ b/app/Services/SqliteExportService.php @@ -42,6 +42,7 @@ class SqliteExportService $rows = $chunk->map($transformer)->toArray(); $this->insertData($tableName, $rows); }); + $this->purge(); } /** @@ -83,6 +84,7 @@ class SqliteExportService throw new \InvalidArgumentException("Each table config must define either 'modelClass' or 'query'."); } } + $this->purge(); } protected function dropAndCreateTable(string $table, Closure $schema): void @@ -96,4 +98,10 @@ class SqliteExportService if (empty($rows)) return; DB::connection($this->connection)->table($table)->insert($rows); } + + protected function purge(): void + { + DB::purge($this->connection); + } + } \ No newline at end of file diff --git a/database/seeders/CreateAdminUserSeeder.php b/database/seeders/CreateAdminUserSeeder.php index f406bf3..987e2a3 100644 --- a/database/seeders/CreateAdminUserSeeder.php +++ b/database/seeders/CreateAdminUserSeeder.php @@ -5,6 +5,7 @@ namespace Database\Seeders; use Illuminate\Database\Console\Seeds\WithoutModelEvents; use Illuminate\Database\Seeder; use App\Models\User; +use App\Jobs\ExportSqliteUserJob; class CreateAdminUserSeeder extends Seeder { @@ -45,5 +46,7 @@ class CreateAdminUserSeeder extends Seeder 'password' => bcrypt('aa1234') ]); $user->assignRole('User'); + + (new ExportSqliteUserJob(false))->handle(); } }