Workarounded doctrine-dbal issue by creating new columns instead of changing column types

This commit is contained in:
Alejandro Celaya 2020-01-06 22:57:10 +01:00
parent 7748dd7cef
commit 886f63d3e4
3 changed files with 75 additions and 15 deletions

View File

@ -6,13 +6,12 @@ namespace ShlinkMigrations;
use Doctrine\DBAL\DBALException; use Doctrine\DBAL\DBALException;
use Doctrine\DBAL\Schema\Schema; use Doctrine\DBAL\Schema\Schema;
use Doctrine\DBAL\Types\Type;
use Doctrine\DBAL\Types\Types; use Doctrine\DBAL\Types\Types;
use Doctrine\Migrations\AbstractMigration; use Doctrine\Migrations\AbstractMigration;
final class Version20200105165647 extends AbstractMigration final class Version20200105165647 extends AbstractMigration
{ {
private const COLUMNS = ['latitude', 'longitude']; private const COLUMNS = ['lat' => 'latitude', 'lon' => 'longitude'];
public function preUp(Schema $schema): void public function preUp(Schema $schema): void
{ {
@ -33,8 +32,28 @@ final class Version20200105165647 extends AbstractMigration
{ {
$visitLocations = $schema->getTable('visit_locations'); $visitLocations = $schema->getTable('visit_locations');
foreach (self::COLUMNS as $columnName) { foreach (self::COLUMNS as $newName => $oldName) {
$visitLocations->getColumn($columnName)->setType(Type::getType(Types::FLOAT)); $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'); $visitLocations = $schema->getTable('visit_locations');
foreach (self::COLUMNS as $columnName) { foreach (self::COLUMNS as $colName => $oldName) {
$visitLocations->getColumn($columnName)->setType(Type::getType(Types::STRING)); $visitLocations->dropColumn($colName);
} }
} }
} }

View File

@ -0,0 +1,41 @@
<?php
declare(strict_types=1);
namespace ShlinkMigrations;
use Doctrine\DBAL\DBALException;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\DBAL\Types\Types;
use Doctrine\Migrations\AbstractMigration;
final class Version20200106215144 extends AbstractMigration
{
private const COLUMNS = ['latitude', 'longitude'];
/**
* @throws DBALException
*/
public function up(Schema $schema): void
{
$visitLocations = $schema->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,
]);
}
}
}

View File

@ -4,7 +4,7 @@ declare(strict_types=1);
namespace Shlinkio\Shlink\Core; namespace Shlinkio\Shlink\Core;
use Doctrine\DBAL\Types\Type; use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping\Builder\ClassMetadataBuilder; use Doctrine\ORM\Mapping\Builder\ClassMetadataBuilder;
use Doctrine\ORM\Mapping\ClassMetadata; // @codingStandardsIgnoreLine use Doctrine\ORM\Mapping\ClassMetadata; // @codingStandardsIgnoreLine
@ -13,7 +13,7 @@ $builder = new ClassMetadataBuilder($metadata);
$builder->setTable('visit_locations'); $builder->setTable('visit_locations');
$builder->createField('id', Type::BIGINT) $builder->createField('id', Types::BIGINT)
->columnName('id') ->columnName('id')
->makePrimaryKey() ->makePrimaryKey()
->generatedValue('IDENTITY') ->generatedValue('IDENTITY')
@ -29,18 +29,18 @@ $columns = [
]; ];
foreach ($columns as $columnName => $fieldName) { foreach ($columns as $columnName => $fieldName) {
$builder->createField($fieldName, Type::STRING) $builder->createField($fieldName, Types::STRING)
->columnName($columnName) ->columnName($columnName)
->nullable() ->nullable()
->build(); ->build();
} }
$builder->createField('latitude', Type::FLOAT) $builder->createField('latitude', Types::FLOAT)
->columnName('latitude') ->columnName('lat')
->nullable() ->nullable(false)
->build(); ->build();
$builder->createField('longitude', Type::FLOAT) $builder->createField('longitude', Types::FLOAT)
->columnName('longitude') ->columnName('lon')
->nullable() ->nullable(false)
->build(); ->build();