Created database migration which ensures no nulls are present

This commit is contained in:
Alejandro Celaya 2020-01-10 19:44:35 +01:00
parent 49bccf9a06
commit ad1334f289
3 changed files with 58 additions and 5 deletions

View file

@ -0,0 +1,53 @@
<?php
declare(strict_types=1);
namespace ShlinkMigrations;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;
use function Functional\each;
use function Functional\partial_left;
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
{
each(
self::COLUMN_DEFAULTS_MAP,
fn (array $columns, string $tableName) =>
each($columns, partial_left([$this, 'setDefaultValueForColumnInTable'], $tableName)),
);
}
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))
->execute();
}
public function down(Schema $schema): void
{
// No need (and no way) to undo this migration
}
}

View file

@ -29,9 +29,9 @@ class ShortUrl extends AbstractEntity
private Collection $visits;
/** @var Collection|Tag[] */
private Collection $tags;
private ?Chronos $validSince;
private ?Chronos $validUntil;
private ?int $maxVisits;
private ?Chronos $validSince = null;
private ?Chronos $validUntil = null;
private ?int $maxVisits = null;
private ?Domain $domain;
private bool $customSlugWasProvided;

View file

@ -15,10 +15,10 @@ use Shlinkio\Shlink\Core\Visit\Model\VisitLocationInterface;
class Visit extends AbstractEntity implements JsonSerializable
{
private string $referer = '';
private string $referer;
private Chronos $date;
private ?string $remoteAddr = null;
private string $userAgent = '';
private string $userAgent;
private ShortUrl $shortUrl;
private ?VisitLocation $visitLocation = null;