From 886f63d3e4ca7e5b91b12eb542e5c278ef1f968f Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Mon, 6 Jan 2020 22:57:10 +0100 Subject: [PATCH] Workarounded doctrine-dbal issue by creating new columns instead of changing column types --- data/migrations/Version20200105165647.php | 31 +++++++++++--- data/migrations/Version20200106215144.php | 41 +++++++++++++++++++ ...inkio.Shlink.Core.Entity.VisitLocation.php | 18 ++++---- 3 files changed, 75 insertions(+), 15 deletions(-) create mode 100644 data/migrations/Version20200106215144.php diff --git a/data/migrations/Version20200105165647.php b/data/migrations/Version20200105165647.php index 838a6f70..24b9f984 100644 --- a/data/migrations/Version20200105165647.php +++ b/data/migrations/Version20200105165647.php @@ -6,13 +6,12 @@ namespace ShlinkMigrations; use Doctrine\DBAL\DBALException; use Doctrine\DBAL\Schema\Schema; -use Doctrine\DBAL\Types\Type; use Doctrine\DBAL\Types\Types; use Doctrine\Migrations\AbstractMigration; final class Version20200105165647 extends AbstractMigration { - private const COLUMNS = ['latitude', 'longitude']; + private const COLUMNS = ['lat' => 'latitude', 'lon' => 'longitude']; public function preUp(Schema $schema): void { @@ -33,8 +32,28 @@ final class Version20200105165647 extends AbstractMigration { $visitLocations = $schema->getTable('visit_locations'); - foreach (self::COLUMNS as $columnName) { - $visitLocations->getColumn($columnName)->setType(Type::getType(Types::FLOAT)); + foreach (self::COLUMNS as $newName => $oldName) { + $visitLocations->addColumn($newName, Types::FLOAT); + } + } + + public function postUp(Schema $schema): void + { + foreach (self::COLUMNS as $newName => $oldName) { + $qb = $this->connection->createQueryBuilder(); + $qb->update('visit_locations') + ->set($newName, $oldName) + ->execute(); + } + } + + public function preDown(Schema $schema): void + { + foreach (self::COLUMNS as $newName => $oldName) { + $qb = $this->connection->createQueryBuilder(); + $qb->update('visit_locations') + ->set($oldName, $newName) + ->execute(); } } @@ -45,8 +64,8 @@ final class Version20200105165647 extends AbstractMigration { $visitLocations = $schema->getTable('visit_locations'); - foreach (self::COLUMNS as $columnName) { - $visitLocations->getColumn($columnName)->setType(Type::getType(Types::STRING)); + foreach (self::COLUMNS as $colName => $oldName) { + $visitLocations->dropColumn($colName); } } } diff --git a/data/migrations/Version20200106215144.php b/data/migrations/Version20200106215144.php new file mode 100644 index 00000000..8969441b --- /dev/null +++ b/data/migrations/Version20200106215144.php @@ -0,0 +1,41 @@ +getTable('visit_locations'); + + foreach (self::COLUMNS as $colName) { + $visitLocations->dropColumn($colName); + } + } + + /** + * @throws DBALException + */ + public function down(Schema $schema): void + { + $visitLocations = $schema->getTable('visit_locations'); + + foreach (self::COLUMNS as $colName) { + $visitLocations->addColumn($colName, Types::STRING, [ + 'notnull' => false, + ]); + } + } +} diff --git a/module/Core/config/entities-mappings/Shlinkio.Shlink.Core.Entity.VisitLocation.php b/module/Core/config/entities-mappings/Shlinkio.Shlink.Core.Entity.VisitLocation.php index decf582a..117c2acc 100644 --- a/module/Core/config/entities-mappings/Shlinkio.Shlink.Core.Entity.VisitLocation.php +++ b/module/Core/config/entities-mappings/Shlinkio.Shlink.Core.Entity.VisitLocation.php @@ -4,7 +4,7 @@ declare(strict_types=1); namespace Shlinkio\Shlink\Core; -use Doctrine\DBAL\Types\Type; +use Doctrine\DBAL\Types\Types; use Doctrine\ORM\Mapping\Builder\ClassMetadataBuilder; use Doctrine\ORM\Mapping\ClassMetadata; // @codingStandardsIgnoreLine @@ -13,7 +13,7 @@ $builder = new ClassMetadataBuilder($metadata); $builder->setTable('visit_locations'); -$builder->createField('id', Type::BIGINT) +$builder->createField('id', Types::BIGINT) ->columnName('id') ->makePrimaryKey() ->generatedValue('IDENTITY') @@ -29,18 +29,18 @@ $columns = [ ]; foreach ($columns as $columnName => $fieldName) { - $builder->createField($fieldName, Type::STRING) + $builder->createField($fieldName, Types::STRING) ->columnName($columnName) ->nullable() ->build(); } -$builder->createField('latitude', Type::FLOAT) - ->columnName('latitude') - ->nullable() +$builder->createField('latitude', Types::FLOAT) + ->columnName('lat') + ->nullable(false) ->build(); -$builder->createField('longitude', Type::FLOAT) - ->columnName('longitude') - ->nullable() +$builder->createField('longitude', Types::FLOAT) + ->columnName('lon') + ->nullable(false) ->build();