disable FOREIGN_KEY_CHECKS when restore

This commit is contained in:
Ibnu Maksum 2024-05-14 14:25:34 +07:00
parent ae3db05649
commit fa50539faa
No known key found for this signature in database
GPG key ID: 7FC82848810579E5

View file

@ -732,22 +732,48 @@ switch ($action) {
$suc = 0;
$fal = 0;
$json = json_decode(file_get_contents($_FILES['json']['tmp_name']), true);
try{
ORM::raw_execute("SET FOREIGN_KEY_CHECKS=0;");
} catch (Throwable $e) {
} catch (Exception $e) {
}
try{
ORM::raw_execute("SET GLOBAL FOREIGN_KEY_CHECKS=0;");
} catch (Throwable $e) {
} catch (Exception $e) {
}
foreach ($json as $table => $records) {
ORM::raw_execute("TRUNCATE $table;");
foreach ($records as $rec) {
$t = ORM::for_table($table)->create();
foreach ($rec as $k => $v) {
if ($k != 'id') {
$t->set($k, $v);
try{
$t = ORM::for_table($table)->create();
foreach ($rec as $k => $v) {
if ($k != 'id') {
$t->set($k, $v);
}
}
}
if ($t->save()) {
$suc++;
} else {
if ($t->save()) {
$suc++;
} else {
$fal++;
}
} catch (Throwable $e) {
$fal++;
} catch (Exception $e) {
$fal++;
}
}
}
try{
ORM::raw_execute("SET FOREIGN_KEY_CHECKS=1;");
} catch (Throwable $e) {
} catch (Exception $e) {
}
try{
ORM::raw_execute("SET GLOBAL FOREIGN_KEY_CHECKS=1;");
} catch (Throwable $e) {
} catch (Exception $e) {
}
if (file_exists($_FILES['json']['tmp_name'])) unlink($_FILES['json']['tmp_name']);
r2(U . "settings/dbstatus", 's', "Restored $suc success $fal failed");
} else {