Version20200105165647::preUp()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 21
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 16
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 21
rs 9.7333
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
use function Functional\some;
13
14
final class Version20200105165647 extends AbstractMigration
15
{
16
    private const COLUMNS = ['lat' => 'latitude', 'lon' => 'longitude'];
17
18
    /**
19
     * @throws DBALException
20
     */
21
    public function preUp(Schema $schema): void
22
    {
23
        $visitLocations = $schema->getTable('visit_locations');
24
        $this->skipIf(some(
25
            self::COLUMNS,
26
            fn (string $v, string $newColName) => $visitLocations->hasColumn($newColName),
27
        ), 'New columns already exist');
28
29
        foreach (self::COLUMNS as $columnName) {
30
            $qb = $this->connection->createQueryBuilder();
31
            $qb->update('visit_locations')
32
               ->set($columnName, ':zeroValue')
33
               ->where($qb->expr()->orX(
0 ignored issues
show
Deprecated Code introduced by
The function Doctrine\DBAL\Query\Expr...xpressionBuilder::orX() has been deprecated: Use `or()` instead. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

33
               ->where(/** @scrutinizer ignore-deprecated */ $qb->expr()->orX(

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
34
                   $qb->expr()->eq($columnName, ':emptyString'),
35
                   $qb->expr()->isNull($columnName),
36
               ))
37
               ->setParameters([
38
                   'zeroValue' => '0',
39
                   'emptyString' => '',
40
               ])
41
               ->execute();
42
        }
43
    }
44
45
    /**
46
     * @throws DBALException
47
     */
48
    public function up(Schema $schema): void
49
    {
50
        $visitLocations = $schema->getTable('visit_locations');
51
52
        foreach (self::COLUMNS as $newName => $oldName) {
53
            $visitLocations->addColumn($newName, Types::FLOAT, [
54
                'default' => '0.0',
55
            ]);
56
        }
57
    }
58
59
    /**
60
     * @throws DBALException
61
     */
62
    public function postUp(Schema $schema): void
63
    {
64
        $platformName = $this->connection->getDatabasePlatform()->getName();
65
        $castType = $platformName === 'postgres' ? 'DOUBLE PRECISION' : 'DECIMAL(9,2)';
66
67
        foreach (self::COLUMNS as $newName => $oldName) {
68
            $qb = $this->connection->createQueryBuilder();
69
            $qb->update('visit_locations')
70
               ->set($newName, 'CAST(' . $oldName . ' AS ' . $castType . ')')
71
               ->execute();
72
        }
73
    }
74
75
    public function preDown(Schema $schema): void
76
    {
77
        foreach (self::COLUMNS as $newName => $oldName) {
78
            $qb = $this->connection->createQueryBuilder();
79
            $qb->update('visit_locations')
80
               ->set($oldName, $newName)
81
               ->execute();
82
        }
83
    }
84
85
    /**
86
     * @throws DBALException
87
     */
88
    public function down(Schema $schema): void
89
    {
90
        $visitLocations = $schema->getTable('visit_locations');
91
92
        foreach (self::COLUMNS as $colName => $oldName) {
93
            $visitLocations->dropColumn($colName);
94
        }
95
    }
96
}
97