update
This commit is contained in:
@@ -94,37 +94,57 @@ class JsonTransferController extends Controller
|
||||
|
||||
return response()->download($zipFile);
|
||||
}
|
||||
|
||||
public function importTable($table) {
|
||||
set_time_limit(0);
|
||||
ini_set('memory_limit', '-1');
|
||||
ini_set('max_execution_time', 0);
|
||||
|
||||
$path = base_path($this->exportPath . $table . '.json');
|
||||
if (!file_exists($path)) {
|
||||
return back()->with('error', "File JSON untuk tabel $table tidak ditemukan.");
|
||||
return back()->with('error', "File tidak ditemukan.");
|
||||
}
|
||||
|
||||
DB::statement("TRUNCATE TABLE $table RESTART IDENTITY CASCADE");
|
||||
DB::disableQueryLog();
|
||||
try {
|
||||
DB::beginTransaction();
|
||||
DB::statement("TRUNCATE TABLE $table RESTART IDENTITY CASCADE");
|
||||
$items = Items::fromFile($path);
|
||||
|
||||
$chunk = [];
|
||||
$batchSize = 1000;
|
||||
$counter = 0;
|
||||
|
||||
$items = Items::fromFile($path);
|
||||
$chunk = [];
|
||||
foreach ($items as $row) {
|
||||
$data = (array) $row;
|
||||
unset($data['id']);
|
||||
$chunk[] = $data;
|
||||
$counter++;
|
||||
|
||||
foreach ($items as $row) {
|
||||
$data = (array) $row;
|
||||
unset($data['id']);
|
||||
$chunk[] = (array) $row;
|
||||
|
||||
if (count($chunk) >= 50) {
|
||||
|
||||
DB::table($table)->insert($chunk);
|
||||
$chunk = [];
|
||||
if (count($chunk) >= $batchSize) {
|
||||
DB::table($table)->insert($chunk);
|
||||
$chunk = [];
|
||||
if ($counter % 5000 == 0) {
|
||||
gc_collect_cycles();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
unset($row);
|
||||
}
|
||||
if (!empty($chunk)) {
|
||||
DB::table($table)->insert($chunk);
|
||||
}
|
||||
$resetSequenceSql = "SELECT setval(pg_get_serial_sequence('$table', 'id'), COALESCE(MAX(id), 1)) FROM $table";
|
||||
DB::statement($resetSequenceSql);
|
||||
DB::commit();
|
||||
DB::enableQueryLog();
|
||||
|
||||
if (!empty($chunk)) {
|
||||
DB::table($table)->insert($chunk);
|
||||
}
|
||||
return back()->with('success', 'Import berhasil!');
|
||||
|
||||
return back()->with('success', 'Import berhasil!');
|
||||
} catch (\Exception $e) {
|
||||
DB::rollBack();
|
||||
\Log::error("Import Error $table: " . $e->getMessage());
|
||||
return back()->with('error', "Error 500 Details: " . $e->getMessage());
|
||||
}
|
||||
}
|
||||
public function pull(Request $request){
|
||||
if (session('previlage') !== 'developer') {
|
||||
|
||||
Reference in New Issue
Block a user