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( |
|
|
|
|
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
|
|
|
|
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.