/
Configuration.php
294 lines (273 loc) · 11.5 KB
/
Configuration.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
<?php
namespace Concrete\Core\Updater\Migrations;
use Concrete\Core\Database\Connection\Connection;
use Concrete\Core\Support\Facade\Application;
use Doctrine\Migrations\Configuration\Configuration as DoctrineMigrationConfiguration;
use Exception;
class Configuration extends DoctrineMigrationConfiguration
{
/**
* Forced initial migration: inclusive.
*
* @var string
*/
const FORCEDMIGRATION_INCLUSIVE = '>=';
/**
* Forced initial migration: exclusive.
*
* @var string
*/
const FORCEDMIGRATION_EXCLUSIVE = '>';
/**
* Forced initial migration.
*
* @var null|\Doctrine\DBAL\Migrations\Version
*/
protected $forcedInitialMigration = null;
/**
* Construct a migration configuration object.
*
* @param bool $registerMigrations set to true to load the currently available migrations
*/
public function __construct($registerMigrations = true)
{
$app = Application::getFacadeApplication();
$db = $app->make(Connection::class);
parent::__construct($db);
$directory = DIR_BASE_CORE . '/' . DIRNAME_CLASSES . '/Updater/Migrations/Migrations';
$this->setName(t('Concrete Migrations'));
$this->setMigrationsNamespace('Concrete\Core\Updater\Migrations\Migrations');
$this->setMigrationsDirectory($directory);
if ($registerMigrations) {
$this->registerMigrationsFromDirectory($directory);
}
$this->setMigrationsTableName('SystemDatabaseMigrations');
}
/**
* Force the initial migration to be the least recent repeatable one.
*/
public function forceMaxInitialMigration()
{
$forcedInitialMigration = null;
foreach (array_reverse($this->getMigrations()) as $migration) {
/* @var \Doctrine\Migrations\Version $migration */
if ($migration->isMigrated() && !$migration->getMigration() instanceof RepeatableMigrationInterface) {
break;
}
$forcedInitialMigration = $migration;
}
$this->forcedInitialMigration = $forcedInitialMigration;
}
/**
* Force the initial migration, using a specific point.
*
* @param string $reference A Concrete version (eg. '8.3.1') or a migration identifier (eg '20171218000000')
* @param string $criteria One of the FORCEDMIGRATION_... constants
*/
public function forceInitialMigration($reference, $criteria = self::FORCEDMIGRATION_INCLUSIVE)
{
$reference = trim((string) $reference);
if ($reference === '') {
throw new Exception(t('Invalid initial migration reference.'));
}
if (!in_array($criteria, [static::FORCEDMIGRATION_INCLUSIVE, static::FORCEDMIGRATION_EXCLUSIVE], true)) {
throw new Exception(t('Invalid initial migration criteria.'));
}
// Remove possible 'Version' prefix from the migration identifier.
// This allows a user to also use VersionXXXXXXXXXXXX, which corresponds with the class name.
$reference = str_replace('Version', '', $reference);
$migration = $this->findInitialMigration($reference, $criteria);
if ($migration === null) {
throw new Exception(t('Unable to find a migration with identifier %s', $reference));
}
$this->forcedInitialMigration = $migration;
}
/**
* Get the forced initial migration (if set).
*
* @return \Doctrine\DBAL\Migrations\Version|null
*/
public function getForcedInitialMigration()
{
return $this->forcedInitialMigration;
}
/**
* Reset the forced initial migration.
*/
public function resetForceInitialMigration()
{
$this->forcedInitialMigration = null;
}
/**
* This is a stupid requirement, but basically, we grab the lowest version number in our
* system database migrations table, and we loop through all migrations in our file system
* and for any of those LOWER than the lowest one in the table, we can assume they are included
* in this migration. We then manually insert these rows into the SystemDatabaseMigrations table
* so Doctrine isn't stupid and attempt to apply them.
*/
public function registerPreviousMigratedVersions()
{
$app = Application::getFacadeApplication();
$db = $app->make(Connection::class);
try {
$minimum = $db->fetchColumn('select min(version) from SystemDatabaseMigrations');
} catch (Exception $e) {
return;
}
$migrations = $this->getMigrations();
$keys = array_keys($migrations);
if ($keys[0] == $minimum) {
// This is the first migration in Concrete. That means we have already populated this table.
return;
} else {
// We have to populate this table with all the migrations from the very first migration up to
// the $minMigration
foreach ($migrations as $key => $migration) {
if ($key < $minimum) {
$migration->markMigrated();
}
}
}
}
/**
* {@inheritdoc}
*
* @see \Doctrine\Migrations\Configuration\Configuration::getMigrationsToExecute()
*/
public function getMigrationsToExecute(string $direction, string $to): array
{
$result = parent::getMigrationsToExecute($direction, $to);
$forcedInitialMigration = $this->getForcedInitialMigration();
if ($forcedInitialMigration !== null && $direction === 'up') {
$allMigrations = $this->getMigrations();
$allMigrationKeys = array_keys($allMigrations);
$forcedInitialMigrationIndex = array_search($forcedInitialMigration->getVersion(), $allMigrationKeys, false);
if ($forcedInitialMigrationIndex === false) {
// This should never occur
throw new Exception(t('Unable to find the forced migration with version %s', $forcedInitialMigration->getVersion()));
}
$forcedMigrationKeys = array_slice($allMigrationKeys, $forcedInitialMigrationIndex);
$forcedMigrations = [];
foreach (array_reverse($forcedMigrationKeys) as $forcedMigrationKey) {
$migration = $allMigrations[$forcedMigrationKey];
if ($migration->isMigrated() && !$migration->getMigration() instanceof RepeatableMigrationInterface) {
throw new Exception(t('The migration %s has already been executed, and can\'t be executed again.', $migration->getVersion()));
}
$forcedMigrations[$forcedMigrationKey] = $migration;
}
$forcedMigrations = array_reverse($forcedMigrations, true);
$missingMigrations = array_diff_key($result, $forcedMigrations);
if (count($missingMigrations) > 0) {
throw new Exception(t('The forced migration is later than the one to be executed (%s)', reset($missingMigrations)->getVersion()));
}
$result = $forcedMigrations;
}
return $result;
}
/**
* Get the initial migration given a reference (in form YYYYMMDDhhmmss or as a core version).
*
* @param string $reference The migration reference
* @param string $criteria One of the FORCEDMIGRATION_... constants
*
* @throws Exception throws an Exception if $criteria is not valid
*
* @return \Doctrine\DBAL\Migrations\Version|null
*/
protected function findInitialMigration($reference, $criteria)
{
return preg_match('/^' . str_repeat('\d', strlen('YYYYMMDDhhmmss')) . '$/', $reference) ?
$this->findInitialMigrationByIdentifier($reference, $criteria) :
$this->findInitialMigrationByCoreVersion($reference, $criteria);
}
/**
* Get the initial migration starting from its identifier (in form YYYYMMDDhhmmss).
*
* @param string $identifier The migration identifier
* @param string $criteria One of the FORCEDMIGRATION_... constants
*
* @throws Exception throws an Exception if $criteria is not valid
*
* @return \Doctrine\DBAL\Migrations\Version|null
*/
protected function findInitialMigrationByIdentifier($identifier, $criteria)
{
switch ($criteria) {
case static::FORCEDMIGRATION_INCLUSIVE:
if ($this->hasVersion($identifier)) {
$result = $this->getVersion($identifier);
} else {
$result = null;
}
break;
case static::FORCEDMIGRATION_EXCLUSIVE:
$allIdentifiers = array_keys($this->getMigrations());
$migrationIndex = array_search($identifier, $allIdentifiers, false);
if ($migrationIndex === false) {
$result = null;
} elseif ($migrationIndex === count($allIdentifiers) - 1) {
$result = null;
} else {
$result = $this->getVersion($allIdentifiers[$migrationIndex + 1]);
}
break;
default:
throw new Exception(t('Invalid initial migration criteria.'));
}
return $result;
}
/**
* Get the initial migration starting from a core version.
*
* @param string $coreVersion The core version
* @param string $criteria One of the FORCEDMIGRATION_... constants
*
* @throws Exception throws an Exception if $criteria is not valid
*
* @return \Doctrine\DBAL\Migrations\Version|null
*
* @example If looking for version 1.4:
*
* 20010101000000 v1.1
* 20020101000000
* 20030101000000
* 20040101000000 v1.3 <- if $criteria is FORCEDMIGRATION_INCLUSIVE
* 20050101000000 <- if $criteria is FORCEDMIGRATION_EXCLUSIVE
* 20060101000000
* 20070101000000 v1.5
*/
protected function findInitialMigrationByCoreVersion($coreVersion, $criteria)
{
$coreVersionNormalized = preg_replace('/(\.0+)+$/', '', $coreVersion);
if (version_compare($coreVersionNormalized, '5.7') < 0) {
throw new Exception(t('Invalid version specified (%s).', $coreVersion));
}
$migrations = $this->getMigrations();
$migrationIdentifiers = array_keys($migrations);
$maxMigrationIndex = count($migrationIdentifiers) - 1;
$result = null;
foreach ($migrations as $identifier => $migration) {
$migrationCoreVersionNormalized = preg_replace('/(\.0+)+$/', '', $migration->getMigration()->getDescription());
if ($migrationCoreVersionNormalized !== '') {
$cmp = version_compare($migrationCoreVersionNormalized, $coreVersionNormalized);
if ($cmp <= 0 || $result === null) {
switch ($criteria) {
case static::FORCEDMIGRATION_INCLUSIVE:
$result = $migration;
break;
case static::FORCEDMIGRATION_EXCLUSIVE:
$migrationIndex = array_search($identifier, $migrationIdentifiers, false);
$result = $migrationIndex === $maxMigrationIndex ? null : $migrations[$migrationIdentifiers[$migrationIndex + 1]];
break;
default:
throw new Exception(t('Invalid initial migration criteria.'));
}
}
if ($cmp >= 0) {
break;
}
}
}
return $result;
}
}