From 4a90f2ac35f595b22fbc71dc5eeaf7bc8b359fb8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20B=C3=B6hmer?= Date: Fri, 4 Mar 2022 13:03:12 +0100 Subject: [PATCH] Fix migrations with DBAL 3 --- migrations/Version20190902140506.php | 13 +++++++------ src/Migration/AbstractMultiPlatformMigration.php | 9 +++++++-- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/migrations/Version20190902140506.php b/migrations/Version20190902140506.php index 42d6d016..a2ca80d9 100644 --- a/migrations/Version20190902140506.php +++ b/migrations/Version20190902140506.php @@ -41,16 +41,17 @@ final class Version20190902140506 extends AbstractMultiPlatformMigration // this up() migration is auto-generated, please modify it to your needs $this->abortIf('mysql' !== $this->connection->getDatabasePlatform()->getName(), 'Migration can only be executed safely on \'mysql\'.'); - try { - //Check if we can use this migration method: - $version = (int) $this->connection->fetchColumn("SELECT keyValue AS version FROM `internal` WHERE `keyName` = 'dbVersion'"); - $this->abortIf(26 !== $version, 'This database migration can only be used if the database version is 26! Install Part-DB 0.5.6 and update database there!'); - } catch (DBALException $dBALException) { - //when the table was not found, then you can not use this migration + //Check if we can use this migration method: + $version = $this->getOldDBVersion(); + + //If we dont have an old databse, skip this migration + if ($version === 0) { $this->warnIf(true, 'Empty database detected. Skip migration.'); return; } + $this->abortIf(26 !== $version, 'This database migration can only be used if the database version is 26! Install Part-DB 0.5.6 and update database there!'); + //Deactive SQL Modes (especially NO_ZERO_DATE, which prevents updating) $this->addSql("SET sql_mode = ''"); diff --git a/src/Migration/AbstractMultiPlatformMigration.php b/src/Migration/AbstractMultiPlatformMigration.php index f73b79da..37e52bcc 100644 --- a/src/Migration/AbstractMultiPlatformMigration.php +++ b/src/Migration/AbstractMultiPlatformMigration.php @@ -4,6 +4,7 @@ namespace App\Migration; use Doctrine\DBAL\Connection; use Doctrine\DBAL\DBALException; +use Doctrine\DBAL\Exception; use Doctrine\DBAL\Schema\Schema; use Doctrine\Migrations\AbstractMigration; use Psr\Log\LoggerInterface; @@ -68,8 +69,12 @@ abstract class AbstractMultiPlatformMigration extends AbstractMigration } try { - return (int) $this->connection->fetchColumn("SELECT keyValue AS version FROM `internal` WHERE `keyName` = 'dbVersion'"); - } catch (DBALException $dBALException) { + $version = $this->connection->fetchOne("SELECT keyValue AS version FROM `internal` WHERE `keyName` = 'dbVersion'"); + if (is_bool($version)) { + return 0; + } + return (int) $version; + } catch (Exception $dBALException) { //when the table was not found, we can proceed, because we have an empty DB! return 0; }