資料滙出時 DB 連線 回收調整
初建資料留下User 資料 備用 20250528
This commit is contained in:
parent
cd099bce76
commit
144aa499b6
@ -35,16 +35,16 @@ class ExportSqliteBranchJob implements ShouldQueue
|
|||||||
if (!file_exists($sqlitePath)) {
|
if (!file_exists($sqlitePath)) {
|
||||||
file_put_contents($sqlitePath, '');
|
file_put_contents($sqlitePath, '');
|
||||||
}
|
}
|
||||||
|
$connectionName = 'tempsqlite_' . md5($sqlitePath . microtime());
|
||||||
config(['database.connections.tempsqlite' => [
|
config(["database.connections.{$connectionName}" => [
|
||||||
'driver' => 'sqlite',
|
'driver' => 'sqlite',
|
||||||
'database' => $sqlitePath,
|
'database' => $sqlitePath,
|
||||||
'prefix' => '',
|
'prefix' => '',
|
||||||
]]);
|
]]);
|
||||||
|
|
||||||
$exportService = new SqliteExportService();
|
$exporter = new SqliteExportService($connectionName);
|
||||||
|
|
||||||
$exportService->exportMultiple([
|
$exporter->exportMultiple([
|
||||||
'branches' => [
|
'branches' => [
|
||||||
'modelClass' => Branch::class,
|
'modelClass' => Branch::class,
|
||||||
'query' => fn () => Branch::where('id', $this->branchId)->get(),
|
'query' => fn () => Branch::where('id', $this->branchId)->get(),
|
||||||
@ -98,7 +98,6 @@ class ExportSqliteBranchJob implements ShouldQueue
|
|||||||
],
|
],
|
||||||
]
|
]
|
||||||
]);
|
]);
|
||||||
|
|
||||||
SendSqliteFileJob::dispatch($sqlitePath, $this->branchId);
|
SendSqliteFileJob::dispatch($sqlitePath, $this->branchId);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -57,7 +57,6 @@ class ExportSqliteFavoriteJob implements ShouldQueue
|
|||||||
],
|
],
|
||||||
],
|
],
|
||||||
]);
|
]);
|
||||||
DB::purge($connectionName);
|
|
||||||
SendSqliteFileJob::dispatch($sqlitePath);
|
SendSqliteFileJob::dispatch($sqlitePath);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -21,10 +21,19 @@ class ExportSqliteUserJob implements ShouldQueue
|
|||||||
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
||||||
|
|
||||||
public $timeout = 600; // 可依資料量調整 timeout 秒數
|
public $timeout = 600; // 可依資料量調整 timeout 秒數
|
||||||
|
public bool $isSend;
|
||||||
|
public function __construct(bool $isSend = true){
|
||||||
|
$this->isSend =$isSend ;
|
||||||
|
}
|
||||||
|
|
||||||
public function handle()
|
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))) {
|
if (!file_exists(dirname($sqlitePath))) {
|
||||||
@ -199,8 +208,8 @@ class ExportSqliteUserJob implements ShouldQueue
|
|||||||
],
|
],
|
||||||
],
|
],
|
||||||
]);
|
]);
|
||||||
DB::purge($connectionName);
|
if($this->isSend)
|
||||||
SendSqliteFileJob::dispatch($sqlitePath);
|
SendSqliteFileJob::dispatch($sqlitePath);
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -42,6 +42,7 @@ class SqliteExportService
|
|||||||
$rows = $chunk->map($transformer)->toArray();
|
$rows = $chunk->map($transformer)->toArray();
|
||||||
$this->insertData($tableName, $rows);
|
$this->insertData($tableName, $rows);
|
||||||
});
|
});
|
||||||
|
$this->purge();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -83,6 +84,7 @@ class SqliteExportService
|
|||||||
throw new \InvalidArgumentException("Each table config must define either 'modelClass' or 'query'.");
|
throw new \InvalidArgumentException("Each table config must define either 'modelClass' or 'query'.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
$this->purge();
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function dropAndCreateTable(string $table, Closure $schema): void
|
protected function dropAndCreateTable(string $table, Closure $schema): void
|
||||||
@ -96,4 +98,10 @@ class SqliteExportService
|
|||||||
if (empty($rows)) return;
|
if (empty($rows)) return;
|
||||||
DB::connection($this->connection)->table($table)->insert($rows);
|
DB::connection($this->connection)->table($table)->insert($rows);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected function purge(): void
|
||||||
|
{
|
||||||
|
DB::purge($this->connection);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
@ -5,6 +5,7 @@ namespace Database\Seeders;
|
|||||||
use Illuminate\Database\Console\Seeds\WithoutModelEvents;
|
use Illuminate\Database\Console\Seeds\WithoutModelEvents;
|
||||||
use Illuminate\Database\Seeder;
|
use Illuminate\Database\Seeder;
|
||||||
use App\Models\User;
|
use App\Models\User;
|
||||||
|
use App\Jobs\ExportSqliteUserJob;
|
||||||
|
|
||||||
class CreateAdminUserSeeder extends Seeder
|
class CreateAdminUserSeeder extends Seeder
|
||||||
{
|
{
|
||||||
@ -45,5 +46,7 @@ class CreateAdminUserSeeder extends Seeder
|
|||||||
'password' => bcrypt('aa1234')
|
'password' => bcrypt('aa1234')
|
||||||
]);
|
]);
|
||||||
$user->assignRole('User');
|
$user->assignRole('User');
|
||||||
|
|
||||||
|
(new ExportSqliteUserJob(false))->handle();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user