-
-
Notifications
You must be signed in to change notification settings - Fork 390
/
Copy pathTableMetadataStorage.php
100 lines (79 loc) · 3.1 KB
/
TableMetadataStorage.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
<?php
declare(strict_types=1);
namespace Doctrine\Migrations\Metadata;
use DateTime;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Connections\MasterSlaveConnection;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Schema\AbstractSchemaManager;
use Doctrine\DBAL\Schema\Table;
use Doctrine\Migrations\Version\Direction;
use Doctrine\Migrations\Version\ExecutionResult;
use Doctrine\Migrations\Version\Version;
use const CASE_LOWER;
use function array_change_key_case;
use function intval;
class TableMetadataStorage implements MetadataStorage
{
/** @var Connection */
private $connection;
/** @var AbstractSchemaManager */
private $schemaManager;
/** @var AbstractPlatform */
private $platform;
public function __construct(Connection $connection)
{
$this->connection = $connection;
$this->schemaManager = $connection->getSchemaManager();
$this->platform = $connection->getDatabasePlatform();
}
private function isInitialized() : bool
{
if ($this->connection instanceof MasterSlaveConnection) {
$this->connection->connect('master');
}
return $this->schemaManager->tablesExist(['schema_changelog']);
}
private function initialize() : void
{
$schemaChangelog = new Table('schema_changelog');
$schemaChangelog->addColumn('version', 'string', ['notnull' => true]);
$schemaChangelog->addColumn('executed_on', 'datetime', ['notnull' => false]);
$schemaChangelog->addColumn('execution_time', 'integer', ['notnull' => false]);
$schemaChangelog->setPrimaryKey(['version']);
$this->schemaManager->createTable($schemaChangelog);
}
public function getExecutedMigrations() : ExecutedMigrationsSet
{
if (! $this->isInitialized()) {
$this->initialize();
}
$rows = $this->connection->fetchAll('SELECT * FROM schema_changelog');
$migrations = [];
foreach ($rows as $row) {
$row = array_change_key_case($row, CASE_LOWER);
$version = new Version($row['version']);
$executedOn = DateTime::createFromFormat(
$this->platform->getDateTimeFormatString(),
$row['executed_on']
);
$migration = new ExecutedMigration($version, $executedOn, $row['execution_time']? intval($row['execution_time']) : null);
$migrations[(string) $version] = $migration;
}
return new ExecutedMigrationsSet($migrations);
}
public function complete(ExecutionResult $result) : void
{
if ($result->getDirection() === Direction::DOWN) {
$this->connection->delete('schema_changelog', [
'version' => (string) $result->getVersion(),
]);
} else {
$this->connection->insert('schema_changelog', [
'version' => (string) $result->getVersion(),
'executed_on' => $result->getExecutedOn()->format($this->platform->getDateTimeFormatString()),
'execution_time' => $result->getTime(),
]);
}
}
}