2018-10-20 10:00:29 +03:00
|
|
|
<?php
|
2019-10-05 18:26:10 +03:00
|
|
|
|
2018-10-20 10:00:29 +03:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace ShlinkMigrations;
|
|
|
|
|
2021-03-12 13:52:43 +03:00
|
|
|
use Doctrine\DBAL\Exception;
|
2018-10-20 10:00:29 +03:00
|
|
|
use Doctrine\DBAL\Schema\Schema;
|
|
|
|
use Doctrine\DBAL\Schema\SchemaException;
|
|
|
|
use Doctrine\DBAL\Schema\Table;
|
2020-01-07 01:08:14 +03:00
|
|
|
use Doctrine\DBAL\Types\Types;
|
2018-10-20 10:00:29 +03:00
|
|
|
use Doctrine\Migrations\AbstractMigration;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Auto-generated Migration: Please modify to your needs!
|
|
|
|
*/
|
|
|
|
final class Version20181020060559 extends AbstractMigration
|
|
|
|
{
|
|
|
|
private const COLUMNS = [
|
|
|
|
'countryCode' => 'country_code',
|
|
|
|
'countryName' => 'country_name',
|
|
|
|
'regionName' => 'region_name',
|
|
|
|
'cityName' => 'city_name',
|
|
|
|
];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @throws SchemaException
|
|
|
|
*/
|
|
|
|
public function up(Schema $schema): void
|
|
|
|
{
|
|
|
|
$this->createColumns($schema->getTable('visit_locations'), self::COLUMNS);
|
|
|
|
}
|
|
|
|
|
|
|
|
private function createColumns(Table $visitLocations, array $columnNames): void
|
|
|
|
{
|
|
|
|
foreach ($columnNames as $name) {
|
|
|
|
if (! $visitLocations->hasColumn($name)) {
|
2020-01-07 01:08:14 +03:00
|
|
|
$visitLocations->addColumn($name, Types::STRING, ['notnull' => false]);
|
2018-10-20 10:00:29 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @throws SchemaException
|
2021-03-12 13:52:43 +03:00
|
|
|
* @throws Exception
|
2018-10-20 10:00:29 +03:00
|
|
|
*/
|
|
|
|
public function postUp(Schema $schema): void
|
|
|
|
{
|
|
|
|
$visitLocations = $schema->getTable('visit_locations');
|
|
|
|
|
|
|
|
// If the camel case columns do not exist, do nothing
|
|
|
|
if (! $visitLocations->hasColumn('countryCode')) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$qb = $this->connection->createQueryBuilder();
|
|
|
|
$qb->update('visit_locations');
|
|
|
|
foreach (self::COLUMNS as $camelCaseName => $snakeCaseName) {
|
|
|
|
$qb->set($snakeCaseName, $camelCaseName);
|
|
|
|
}
|
|
|
|
$qb->execute();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function down(Schema $schema): void
|
|
|
|
{
|
|
|
|
// No down
|
|
|
|
}
|
2021-03-12 13:52:43 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @fixme Workaround for https://github.com/doctrine/migrations/issues/1104
|
|
|
|
*/
|
|
|
|
public function isTransactional(): bool
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2018-10-20 10:00:29 +03:00
|
|
|
}
|