Version20181020060559   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 18
dl 0
loc 49
rs 10
c 0
b 0
f 0
wmc 8

4 Methods

Rating   Name   Duplication   Size   Complexity  
A up() 0 3 1
A down() 0 2 1
A postUp() 0 15 3
A createColumns() 0 5 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace ShlinkMigrations;
6
7
use Doctrine\DBAL\DBALException;
8
use Doctrine\DBAL\Schema\Schema;
9
use Doctrine\DBAL\Schema\SchemaException;
10
use Doctrine\DBAL\Schema\Table;
11
use Doctrine\DBAL\Types\Types;
12
use Doctrine\Migrations\AbstractMigration;
13
14
/**
15
 * Auto-generated Migration: Please modify to your needs!
16
 */
17
final class Version20181020060559 extends AbstractMigration
18
{
19
    private const COLUMNS = [
20
        'countryCode' => 'country_code',
21
        'countryName' => 'country_name',
22
        'regionName' => 'region_name',
23
        'cityName' => 'city_name',
24
    ];
25
26
    /**
27
     * @throws SchemaException
28
     */
29
    public function up(Schema $schema): void
30
    {
31
        $this->createColumns($schema->getTable('visit_locations'), self::COLUMNS);
32
    }
33
34
    private function createColumns(Table $visitLocations, array $columnNames): void
35
    {
36
        foreach ($columnNames as $name) {
37
            if (! $visitLocations->hasColumn($name)) {
38
                $visitLocations->addColumn($name, Types::STRING, ['notnull' => false]);
39
            }
40
        }
41
    }
42
43
    /**
44
     * @throws SchemaException
45
     * @throws DBALException
46
     */
47
    public function postUp(Schema $schema): void
48
    {
49
        $visitLocations = $schema->getTable('visit_locations');
50
51
        // If the camel case columns do not exist, do nothing
52
        if (! $visitLocations->hasColumn('countryCode')) {
53
            return;
54
        }
55
56
        $qb = $this->connection->createQueryBuilder();
57
        $qb->update('visit_locations');
58
        foreach (self::COLUMNS as $camelCaseName => $snakeCaseName) {
59
            $qb->set($snakeCaseName, $camelCaseName);
60
        }
61
        $qb->execute();
62
    }
63
64
    public function down(Schema $schema): void
65
    {
66
        // No down
67
    }
68
}
69