-
Notifications
You must be signed in to change notification settings - Fork 122
Expand file tree
/
Copy pathEnvironment.php
More file actions
366 lines (307 loc) · 10 KB
/
Copy pathEnvironment.php
File metadata and controls
366 lines (307 loc) · 10 KB
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
<?php
declare(strict_types=1);
/**
* MIT License
* For full license information, please view the LICENSE file that was distributed with this source code.
*/
namespace Migrations\Migration;
use Cake\Console\ConsoleIo;
use Cake\Datasource\ConnectionManager;
use Migrations\Db\Adapter\AdapterFactory;
use Migrations\Db\Adapter\AdapterInterface;
use Migrations\DirectionalMigrationInterface;
use Migrations\MigrationInterface;
use Migrations\ReversibleMigrationInterface;
use Migrations\SeedInterface;
use RuntimeException;
class Environment
{
protected string $name;
/**
* @var array<string, mixed>
*/
protected array $options;
protected ?ConsoleIo $io = null;
protected int $currentVersion;
protected string $schemaTableName = 'phinxlog';
protected AdapterInterface $adapter;
/**
* @param string $name Environment Name
* @param array<string, mixed> $options Options
*/
public function __construct(string $name, array $options)
{
$this->name = $name;
$this->options = $options;
}
/**
* Executes the specified migration on this environment.
*
* @param \Migrations\MigrationInterface $migration Migration
* @param string $direction Direction
* @param bool $fake flag that if true, we just record running the migration, but not actually do the migration
* @return void
*/
public function executeMigration(MigrationInterface $migration, string $direction = MigrationInterface::UP, bool $fake = false): void
{
$direction = $direction === MigrationInterface::UP ? MigrationInterface::UP : MigrationInterface::DOWN;
$migration->setMigratingUp($direction === MigrationInterface::UP);
$startTime = time();
$adapter = $this->getAdapter();
$migration->setAdapter($adapter);
$migration->preFlightCheck();
if (method_exists($migration, MigrationInterface::INIT)) {
$migration->{MigrationInterface::INIT}();
}
$atomic = $migration->useTransactions();
// begin the transaction if the adapter supports it
if ($atomic) {
$adapter->beginTransaction();
}
if (!$fake) {
// Run the migration. Dispatch order: capability interfaces first
// (statically narrowable for IDEs and static analysis), then a
// method_exists fallback for migrations that haven't yet adopted
// either ReversibleMigrationInterface or DirectionalMigrationInterface.
$isReversible = $migration instanceof ReversibleMigrationInterface
|| (!$migration instanceof DirectionalMigrationInterface
&& method_exists($migration, MigrationInterface::CHANGE));
if ($isReversible) {
if ($direction === MigrationInterface::DOWN) {
// Create an instance of the RecordingAdapter so we can record all
// of the migration commands for reverse playback
/** @var \Migrations\Db\Adapter\RecordingAdapter $recordAdapter */
$recordAdapter = AdapterFactory::instance()
->getWrapper('record', $adapter);
// Wrap the adapter with a phinx shim to maintain contain
$migration->setAdapter($recordAdapter);
$migration->{MigrationInterface::CHANGE}();
$recordAdapter->executeInvertedCommands();
$migration->setAdapter($this->getAdapter());
} else {
$migration->{MigrationInterface::CHANGE}();
}
} elseif ($migration instanceof DirectionalMigrationInterface) {
$direction === MigrationInterface::UP ? $migration->up() : $migration->down();
} elseif (method_exists($migration, $direction)) {
$migration->{$direction}();
}
}
// Record it in the database
$adapter->migrated($migration, $direction, date('Y-m-d H:i:s', $startTime), date('Y-m-d H:i:s', time()));
// commit the transaction if the adapter supports it
if ($atomic) {
$adapter->commitTransaction();
}
$migration->postFlightCheck();
}
/**
* Executes the specified seeder on this environment.
*
* @param \Migrations\SeedInterface $seed Seed
* @return void
*/
public function executeSeed(SeedInterface $seed): void
{
$adapter = $this->getAdapter();
$seed->setAdapter($adapter);
if (method_exists($seed, SeedInterface::INIT)) {
$seed->{SeedInterface::INIT}();
}
// begin the transaction if the adapter supports it
$atomic = $adapter->hasTransactions();
if ($atomic) {
$adapter->beginTransaction();
}
// Run the seeder
$seed->{SeedInterface::RUN}();
// Record the seed execution
// For idempotent seeds, remove old record first to update the timestamp
if ($seed->isIdempotent()) {
$adapter->removeSeedFromLog($seed);
}
$executedTime = date('Y-m-d H:i:s');
$adapter->seedExecuted($seed, $executedTime);
// commit the transaction if the adapter supports it
if ($atomic) {
$adapter->commitTransaction();
}
}
/**
* Sets the environment's name.
*
* @param string $name Environment Name
* @return $this
*/
public function setName(string $name)
{
$this->name = $name;
return $this;
}
/**
* Gets the environment name.
*
* @return string
*/
public function getName(): string
{
return $this->name;
}
/**
* Sets the environment's options.
*
* @param array<string, mixed> $options Environment Options
* @return $this
*/
public function setOptions(array $options)
{
$this->options = $options;
return $this;
}
/**
* Gets the environment's options.
*
* @return array<string, mixed>
*/
public function getOptions(): array
{
return $this->options;
}
/**
* Sets the consoleio.
*
* @param \Cake\Console\ConsoleIo $io ConsoleIo
* @return $this
*/
public function setIo(ConsoleIo $io)
{
$this->io = $io;
return $this;
}
/**
* Get the io instance
*
* @return \Cake\Console\ConsoleIo $io The io instance to use
*/
public function getIo(): ?ConsoleIo
{
return $this->io;
}
/**
* Gets all migrated version numbers.
*
* @return array
*/
public function getVersions(): array
{
return $this->getAdapter()->getVersions();
}
/**
* Get all migration log entries, indexed by version creation time and sorted in ascending order by the configuration's
* version_order option
*
* @return array
*/
public function getVersionLog(): array
{
return $this->getAdapter()->getVersionLog();
}
/**
* Sets the current version of the environment.
*
* @param int $version Environment Version
* @return $this
*/
public function setCurrentVersion(int $version)
{
$this->currentVersion = $version;
return $this;
}
/**
* Gets the current version of the environment.
*
* @return int
*/
public function getCurrentVersion(): int
{
// We don't cache this code as the current version is pretty volatile.
// that means they're no point in a setter then?
// maybe we should cache and call a reset() method every time a migration is run
$versions = $this->getVersions();
$version = 0;
if ($versions) {
$version = end($versions);
}
$this->setCurrentVersion($version);
return $this->currentVersion;
}
/**
* Sets the database adapter.
*
* @param \Migrations\Db\Adapter\AdapterInterface $adapter Database Adapter
* @return $this
*/
public function setAdapter(AdapterInterface $adapter)
{
$this->adapter = $adapter;
return $this;
}
/**
* Gets the database adapter.
*
* @throws \RuntimeException
* @return \Migrations\Db\Adapter\AdapterInterface
*/
public function getAdapter(): AdapterInterface
{
if (isset($this->adapter)) {
return $this->adapter;
}
$options = $this->getOptions();
if (!isset($options['connection'])) {
throw new RuntimeException('No connection defined');
}
$connection = ConnectionManager::get($options['connection']);
$options['connection'] = $connection;
// Get the driver classname as those are aligned with adapter names.
$driver = $connection->getDriver();
$driverClass = $driver::class;
$driverName = strtolower(substr($driverClass, (int)strrpos($driverClass, '\\') + 1));
$options['adapter'] = $driverName;
$factory = AdapterFactory::instance();
$adapter = $factory
->getAdapter($driverName, $options);
// Automatically time the executed commands
$adapter = $factory->getWrapper('timed', $adapter);
if (isset($options['wrapper'])) {
$adapter = $factory
->getWrapper($options['wrapper'], $adapter);
}
$io = $this->getIo();
if ($io instanceof ConsoleIo) {
$adapter->setIo($io);
}
$this->setAdapter($adapter);
return $adapter;
}
/**
* Sets the schema table name.
*
* @param string $schemaTableName Schema Table Name
* @return $this
*/
public function setSchemaTableName(string $schemaTableName)
{
$this->schemaTableName = $schemaTableName;
return $this;
}
/**
* Gets the schema table name.
*
* @return string
*/
public function getSchemaTableName(): string
{
return $this->schemaTableName;
}
}