Import Crater verbessert
This commit is contained in:
@@ -4,6 +4,8 @@ declare(strict_types=1);
|
||||
ini_set('display_errors', '1');
|
||||
error_reporting(E_ALL);
|
||||
session_start();
|
||||
@set_time_limit(0);
|
||||
ignore_user_abort(true);
|
||||
|
||||
$loggedIn = isset($_SESSION['user_id']);
|
||||
$pageTitle = 'Krater-Import - AstroTools';
|
||||
@@ -374,6 +376,7 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
||||
[
|
||||
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
|
||||
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
|
||||
PDO::ATTR_EMULATE_PREPARES => false,
|
||||
]
|
||||
);
|
||||
|
||||
@@ -491,6 +494,10 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
||||
$stmt = $pdo->prepare($insertSql);
|
||||
$imported = 0;
|
||||
$skipped = 0;
|
||||
$rowsSinceCommit = 0;
|
||||
$batchSize = 250;
|
||||
|
||||
$pdo->beginTransaction();
|
||||
|
||||
while (($row = fgetcsv($handle, 0, ';', '"', '\\')) !== false) {
|
||||
if ($row === [null] || $row === false) {
|
||||
@@ -586,6 +593,17 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
||||
|
||||
$stmt->execute($payload);
|
||||
$imported++;
|
||||
$rowsSinceCommit++;
|
||||
|
||||
if ($rowsSinceCommit >= $batchSize) {
|
||||
$pdo->commit();
|
||||
$pdo->beginTransaction();
|
||||
$rowsSinceCommit = 0;
|
||||
}
|
||||
}
|
||||
|
||||
if ($pdo->inTransaction()) {
|
||||
$pdo->commit();
|
||||
}
|
||||
|
||||
fclose($handle);
|
||||
@@ -599,6 +617,14 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
||||
'truncate_first' => $truncateFirst,
|
||||
];
|
||||
} catch (Throwable $exception) {
|
||||
if (isset($pdo) && $pdo instanceof PDO && $pdo->inTransaction()) {
|
||||
$pdo->rollBack();
|
||||
}
|
||||
|
||||
if (isset($handle) && is_resource($handle)) {
|
||||
fclose($handle);
|
||||
}
|
||||
|
||||
$report = [
|
||||
'ok' => false,
|
||||
'error' => $exception->getMessage(),
|
||||
|
||||
Reference in New Issue
Block a user