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,
]);
}
}
}