Version20200110182849   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 21
c 1
b 0
f 0
dl 0
loc 38
rs 10
wmc 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
A down() 0 2 1
A up() 0 6 1
A setDefaultValueForColumnInTable() 0 8 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace ShlinkMigrations;
6
7
use Doctrine\DBAL\Schema\Schema;
8
use Doctrine\Migrations\AbstractMigration;
9
10
use function Functional\each;
11
use function Functional\partial_left;
12
13
final class Version20200110182849 extends AbstractMigration
14
{
15
    private const DEFAULT_EMPTY_VALUE = '';
16
    private const COLUMN_DEFAULTS_MAP = [
17
        'visits' => [
18
            'referer',
19
            'user_agent',
20
        ],
21
        'visit_locations' => [
22
            'timezone',
23
            'country_code',
24
            'country_name',
25
            'region_name',
26
            'city_name',
27
        ],
28
    ];
29
30
    public function up(Schema $schema): void
31
    {
32
        each(
33
            self::COLUMN_DEFAULTS_MAP,
34
            fn (array $columns, string $tableName) =>
35
                each($columns, partial_left([$this, 'setDefaultValueForColumnInTable'], $tableName)),
36
        );
37
    }
38
39
    public function setDefaultValueForColumnInTable(string $tableName, string $columnName): void
40
    {
41
        $qb = $this->connection->createQueryBuilder();
42
        $qb->update($tableName)
43
           ->set($columnName, ':emptyValue')
44
           ->setParameter('emptyValue', self::DEFAULT_EMPTY_VALUE)
45
           ->where($qb->expr()->isNull($columnName))
46
           ->execute();
47
    }
48
49
    public function down(Schema $schema): void
50
    {
51
        // No need (and no way) to undo this migration
52
    }
53
}
54