Fixed use of hardcoded quotes on database migration, making it crash on postgres

This commit is contained in:
Alejandro Celaya 2020-01-10 16:50:43 +01:00
parent f9119a38b3
commit 37f0abf86f
3 changed files with 31 additions and 5 deletions

View File

@ -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

View File

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

View File

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