setDefaultValueForColumnInTable()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 6
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 8
rs 10
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