2020-01-07 00:31:00 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace ShlinkMigrations;
|
|
|
|
|
2021-03-12 13:52:43 +03:00
|
|
|
use Doctrine\DBAL\Exception;
|
2022-01-10 14:05:01 +03:00
|
|
|
use Doctrine\DBAL\Platforms\MySQLPlatform;
|
|
|
|
use Doctrine\DBAL\Platforms\PostgreSQLPlatform;
|
2020-01-07 00:31:00 +03:00
|
|
|
use Doctrine\DBAL\Schema\Schema;
|
|
|
|
use Doctrine\DBAL\Types\Types;
|
|
|
|
use Doctrine\Migrations\AbstractMigration;
|
|
|
|
|
2020-01-10 18:50:43 +03:00
|
|
|
use function Functional\some;
|
|
|
|
|
2020-01-07 00:31:00 +03:00
|
|
|
final class Version20200105165647 extends AbstractMigration
|
|
|
|
{
|
2020-01-07 00:57:10 +03:00
|
|
|
private const COLUMNS = ['lat' => 'latitude', 'lon' => 'longitude'];
|
2020-01-07 00:31:00 +03:00
|
|
|
|
2020-01-10 18:50:43 +03:00
|
|
|
/**
|
2021-03-12 13:52:43 +03:00
|
|
|
* @throws Exception
|
2020-01-10 18:50:43 +03:00
|
|
|
*/
|
2020-01-07 00:31:00 +03:00
|
|
|
public function preUp(Schema $schema): void
|
|
|
|
{
|
2020-01-10 18:50:43 +03:00
|
|
|
$visitLocations = $schema->getTable('visit_locations');
|
|
|
|
$this->skipIf(some(
|
|
|
|
self::COLUMNS,
|
|
|
|
fn (string $v, string $newColName) => $visitLocations->hasColumn($newColName),
|
|
|
|
), 'New columns already exist');
|
|
|
|
|
2020-01-07 00:31:00 +03:00
|
|
|
foreach (self::COLUMNS as $columnName) {
|
|
|
|
$qb = $this->connection->createQueryBuilder();
|
|
|
|
$qb->update('visit_locations')
|
2020-01-10 18:50:43 +03:00
|
|
|
->set($columnName, ':zeroValue')
|
|
|
|
->where($qb->expr()->orX(
|
|
|
|
$qb->expr()->eq($columnName, ':emptyString'),
|
|
|
|
$qb->expr()->isNull($columnName),
|
|
|
|
))
|
|
|
|
->setParameters([
|
|
|
|
'zeroValue' => '0',
|
|
|
|
'emptyString' => '',
|
|
|
|
])
|
2022-01-10 14:05:01 +03:00
|
|
|
->executeStatement();
|
2020-01-07 00:31:00 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-03-12 13:52:43 +03:00
|
|
|
* @throws Exception
|
2020-01-07 00:31:00 +03:00
|
|
|
*/
|
|
|
|
public function up(Schema $schema): void
|
|
|
|
{
|
|
|
|
$visitLocations = $schema->getTable('visit_locations');
|
|
|
|
|
2020-01-07 00:57:10 +03:00
|
|
|
foreach (self::COLUMNS as $newName => $oldName) {
|
2020-01-10 18:50:43 +03:00
|
|
|
$visitLocations->addColumn($newName, Types::FLOAT, [
|
|
|
|
'default' => '0.0',
|
|
|
|
]);
|
2020-01-07 00:57:10 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-10 21:08:23 +03:00
|
|
|
/**
|
2021-03-12 13:52:43 +03:00
|
|
|
* @throws Exception
|
2020-01-10 21:08:23 +03:00
|
|
|
*/
|
2020-01-07 00:57:10 +03:00
|
|
|
public function postUp(Schema $schema): void
|
|
|
|
{
|
2022-01-10 14:05:01 +03:00
|
|
|
$isPostgres = $this->connection->getDatabasePlatform() instanceof PostgreSQLPlatform;
|
|
|
|
$castType = $isPostgres ? 'DOUBLE PRECISION' : 'DECIMAL(9,2)';
|
2020-01-10 21:08:23 +03:00
|
|
|
|
2020-01-07 00:57:10 +03:00
|
|
|
foreach (self::COLUMNS as $newName => $oldName) {
|
|
|
|
$qb = $this->connection->createQueryBuilder();
|
|
|
|
$qb->update('visit_locations')
|
2020-01-10 21:08:23 +03:00
|
|
|
->set($newName, 'CAST(' . $oldName . ' AS ' . $castType . ')')
|
2022-01-10 14:05:01 +03:00
|
|
|
->executeStatement();
|
2020-01-07 00:57:10 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function preDown(Schema $schema): void
|
|
|
|
{
|
|
|
|
foreach (self::COLUMNS as $newName => $oldName) {
|
|
|
|
$qb = $this->connection->createQueryBuilder();
|
|
|
|
$qb->update('visit_locations')
|
|
|
|
->set($oldName, $newName)
|
2022-01-10 14:05:01 +03:00
|
|
|
->executeStatement();
|
2020-01-07 00:31:00 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-03-12 13:52:43 +03:00
|
|
|
* @throws Exception
|
2020-01-07 00:31:00 +03:00
|
|
|
*/
|
|
|
|
public function down(Schema $schema): void
|
|
|
|
{
|
|
|
|
$visitLocations = $schema->getTable('visit_locations');
|
|
|
|
|
2020-01-07 00:57:10 +03:00
|
|
|
foreach (self::COLUMNS as $colName => $oldName) {
|
|
|
|
$visitLocations->dropColumn($colName);
|
2020-01-07 00:31:00 +03:00
|
|
|
}
|
|
|
|
}
|
2021-10-23 17:02:29 +03:00
|
|
|
|
|
|
|
public function isTransactional(): bool
|
|
|
|
{
|
2022-01-10 14:05:01 +03:00
|
|
|
return ! ($this->connection->getDatabasePlatform() instanceof MySQLPlatform);
|
2021-10-23 17:02:29 +03:00
|
|
|
}
|
2020-01-07 00:31:00 +03:00
|
|
|
}
|