add backup of database in case of update failure

This commit is contained in:
Kacper Donat 2019-02-02 22:28:07 +01:00
parent 5a9096830f
commit 702ae47945
3 changed files with 28 additions and 11 deletions

View File

@ -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',
];
}
}
}

View File

@ -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();
}
}
}

View File

@ -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);
}
}
}