Completed
Pull Request — develop (#599)
by Alejandro
04:29
created

Version20200105165647::up()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
c 2
b 0
f 0
nc 2
nop 1
dl 0
loc 6
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\Type;
10
use Doctrine\DBAL\Types\Types;
11
use Doctrine\Migrations\AbstractMigration;
12
13
final class Version20200105165647 extends AbstractMigration
14
{
15
    private const COLUMNS = ['latitude', 'longitude'];
16
17
    public function preUp(Schema $schema): void
18
    {
19
        foreach (self::COLUMNS as $columnName) {
20
            $qb = $this->connection->createQueryBuilder();
21
            $qb->update('visit_locations')
22
               ->set($columnName, '"0"')
23
               ->where($columnName . '=""')
24
               ->orWhere($columnName . ' IS NULL')
25
               ->execute();
26
        }
27
    }
28
29
    /**
30
     * @throws DBALException
31
     */
32
    public function up(Schema $schema): void
33
    {
34
        $visitLocations = $schema->getTable('visit_locations');
35
36
        foreach (self::COLUMNS as $columnName) {
37
            $visitLocations->getColumn($columnName)->setType(Type::getType(Types::FLOAT));
38
        }
39
    }
40
41
    /**
42
     * @throws DBALException
43
     */
44
    public function down(Schema $schema): void
45
    {
46
        $visitLocations = $schema->getTable('visit_locations');
47
48
        foreach (self::COLUMNS as $columnName) {
49
            $visitLocations->getColumn($columnName)->setType(Type::getType(Types::STRING));
50
        }
51
    }
52
}
53