|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Doctrine\Migrations; |
|
6
|
|
|
|
|
7
|
|
|
use DateTime; |
|
8
|
|
|
use Doctrine\DBAL\Connection; |
|
9
|
|
|
use Doctrine\Migrations\Configuration\Configuration; |
|
10
|
|
|
use Doctrine\Migrations\Exception\MigrationNotConvertibleToSql; |
|
11
|
|
|
use function assert; |
|
12
|
|
|
use function count; |
|
13
|
|
|
use function in_array; |
|
14
|
|
|
use function str_replace; |
|
15
|
|
|
|
|
16
|
|
|
class Version implements VersionInterface |
|
17
|
|
|
{ |
|
18
|
|
|
public const STATE_NONE = 0; |
|
19
|
|
|
public const STATE_PRE = 1; |
|
20
|
|
|
public const STATE_EXEC = 2; |
|
21
|
|
|
public const STATE_POST = 3; |
|
22
|
|
|
|
|
23
|
|
|
public const DIRECTION_UP = 'up'; |
|
24
|
|
|
public const DIRECTION_DOWN = 'down'; |
|
25
|
|
|
|
|
26
|
|
|
private const STATES = [ |
|
27
|
|
|
self::STATE_NONE, |
|
28
|
|
|
self::STATE_PRE, |
|
29
|
|
|
self::STATE_EXEC, |
|
30
|
|
|
self::STATE_POST, |
|
31
|
|
|
]; |
|
32
|
|
|
|
|
33
|
|
|
/** @var Configuration */ |
|
34
|
|
|
private $configuration; |
|
35
|
|
|
|
|
36
|
|
|
/** @var OutputWriter */ |
|
37
|
|
|
private $outputWriter; |
|
38
|
|
|
|
|
39
|
|
|
/** @var string */ |
|
40
|
|
|
private $version; |
|
41
|
|
|
|
|
42
|
|
|
/** @var AbstractMigration */ |
|
43
|
|
|
private $migration; |
|
44
|
|
|
|
|
45
|
|
|
/** @var Connection */ |
|
46
|
|
|
private $connection; |
|
47
|
|
|
|
|
48
|
|
|
/** @var string */ |
|
49
|
|
|
private $class; |
|
50
|
|
|
|
|
51
|
|
|
/** @var int */ |
|
52
|
|
|
private $state = self::STATE_NONE; |
|
53
|
|
|
|
|
54
|
|
|
/** @var VersionExecutorInterface */ |
|
55
|
|
|
private $versionExecutor; |
|
56
|
|
|
|
|
57
|
124 |
|
public function __construct( |
|
58
|
|
|
Configuration $configuration, |
|
59
|
|
|
string $version, |
|
60
|
|
|
string $class, |
|
61
|
|
|
VersionExecutorInterface $versionExecutor |
|
62
|
|
|
) { |
|
63
|
124 |
|
$this->configuration = $configuration; |
|
64
|
124 |
|
$this->outputWriter = $configuration->getOutputWriter(); |
|
65
|
124 |
|
$this->class = $class; |
|
66
|
124 |
|
$this->connection = $configuration->getConnection(); |
|
67
|
124 |
|
$this->migration = new $class($this); |
|
68
|
124 |
|
$this->version = $version; |
|
69
|
124 |
|
$this->versionExecutor = $versionExecutor; |
|
70
|
124 |
|
} |
|
71
|
|
|
|
|
72
|
53 |
|
public function __toString() : string |
|
73
|
|
|
{ |
|
74
|
53 |
|
return $this->version; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
79 |
|
public function getVersion() : string |
|
78
|
|
|
{ |
|
79
|
79 |
|
return $this->version; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
1 |
|
public function getDateTime() : string |
|
83
|
|
|
{ |
|
84
|
1 |
|
$datetime = str_replace('Version', '', $this->version); |
|
85
|
1 |
|
$datetime = DateTime::createFromFormat('YmdHis', $datetime); |
|
86
|
|
|
|
|
87
|
1 |
|
if ($datetime === false) { |
|
88
|
1 |
|
return ''; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
1 |
|
return $datetime->format('Y-m-d H:i:s'); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
124 |
|
public function getConfiguration() : Configuration |
|
95
|
|
|
{ |
|
96
|
124 |
|
return $this->configuration; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
14 |
|
public function getMigration() : AbstractMigration |
|
100
|
|
|
{ |
|
101
|
14 |
|
return $this->migration; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
15 |
|
public function isMigrated() : bool |
|
105
|
|
|
{ |
|
106
|
15 |
|
return $this->configuration->hasVersionMigrated($this); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
53 |
|
public function setState(int $state) : void |
|
110
|
|
|
{ |
|
111
|
53 |
|
assert(in_array($state, self::STATES, true)); |
|
112
|
|
|
|
|
113
|
53 |
|
$this->state = $state; |
|
114
|
53 |
|
} |
|
115
|
|
|
|
|
116
|
10 |
|
public function getExecutionState() : string |
|
117
|
|
|
{ |
|
118
|
10 |
|
switch ($this->state) { |
|
119
|
10 |
|
case self::STATE_PRE: |
|
120
|
1 |
|
return 'Pre-Checks'; |
|
121
|
|
|
|
|
122
|
9 |
|
case self::STATE_POST: |
|
123
|
1 |
|
return 'Post-Checks'; |
|
124
|
|
|
|
|
125
|
8 |
|
case self::STATE_EXEC: |
|
126
|
2 |
|
return 'Execution'; |
|
127
|
|
|
|
|
128
|
|
|
default: |
|
129
|
6 |
|
return 'No State'; |
|
130
|
|
|
} |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* @param mixed[] $params |
|
135
|
|
|
* @param mixed[] $types |
|
136
|
|
|
*/ |
|
137
|
32 |
|
public function addSql(string $sql, array $params = [], array $types = []) : void |
|
138
|
|
|
{ |
|
139
|
32 |
|
$this->versionExecutor->addSql($sql, $params, $types); |
|
140
|
32 |
|
} |
|
141
|
|
|
|
|
142
|
9 |
|
public function writeSqlFile( |
|
143
|
|
|
string $path, |
|
144
|
|
|
string $direction = self::DIRECTION_UP |
|
145
|
|
|
) : bool { |
|
146
|
9 |
|
$versionExecutionResult = $this->execute($direction, true); |
|
147
|
|
|
|
|
148
|
9 |
|
if (count($versionExecutionResult->getParams()) !== 0) { |
|
149
|
1 |
|
throw MigrationNotConvertibleToSql::new($this->class); |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
8 |
|
$this->outputWriter->write("\n-- Version " . $this->version . "\n"); |
|
153
|
|
|
|
|
154
|
8 |
|
$sqlQueries = [$this->version => $versionExecutionResult->getSql()]; |
|
155
|
|
|
|
|
156
|
|
|
/* |
|
157
|
|
|
* Since the configuration object changes during the creation we cannot inject things |
|
158
|
|
|
* properly, so I had to violate LoD here (so please, let's find a way to solve it on v2). |
|
159
|
|
|
*/ |
|
160
|
8 |
|
return $this->configuration |
|
161
|
8 |
|
->getQueryWriter() |
|
162
|
8 |
|
->write($path, $direction, $sqlQueries); |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
51 |
|
public function execute( |
|
166
|
|
|
string $direction, |
|
167
|
|
|
bool $dryRun = false, |
|
168
|
|
|
bool $timeAllQueries = false |
|
169
|
|
|
) : VersionExecutionResult { |
|
170
|
51 |
|
return $this->versionExecutor->execute( |
|
171
|
51 |
|
$this, |
|
172
|
51 |
|
$this->migration, |
|
173
|
51 |
|
$direction, |
|
174
|
51 |
|
$dryRun, |
|
175
|
51 |
|
$timeAllQueries |
|
176
|
|
|
); |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
11 |
|
public function markMigrated() : void |
|
180
|
|
|
{ |
|
181
|
11 |
|
$this->markVersion(self::DIRECTION_UP); |
|
182
|
11 |
|
} |
|
183
|
|
|
|
|
184
|
3 |
|
public function markNotMigrated() : void |
|
185
|
|
|
{ |
|
186
|
3 |
|
$this->markVersion(self::DIRECTION_DOWN); |
|
187
|
3 |
|
} |
|
188
|
|
|
|
|
189
|
45 |
|
public function markVersion(string $direction) : void |
|
190
|
|
|
{ |
|
191
|
45 |
|
$this->configuration->createMigrationTable(); |
|
192
|
|
|
|
|
193
|
45 |
|
$migrationsColumnName = $this->configuration |
|
194
|
45 |
|
->getQuotedMigrationsColumnName(); |
|
195
|
|
|
|
|
196
|
45 |
|
if ($direction === self::DIRECTION_UP) { |
|
197
|
44 |
|
$this->connection->insert( |
|
198
|
44 |
|
$this->configuration->getMigrationsTableName(), |
|
199
|
|
|
[ |
|
200
|
44 |
|
$migrationsColumnName => $this->version, |
|
201
|
|
|
] |
|
202
|
|
|
); |
|
203
|
|
|
} else { |
|
204
|
10 |
|
$this->connection->delete( |
|
205
|
10 |
|
$this->configuration->getMigrationsTableName(), |
|
206
|
|
|
[ |
|
207
|
10 |
|
$migrationsColumnName => $this->version, |
|
208
|
|
|
] |
|
209
|
|
|
); |
|
210
|
|
|
} |
|
211
|
45 |
|
} |
|
212
|
|
|
} |
|
213
|
|
|
|