加入 User.data (sqlite) 用seeder 滙入資料 20250603

This commit is contained in:
allen.yan 2025-06-03 13:14:35 +08:00
parent 86d8d8ccd4
commit 26206f937b
4 changed files with 22 additions and 18 deletions

View File

@ -66,7 +66,7 @@ class SqliteUploadController extends Controller
$filename = $request->file('file')->getClientOriginalName(); $filename = $request->file('file')->getClientOriginalName();
$path = $request->file('file')->storeAs('sqlite', $filename, 'local'); $path = $request->file('file')->storeAs('sqlite', $filename, 'local');
TransferSqliteTableJob::dispatch(Storage::disk('local')->path($path)); TransferSqliteTableJob::dispatch(Storage::disk('local')->path($path),deleteFile: true);
return response()->json([ return response()->json([
'message' => '上傳成功,已派送資料處理任務', 'message' => '上傳成功,已派送資料處理任務',
'path' => $path, 'path' => $path,

View File

@ -14,9 +14,11 @@ class TransferSqliteTableJob implements ShouldQueue
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
protected string $sqlitePath; protected string $sqlitePath;
protected bool $deleteFile;
public function __construct(string $sqlitePath){ public function __construct(string $sqlitePath , bool $deleteFile = false){
$this->sqlitePath=$sqlitePath; $this->sqlitePath=$sqlitePath;
$this->deleteFile = $deleteFile;
} }
public function handle(): void public function handle(): void
@ -26,7 +28,8 @@ class TransferSqliteTableJob implements ShouldQueue
return; return;
} }
// ✅ 動態產生唯一 connection 名稱 // ✅ 動態產生唯一 connection 名稱
$connectionName = 'tempsqlite_' . md5($this->sqlitePath . microtime()); $connectionName = 'sqlite_' . md5($this->sqlitePath . microtime());
config(["database.connections.{$connectionName}" => [ config(["database.connections.{$connectionName}" => [
'driver' => 'sqlite', 'driver' => 'sqlite',
'database' => $this->sqlitePath, 'database' => $this->sqlitePath,
@ -42,6 +45,7 @@ class TransferSqliteTableJob implements ShouldQueue
if (empty($sqliteTables)) { if (empty($sqliteTables)) {
logger()->error("❌ No tables found in SQLite database."); logger()->error("❌ No tables found in SQLite database.");
DB::purge($connectionName);
return; return;
} }
@ -58,7 +62,6 @@ class TransferSqliteTableJob implements ShouldQueue
DB::statement("CREATE TABLE IF NOT EXISTS _{$table} LIKE {$table}"); DB::statement("CREATE TABLE IF NOT EXISTS _{$table} LIKE {$table}");
$rows = DB::connection($connectionName)->table($table)->cursor(); $rows = DB::connection($connectionName)->table($table)->cursor();
$buffer = []; $buffer = [];
$count = 0; $count = 0;
@ -67,22 +70,22 @@ class TransferSqliteTableJob implements ShouldQueue
$count++; $count++;
if ($count % 500 === 0) { if ($count % 500 === 0) {
DB::connection($mysqlConnection)->table("_" . $table)->insert($buffer); DB::connection($mysqlConnection)->table("_{$table}")->insert($buffer);
$buffer = []; $buffer = [];
} }
} }
if (!empty($buffer)) { if (!empty($buffer)) {
DB::connection($mysqlConnection)->table("_" . $table)->insert($buffer); DB::connection($mysqlConnection)->table("_{$table}")->insert($buffer);
} }
DB::statement("RENAME TABLE {$table} TO {$table}_"); DB::statement("RENAME TABLE {$table} TO {$table}_");
DB::statement("RENAME TABLE _{$table} TO {$table}"); DB::statement("RENAME TABLE _{$table} TO {$table}");
DB::statement("DROP TABLE IF EXISTS {$table}_"); DB::statement("DROP TABLE IF EXISTS {$table}_");
logger()->info("Done: {$table} ({$count} records)"); logger()->info("Imported: {$table} ({$count} records)");
} catch (\Exception $e) { } catch (\Exception $e) {
logger()->error("❌ Failed to transfer {$table}: " . $e->getMessage()); logger()->error("❌ Failed to import {$table}: " . $e->getMessage());
} finally { } finally {
DB::connection($mysqlConnection)->statement('SET FOREIGN_KEY_CHECKS=1;'); DB::connection($mysqlConnection)->statement('SET FOREIGN_KEY_CHECKS=1;');
} }
@ -90,12 +93,13 @@ class TransferSqliteTableJob implements ShouldQueue
// 🔥 結束後刪檔與釋放 connection // 🔥 結束後刪檔與釋放 connection
DB::purge($connectionName); DB::purge($connectionName);
if (file_exists($this->sqlitePath)) {
if ($this->deleteFile && file_exists($this->sqlitePath)) {
sleep(1); sleep(1);
unlink($this->sqlitePath); unlink($this->sqlitePath);
logger()->info("🧹 Temp SQLite file deleted: {$this->sqlitePath}"); logger()->info("🧹 Deleted SQLite file: {$this->sqlitePath}");
} }
logger()->info("🎉 All tables transferred."); logger()->info("🎉 SQLite import completed: {$this->sqlitePath}");
} }
} }

BIN
database/User.data Normal file

Binary file not shown.

View File

@ -2,9 +2,8 @@
namespace Database\Seeders; namespace Database\Seeders;
use App\Models\User;
// use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder; use Illuminate\Database\Seeder;
use App\Jobs\ImportSqliteJob;
class DatabaseSeeder extends Seeder class DatabaseSeeder extends Seeder
{ {
@ -13,10 +12,11 @@ class DatabaseSeeder extends Seeder
*/ */
public function run(): void public function run(): void
{ {
$this->call([ (new ImportSqliteJob('database/User.data', false))->handle();
PermissionTableSeeder::class, //$this->call([
CreateAdminUserSeeder::class, // PermissionTableSeeder::class,
FavoriteSongsSeeder::class, // CreateAdminUserSeeder::class,
]); // FavoriteSongsSeeder::class,
//]);
} }
} }