2020-01-10 12:44:35 -06:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace ShlinkMigrations;
|
|
|
|
|
2022-01-10 05:05:01 -06:00
|
|
|
use Doctrine\DBAL\Exception;
|
|
|
|
use Doctrine\DBAL\Platforms\MySQLPlatform;
|
2020-01-10 12:44:35 -06:00
|
|
|
use Doctrine\DBAL\Schema\Schema;
|
|
|
|
use Doctrine\Migrations\AbstractMigration;
|
|
|
|
|
|
|
|
final class Version20200110182849 extends AbstractMigration
|
|
|
|
{
|
|
|
|
private const DEFAULT_EMPTY_VALUE = '';
|
|
|
|
private const COLUMN_DEFAULTS_MAP = [
|
|
|
|
'visits' => [
|
|
|
|
'referer',
|
|
|
|
'user_agent',
|
|
|
|
],
|
|
|
|
'visit_locations' => [
|
|
|
|
'timezone',
|
|
|
|
'country_code',
|
|
|
|
'country_name',
|
|
|
|
'region_name',
|
|
|
|
'city_name',
|
|
|
|
],
|
|
|
|
];
|
|
|
|
|
|
|
|
public function up(Schema $schema): void
|
|
|
|
{
|
2023-11-30 07:34:21 -06:00
|
|
|
foreach (self::COLUMN_DEFAULTS_MAP as $tableName => $columns) {
|
|
|
|
foreach ($columns as $columnName) {
|
|
|
|
$this->setDefaultValueForColumnInTable($tableName, $columnName);
|
|
|
|
}
|
|
|
|
}
|
2020-01-10 12:44:35 -06:00
|
|
|
}
|
|
|
|
|
2022-01-10 05:05:01 -06:00
|
|
|
/**
|
|
|
|
* @throws Exception
|
|
|
|
*/
|
2020-01-10 12:44:35 -06:00
|
|
|
public function setDefaultValueForColumnInTable(string $tableName, string $columnName): void
|
|
|
|
{
|
|
|
|
$qb = $this->connection->createQueryBuilder();
|
|
|
|
$qb->update($tableName)
|
|
|
|
->set($columnName, ':emptyValue')
|
|
|
|
->setParameter('emptyValue', self::DEFAULT_EMPTY_VALUE)
|
|
|
|
->where($qb->expr()->isNull($columnName))
|
2022-01-10 05:05:01 -06:00
|
|
|
->executeStatement();
|
2020-01-10 12:44:35 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
public function down(Schema $schema): void
|
|
|
|
{
|
|
|
|
// No need (and no way) to undo this migration
|
|
|
|
}
|
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-10 12:44:35 -06:00
|
|
|
}
|