|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Doctrine\Migrations\Metadata\Storage; |
|
6
|
|
|
|
|
7
|
|
|
use DateTimeImmutable; |
|
8
|
|
|
use Doctrine\DBAL\Connection; |
|
9
|
|
|
use Doctrine\DBAL\Connections\MasterSlaveConnection; |
|
10
|
|
|
use Doctrine\DBAL\Platforms\AbstractPlatform; |
|
11
|
|
|
use Doctrine\DBAL\Schema\AbstractSchemaManager; |
|
12
|
|
|
use Doctrine\DBAL\Schema\Table; |
|
13
|
|
|
use Doctrine\DBAL\Types\Type; |
|
14
|
|
|
use Doctrine\Migrations\Metadata\ExecutedMigration; |
|
15
|
|
|
use Doctrine\Migrations\Metadata\ExecutedMigrationsSet; |
|
16
|
|
|
use Doctrine\Migrations\Version\Direction; |
|
17
|
|
|
use Doctrine\Migrations\Version\ExecutionResult; |
|
18
|
|
|
use Doctrine\Migrations\Version\Version; |
|
19
|
|
|
use InvalidArgumentException; |
|
20
|
|
|
use const CASE_LOWER; |
|
21
|
|
|
use function array_change_key_case; |
|
22
|
|
|
use function intval; |
|
23
|
|
|
use function sprintf; |
|
24
|
|
|
use function strtolower; |
|
25
|
|
|
|
|
26
|
|
|
final class TableMetadataStorage implements MetadataStorage |
|
27
|
|
|
{ |
|
28
|
|
|
/** @var Connection */ |
|
29
|
|
|
private $connection; |
|
30
|
|
|
|
|
31
|
|
|
/** @var AbstractSchemaManager */ |
|
32
|
|
|
private $schemaManager; |
|
33
|
|
|
|
|
34
|
|
|
/** @var AbstractPlatform */ |
|
35
|
|
|
private $platform; |
|
36
|
|
|
|
|
37
|
|
|
/** @var TableMetadataStorageConfiguration */ |
|
38
|
|
|
private $configuration; |
|
39
|
|
|
|
|
40
|
59 |
|
public function __construct(Connection $connection, ?MetadataStorageConfiguration $configuration = null) |
|
41
|
|
|
{ |
|
42
|
59 |
|
$this->connection = $connection; |
|
43
|
59 |
|
$this->schemaManager = $connection->getSchemaManager(); |
|
44
|
59 |
|
$this->platform = $connection->getDatabasePlatform(); |
|
45
|
|
|
|
|
46
|
59 |
|
if ($configuration !== null && ! ($configuration instanceof TableMetadataStorageConfiguration)) { |
|
47
|
|
|
throw new InvalidArgumentException(sprintf('%s accepts only %s as configuration', self::class, TableMetadataStorageConfiguration::class)); |
|
48
|
|
|
} |
|
49
|
59 |
|
$this->configuration = $configuration ?: new TableMetadataStorageConfiguration(); |
|
|
|
|
|
|
50
|
59 |
|
} |
|
51
|
|
|
|
|
52
|
54 |
|
private function isInitialized() : bool |
|
53
|
|
|
{ |
|
54
|
54 |
|
if ($this->connection instanceof MasterSlaveConnection) { |
|
55
|
|
|
$this->connection->connect('master'); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
54 |
|
return $this->schemaManager->tablesExist([$this->configuration->getTableName()]); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
54 |
|
private function initialize() : void |
|
62
|
|
|
{ |
|
63
|
54 |
|
$schemaChangelog = new Table($this->configuration->getTableName()); |
|
64
|
|
|
|
|
65
|
54 |
|
$schemaChangelog->addColumn($this->configuration->getVersionColumnName(), 'string', ['notnull' => true, 'length' => $this->configuration->getVersionColumnLength()]); |
|
66
|
54 |
|
$schemaChangelog->addColumn($this->configuration->getExecutedAtColumnName(), 'datetime', ['notnull' => false]); |
|
67
|
54 |
|
$schemaChangelog->addColumn($this->configuration->getExecutionTimeColumnName(), 'integer', ['notnull' => false]); |
|
68
|
|
|
|
|
69
|
54 |
|
$schemaChangelog->setPrimaryKey([$this->configuration->getVersionColumnName()]); |
|
70
|
|
|
|
|
71
|
54 |
|
$this->schemaManager->createTable($schemaChangelog); |
|
72
|
54 |
|
} |
|
73
|
|
|
|
|
74
|
51 |
|
public function getExecutedMigrations() : ExecutedMigrationsSet |
|
75
|
|
|
{ |
|
76
|
51 |
|
if (! $this->isInitialized()) { |
|
77
|
22 |
|
$this->initialize(); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
51 |
|
$rows = $this->connection->fetchAll(sprintf('SELECT * FROM %s', $this->configuration->getTableName())); |
|
81
|
|
|
|
|
82
|
51 |
|
$migrations = []; |
|
83
|
51 |
|
foreach ($rows as $row) { |
|
84
|
31 |
|
$row = array_change_key_case($row, CASE_LOWER); |
|
85
|
|
|
|
|
86
|
31 |
|
$version = new Version($row[strtolower($this->configuration->getVersionColumnName())]); |
|
87
|
|
|
|
|
88
|
31 |
|
$executedAt = $row[strtolower($this->configuration->getExecutedAtColumnName())] ?? ''; |
|
89
|
31 |
|
$executedAt = $executedAt !== '' |
|
90
|
3 |
|
? DateTimeImmutable::createFromFormat($this->platform->getDateTimeFormatString(), $executedAt) |
|
91
|
31 |
|
: null; |
|
92
|
|
|
|
|
93
|
31 |
|
$executionTime = isset($row[strtolower($this->configuration->getExecutionTimeColumnName())]) |
|
94
|
3 |
|
? intval($row[strtolower($this->configuration->getExecutionTimeColumnName())]) |
|
95
|
31 |
|
: null; |
|
96
|
|
|
|
|
97
|
31 |
|
$migration = new ExecutedMigration( |
|
98
|
31 |
|
$version, |
|
99
|
31 |
|
$executedAt instanceof DateTimeImmutable ? $executedAt : null, |
|
100
|
31 |
|
$executionTime |
|
101
|
|
|
); |
|
102
|
|
|
|
|
103
|
31 |
|
$migrations[(string) $version] = $migration; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
51 |
|
return new ExecutedMigrationsSet($migrations); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
1 |
|
public function reset() : void |
|
110
|
|
|
{ |
|
111
|
1 |
|
$this->connection->executeUpdate( |
|
112
|
1 |
|
sprintf( |
|
113
|
1 |
|
'DELETE FROM %s WHERE 1 = 1', |
|
114
|
1 |
|
$this->platform->quoteIdentifier($this->configuration->getTableName()) |
|
115
|
|
|
) |
|
116
|
|
|
); |
|
117
|
1 |
|
} |
|
118
|
|
|
|
|
119
|
34 |
|
public function complete(ExecutionResult $result) : void |
|
120
|
|
|
{ |
|
121
|
34 |
|
if (! $this->isInitialized()) { |
|
122
|
32 |
|
$this->initialize(); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
34 |
|
if ($result->getDirection() === Direction::DOWN) { |
|
126
|
5 |
|
$this->connection->delete($this->configuration->getTableName(), [ |
|
127
|
5 |
|
$this->configuration->getVersionColumnName() => (string) $result->getVersion(), |
|
128
|
|
|
]); |
|
129
|
|
|
} else { |
|
130
|
34 |
|
$this->connection->insert($this->configuration->getTableName(), [ |
|
131
|
34 |
|
$this->configuration->getVersionColumnName() => (string) $result->getVersion(), |
|
132
|
34 |
|
$this->configuration->getExecutedAtColumnName() => $result->getExecutedAt(), |
|
133
|
34 |
|
$this->configuration->getExecutionTimeColumnName() => $result->getTime(), |
|
134
|
|
|
], [ |
|
135
|
34 |
|
Type::STRING, |
|
136
|
|
|
Type::DATETIME, |
|
137
|
|
|
Type::INTEGER, |
|
138
|
|
|
]); |
|
139
|
|
|
} |
|
140
|
34 |
|
} |
|
141
|
|
|
} |
|
142
|
|
|
|