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