Completed
Pull Request — master (#243)
by Alejandro
01:35
created

Version20181020060559   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
dl 0
loc 53
rs 10
c 0
b 0
f 0
wmc 8
lcom 0
cbo 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A up() 0 4 1
A createColumns() 0 8 3
A postUp() 0 16 3
A down() 0 4 1
1
<?php
2
declare(strict_types=1);
3
4
namespace ShlinkMigrations;
5
6
use Doctrine\DBAL\DBALException;
7
use Doctrine\DBAL\Schema\Schema;
8
use Doctrine\DBAL\Schema\SchemaException;
9
use Doctrine\DBAL\Schema\Table;
10
use Doctrine\DBAL\Types\Type;
11
use Doctrine\Migrations\AbstractMigration;
12
13
/**
14
 * Auto-generated Migration: Please modify to your needs!
15
 */
16
final class Version20181020060559 extends AbstractMigration
17
{
18
    private const COLUMNS = [
19
        'countryCode' => 'country_code',
20
        'countryName' => 'country_name',
21
        'regionName' => 'region_name',
22
        'cityName' => 'city_name',
23
    ];
24
25
    /**
26
     * @param Schema $schema
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, Type::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