From 702ae4794545a8b3e7475b38c58b52a8772d98c2 Mon Sep 17 00:00:00 2001 From: Kacper Donat <kadet1090@gmail.com> Date: Sat, 2 Feb 2019 22:28:07 +0100 Subject: [PATCH] add backup of database in case of update failure --- .../ZtmGdanskDataUpdateSubscriber.php | 8 +++---- .../ZtmGdanskDepartureRepository.php | 8 ++++--- src/Service/DataUpdater.php | 23 +++++++++++++++---- 3 files changed, 28 insertions(+), 11 deletions(-) diff --git a/src/Provider/ZtmGdansk/ZtmGdanskDataUpdateSubscriber.php b/src/Provider/ZtmGdansk/ZtmGdanskDataUpdateSubscriber.php index 5074a05..04f2e45 100644 --- a/src/Provider/ZtmGdansk/ZtmGdanskDataUpdateSubscriber.php +++ b/src/Provider/ZtmGdansk/ZtmGdanskDataUpdateSubscriber.php @@ -209,14 +209,14 @@ class ZtmGdanskDataUpdateSubscriber implements EventSubscriberInterface OperatorEntity::class, $this->ids->generate($provider, $trips->first()['agencyId']) ), - 'track' => $this->em->getReference( + 'track' => $this->em->getReference( TrackEntity::class, $this->ids->generate($provider, $trips->first()['tripId']) ), - 'variant' => $trips->first(function ($trip) { + 'variant' => $trips->first(function ($trip) { return !empty($trip['noteSymbol']); }, ['noteSymbol' => null])['noteSymbol'], - 'note' => $trips->first(function ($trip) { + 'note' => $trips->first(function ($trip) { return !empty($trip['noteSymbol']); }, ['noteDescription' => null])['noteDescription'], ]); @@ -258,4 +258,4 @@ class ZtmGdanskDataUpdateSubscriber implements EventSubscriberInterface DataUpdater::UPDATE_EVENT => 'update', ]; } -} \ No newline at end of file +} diff --git a/src/Provider/ZtmGdansk/ZtmGdanskDepartureRepository.php b/src/Provider/ZtmGdansk/ZtmGdanskDepartureRepository.php index 3eb8985..4f245be 100644 --- a/src/Provider/ZtmGdansk/ZtmGdanskDepartureRepository.php +++ b/src/Provider/ZtmGdansk/ZtmGdanskDepartureRepository.php @@ -37,7 +37,9 @@ class ZtmGdanskDepartureRepository implements DepartureRepository $estimates = json_decode(file_get_contents(static::ESTIMATES_URL . "?stopId=" . $stop->getId()), true)['delay']; $estimates = collect($estimates); - $lines = $estimates->map(function ($delay) { return $delay['routeId']; })->unique(); + $lines = $estimates->map(function ($delay) { + return $delay['routeId']; + })->unique(); $lines = $this->lines->getManyById($lines)->keyBy(t\property('id')); return collect($estimates)->map(function ($delay) use ($stop, $lines) { @@ -52,9 +54,9 @@ class ZtmGdanskDepartureRepository implements DepartureRepository 'vehicle' => $this->reference->get(Vehicle::class, $delay['vehicleCode']), 'line' => $lines->get($delay['routeId']) ?: Line::createFromArray([ 'symbol' => $delay['routeId'], - 'type' => Line::TYPE_UNKNOWN + 'type' => Line::TYPE_UNKNOWN, ]), ]); })->values(); } -} \ No newline at end of file +} diff --git a/src/Service/DataUpdater.php b/src/Service/DataUpdater.php index 75616dd..e75ba63 100644 --- a/src/Service/DataUpdater.php +++ b/src/Service/DataUpdater.php @@ -5,6 +5,7 @@ namespace App\Service; use App\Event\DataUpdateEvent; use Doctrine\DBAL\Schema\Table; use Doctrine\ORM\EntityManagerInterface; +use function Sodium\add; use Symfony\Component\EventDispatcher\EventDispatcherInterface; class DataUpdater @@ -34,10 +35,24 @@ class DataUpdater $connection->getConfiguration()->setSQLLogger(null); $schema = $connection->getSchemaManager(); - collect($schema->listTables())->reject(function (Table $schema) { - return $schema->getName() === 'migration_versions'; - })->each([$schema, 'dropAndCreateTable']); + $path = $connection->getParams()['path']; + $backup = "$path.backup"; - $this->dispatcher->dispatch(self::UPDATE_EVENT, new DataUpdateEvent()); + copy($path, $backup); + + try { + collect($schema->listTables())->reject(function (Table $schema) { + return $schema->getName() === 'migration_versions'; + })->each([$schema, 'dropAndCreateTable']); + + $this->dispatcher->dispatch(self::UPDATE_EVENT, new DataUpdateEvent()); + } catch (\Throwable $exception) { + $connection->close(); + + unlink($path); + rename($backup, $path); + } finally { + unlink($backup); + } } } \ No newline at end of file