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\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);
}
}
}

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;
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();