加入 User.data (sqlite) 用seeder 滙入資料 20250603
This commit is contained in:
parent
86d8d8ccd4
commit
26206f937b
@ -66,7 +66,7 @@ class SqliteUploadController extends Controller
|
||||
|
||||
$filename = $request->file('file')->getClientOriginalName();
|
||||
$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([
|
||||
'message' => '上傳成功,已派送資料處理任務',
|
||||
'path' => $path,
|
||||
|
@ -14,9 +14,11 @@ class TransferSqliteTableJob implements ShouldQueue
|
||||
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
||||
|
||||
protected string $sqlitePath;
|
||||
protected bool $deleteFile;
|
||||
|
||||
public function __construct(string $sqlitePath){
|
||||
public function __construct(string $sqlitePath , bool $deleteFile = false){
|
||||
$this->sqlitePath=$sqlitePath;
|
||||
$this->deleteFile = $deleteFile;
|
||||
}
|
||||
|
||||
public function handle(): void
|
||||
@ -26,7 +28,8 @@ class TransferSqliteTableJob implements ShouldQueue
|
||||
return;
|
||||
}
|
||||
// ✅ 動態產生唯一 connection 名稱
|
||||
$connectionName = 'tempsqlite_' . md5($this->sqlitePath . microtime());
|
||||
$connectionName = 'sqlite_' . md5($this->sqlitePath . microtime());
|
||||
|
||||
config(["database.connections.{$connectionName}" => [
|
||||
'driver' => 'sqlite',
|
||||
'database' => $this->sqlitePath,
|
||||
@ -42,6 +45,7 @@ class TransferSqliteTableJob implements ShouldQueue
|
||||
|
||||
if (empty($sqliteTables)) {
|
||||
logger()->error("❌ No tables found in SQLite database.");
|
||||
DB::purge($connectionName);
|
||||
return;
|
||||
}
|
||||
|
||||
@ -58,7 +62,6 @@ class TransferSqliteTableJob implements ShouldQueue
|
||||
DB::statement("CREATE TABLE IF NOT EXISTS _{$table} LIKE {$table}");
|
||||
|
||||
$rows = DB::connection($connectionName)->table($table)->cursor();
|
||||
|
||||
$buffer = [];
|
||||
$count = 0;
|
||||
|
||||
@ -67,22 +70,22 @@ class TransferSqliteTableJob implements ShouldQueue
|
||||
$count++;
|
||||
|
||||
if ($count % 500 === 0) {
|
||||
DB::connection($mysqlConnection)->table("_" . $table)->insert($buffer);
|
||||
DB::connection($mysqlConnection)->table("_{$table}")->insert($buffer);
|
||||
$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("DROP TABLE IF EXISTS {$table}_");
|
||||
|
||||
logger()->info("✅ Done: {$table} ({$count} records)");
|
||||
logger()->info("✅ Imported: {$table} ({$count} records)");
|
||||
} catch (\Exception $e) {
|
||||
logger()->error("❌ Failed to transfer {$table}: " . $e->getMessage());
|
||||
logger()->error("❌ Failed to import {$table}: " . $e->getMessage());
|
||||
} finally {
|
||||
DB::connection($mysqlConnection)->statement('SET FOREIGN_KEY_CHECKS=1;');
|
||||
}
|
||||
@ -90,12 +93,13 @@ class TransferSqliteTableJob implements ShouldQueue
|
||||
|
||||
// 🔥 結束後刪檔與釋放 connection
|
||||
DB::purge($connectionName);
|
||||
if (file_exists($this->sqlitePath)) {
|
||||
|
||||
if ($this->deleteFile && file_exists($this->sqlitePath)) {
|
||||
sleep(1);
|
||||
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
BIN
database/User.data
Normal file
Binary file not shown.
@ -2,9 +2,8 @@
|
||||
|
||||
namespace Database\Seeders;
|
||||
|
||||
use App\Models\User;
|
||||
// use Illuminate\Database\Console\Seeds\WithoutModelEvents;
|
||||
use Illuminate\Database\Seeder;
|
||||
use App\Jobs\ImportSqliteJob;
|
||||
|
||||
class DatabaseSeeder extends Seeder
|
||||
{
|
||||
@ -13,10 +12,11 @@ class DatabaseSeeder extends Seeder
|
||||
*/
|
||||
public function run(): void
|
||||
{
|
||||
$this->call([
|
||||
PermissionTableSeeder::class,
|
||||
CreateAdminUserSeeder::class,
|
||||
FavoriteSongsSeeder::class,
|
||||
]);
|
||||
(new ImportSqliteJob('database/User.data', false))->handle();
|
||||
//$this->call([
|
||||
// PermissionTableSeeder::class,
|
||||
// CreateAdminUserSeeder::class,
|
||||
// FavoriteSongsSeeder::class,
|
||||
//]);
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user