From 5634abed82e141bf306ee7084056e2eeacc908b8 Mon Sep 17 00:00:00 2001 From: Bernd Bestel Date: Tue, 17 Nov 2020 20:22:38 +0100 Subject: [PATCH] Use transactions for database migrations --- services/DatabaseMigrationService.php | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/services/DatabaseMigrationService.php b/services/DatabaseMigrationService.php index e19df3a2..8385d8e8 100644 --- a/services/DatabaseMigrationService.php +++ b/services/DatabaseMigrationService.php @@ -49,8 +49,20 @@ class DatabaseMigrationService extends BaseService if (intval($rowCount) === 0) { - $this->getDatabaseService()->ExecuteDbStatement($sql); - $this->getDatabaseService()->ExecuteDbStatement('INSERT INTO migrations (migration) VALUES (' . $migrationId . ')'); + $this->getDatabaseService()->GetDbConnectionRaw()->beginTransaction(); + + try + { + $this->getDatabaseService()->ExecuteDbStatement($sql); + $this->getDatabaseService()->ExecuteDbStatement('INSERT INTO migrations (migration) VALUES (' . $migrationId . ')'); + } + catch (Exception $ex) + { + $this->getDatabaseService()->GetDbConnectionRaw()->rollback(); + throw $ex; + } + + $this->getDatabaseService()->GetDbConnectionRaw()->commit(); } } }