Completed
Pull Request — develop (#599)
by Alejandro
05:40
created

Version20200105165647::preUp()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 7
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 9
rs 10
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\Types\Types;
10
use Doctrine\Migrations\AbstractMigration;
11
12
final class Version20200105165647 extends AbstractMigration
13
{
14
    private const COLUMNS = ['lat' => 'latitude', 'lon' => 'longitude'];
15
16
    public function preUp(Schema $schema): void
17
    {
18
        foreach (self::COLUMNS as $columnName) {
19
            $qb = $this->connection->createQueryBuilder();
20
            $qb->update('visit_locations')
21
               ->set($columnName, '"0"')
22
               ->where($columnName . '=""')
23
               ->orWhere($columnName . ' IS NULL')
24
               ->execute();
25
        }
26
    }
27
28
    /**
29
     * @throws DBALException
30
     */
31
    public function up(Schema $schema): void
32
    {
33
        $visitLocations = $schema->getTable('visit_locations');
34
35
        foreach (self::COLUMNS as $newName => $oldName) {
36
            $visitLocations->addColumn($newName, Types::FLOAT);
37
        }
38
    }
39
40
    public function postUp(Schema $schema): void
41
    {
42
        foreach (self::COLUMNS as $newName => $oldName) {
43
            $qb = $this->connection->createQueryBuilder();
44
            $qb->update('visit_locations')
45
               ->set($newName, $oldName)
46
               ->execute();
47
        }
48
    }
49
50
    public function preDown(Schema $schema): void
51
    {
52
        foreach (self::COLUMNS as $newName => $oldName) {
53
            $qb = $this->connection->createQueryBuilder();
54
            $qb->update('visit_locations')
55
               ->set($oldName, $newName)
56
               ->execute();
57
        }
58
    }
59
60
    /**
61
     * @throws DBALException
62
     */
63
    public function down(Schema $schema): void
64
    {
65
        $visitLocations = $schema->getTable('visit_locations');
66
67
        foreach (self::COLUMNS as $colName => $oldName) {
68
            $visitLocations->dropColumn($colName);
69
        }
70
    }
71
}
72