From f9119a38b38c054b6933656de48c8cfebe138967 Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Fri, 10 Jan 2020 16:04:35 +0100 Subject: [PATCH 1/3] Updated changelog --- CHANGELOG.md | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 90f4acd1..b2e64538 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,29 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com), and this project adheres to [Semantic Versioning](https://semver.org). +## [Unreleased] + +#### Added + +* *Nothing* + +#### Changed + +* *Nothing* + +#### Deprecated + +* *Nothing* + +#### Removed + +* *Nothing* + +#### Fixed + +* [#607](https://github.com/shlinkio/shlink/issues/607) Added missing info on UPGRADE.md doc. + + ## 2.0.0 - 2020-01-08 #### Added From 37f0abf86f1a4298f248af70bf02e95d65590ba6 Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Fri, 10 Jan 2020 16:50:43 +0100 Subject: [PATCH 2/3] Fixed use of hardcoded quotes on database migration, making it crash on postgres --- CHANGELOG.md | 1 + data/migrations/Version20200105165647.php | 29 +++++++++++++++++++---- data/migrations/Version20200106215144.php | 6 +++++ 3 files changed, 31 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b2e64538..7d2459b1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -25,6 +25,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com), and this #### Fixed * [#607](https://github.com/shlinkio/shlink/issues/607) Added missing info on UPGRADE.md doc. +* [#610](https://github.com/shlinkio/shlink/issues/610) Fixed use of hardcoded quotes on a database migration which makes it fail on postgres. ## 2.0.0 - 2020-01-08 diff --git a/data/migrations/Version20200105165647.php b/data/migrations/Version20200105165647.php index 24b9f984..9fa6a918 100644 --- a/data/migrations/Version20200105165647.php +++ b/data/migrations/Version20200105165647.php @@ -9,18 +9,35 @@ use Doctrine\DBAL\Schema\Schema; use Doctrine\DBAL\Types\Types; use Doctrine\Migrations\AbstractMigration; +use function Functional\some; + final class Version20200105165647 extends AbstractMigration { private const COLUMNS = ['lat' => 'latitude', 'lon' => 'longitude']; + /** + * @throws DBALException + */ public function preUp(Schema $schema): void { + $visitLocations = $schema->getTable('visit_locations'); + $this->skipIf(some( + self::COLUMNS, + fn (string $v, string $newColName) => $visitLocations->hasColumn($newColName), + ), 'New columns already exist'); + foreach (self::COLUMNS as $columnName) { $qb = $this->connection->createQueryBuilder(); $qb->update('visit_locations') - ->set($columnName, '"0"') - ->where($columnName . '=""') - ->orWhere($columnName . ' IS NULL') + ->set($columnName, ':zeroValue') + ->where($qb->expr()->orX( + $qb->expr()->eq($columnName, ':emptyString'), + $qb->expr()->isNull($columnName), + )) + ->setParameters([ + 'zeroValue' => '0', + 'emptyString' => '', + ]) ->execute(); } } @@ -33,7 +50,9 @@ final class Version20200105165647 extends AbstractMigration $visitLocations = $schema->getTable('visit_locations'); foreach (self::COLUMNS as $newName => $oldName) { - $visitLocations->addColumn($newName, Types::FLOAT); + $visitLocations->addColumn($newName, Types::FLOAT, [ + 'default' => '0.0', + ]); } } @@ -42,7 +61,7 @@ final class Version20200105165647 extends AbstractMigration foreach (self::COLUMNS as $newName => $oldName) { $qb = $this->connection->createQueryBuilder(); $qb->update('visit_locations') - ->set($newName, $oldName) + ->set($newName, 'CAST(' . $oldName . ' AS DOUBLE PRECISION)') ->execute(); } } diff --git a/data/migrations/Version20200106215144.php b/data/migrations/Version20200106215144.php index 8969441b..e7a2c31a 100644 --- a/data/migrations/Version20200106215144.php +++ b/data/migrations/Version20200106215144.php @@ -9,6 +9,8 @@ use Doctrine\DBAL\Schema\Schema; use Doctrine\DBAL\Types\Types; use Doctrine\Migrations\AbstractMigration; +use function Functional\none; + final class Version20200106215144 extends AbstractMigration { private const COLUMNS = ['latitude', 'longitude']; @@ -19,6 +21,10 @@ final class Version20200106215144 extends AbstractMigration public function up(Schema $schema): void { $visitLocations = $schema->getTable('visit_locations'); + $this->skipIf(none( + self::COLUMNS, + fn (string $oldColName) => $visitLocations->hasColumn($oldColName), + ), 'Old columns do not exist'); foreach (self::COLUMNS as $colName) { $visitLocations->dropColumn($colName); From 96bb0321eb69e0c78c1bd6fbdfeff8b2fed04b9a Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Fri, 10 Jan 2020 19:08:23 +0100 Subject: [PATCH 3/3] Updated casting type so that it is dynamic --- data/migrations/Version20200105165647.php | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/data/migrations/Version20200105165647.php b/data/migrations/Version20200105165647.php index 9fa6a918..6367f440 100644 --- a/data/migrations/Version20200105165647.php +++ b/data/migrations/Version20200105165647.php @@ -56,12 +56,18 @@ final class Version20200105165647 extends AbstractMigration } } + /** + * @throws DBALException + */ public function postUp(Schema $schema): void { + $platformName = $this->connection->getDatabasePlatform()->getName(); + $castType = $platformName === 'postgres' ? 'DOUBLE PRECISION' : 'DECIMAL(9,2)'; + foreach (self::COLUMNS as $newName => $oldName) { $qb = $this->connection->createQueryBuilder(); $qb->update('visit_locations') - ->set($newName, 'CAST(' . $oldName . ' AS DOUBLE PRECISION)') + ->set($newName, 'CAST(' . $oldName . ' AS ' . $castType . ')') ->execute(); } }