Total Complexity | 3 |
Total Lines | 38 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
1 | <?php |
||
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 |
||
51 | // No need (and no way) to undo this migration |
||
52 | } |
||
53 | } |
||
54 |