1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
/** |
3
|
|
|
* This file is part of the daikon-cqrs/dbal project. |
4
|
|
|
* |
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
6
|
|
|
* file that was distributed with this source code. |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Daikon\Dbal\Migration; |
10
|
|
|
|
11
|
|
|
use Assert\Assertion; |
12
|
|
|
use Daikon\Dbal\Connector\ConnectorInterface; |
13
|
|
|
use Daikon\Interop\RuntimeException; |
14
|
|
|
use DateTimeImmutable; |
15
|
|
|
use ReflectionClass; |
16
|
|
|
|
17
|
|
|
trait MigrationTrait |
18
|
|
|
{ |
19
|
|
|
private ?DateTimeImmutable $executedAt; |
20
|
|
|
|
21
|
|
|
private ConnectorInterface $connector; |
22
|
|
|
|
23
|
|
|
public function __construct(DateTimeImmutable $executedAt = null) |
24
|
|
|
{ |
25
|
|
|
$this->executedAt = $executedAt; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
public function execute(ConnectorInterface $connector, string $direction = MigrationInterface::MIGRATE_UP): void |
29
|
|
|
{ |
30
|
|
|
$this->connector = $connector; |
31
|
|
|
|
32
|
|
|
if ($direction === MigrationInterface::MIGRATE_DOWN) { |
33
|
|
|
Assertion::true($this->isReversible(), 'Migration cannot be reversed'); |
|
|
|
|
34
|
|
|
Assertion::true($this->hasExecuted(), 'Migration has not previously been executed'); |
35
|
|
|
$this->down(); |
|
|
|
|
36
|
|
|
$this->executedAt = null; |
37
|
|
|
} else { |
38
|
|
|
Assertion::false($this->hasExecuted(), 'Migration has already been executed'); |
39
|
|
|
$this->up(); |
|
|
|
|
40
|
|
|
$this->executedAt = new DateTimeImmutable; |
41
|
|
|
} |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function getName(): string |
45
|
|
|
{ |
46
|
|
|
$shortName = (new ReflectionClass(static::class))->getShortName(); |
47
|
|
|
if (!preg_match('#^(?<name>.+?)\d+$#', $shortName, $matches)) { |
48
|
|
|
throw new RuntimeException('Unexpected migration name in '.$shortName); |
49
|
|
|
} |
50
|
|
|
return $matches['name']; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function getVersion(): int |
54
|
|
|
{ |
55
|
|
|
$shortName= (new ReflectionClass(static::class))->getShortName(); |
56
|
|
|
if (!preg_match('#(?<version>\d{14})$#', $shortName, $matches)) { |
57
|
|
|
throw new RuntimeException('Unexpected migration version in '.$shortName); |
58
|
|
|
} |
59
|
|
|
return intval($matches['version']); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function hasExecuted(): bool |
63
|
|
|
{ |
64
|
|
|
return $this->executedAt instanceof DateTimeImmutable; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function toNative(): array |
68
|
|
|
{ |
69
|
|
|
$state = [ |
70
|
|
|
'@type' => static::class, |
71
|
|
|
'name' => $this->getName(), |
72
|
|
|
'version' => $this->getVersion(), |
73
|
|
|
'description' => $this->getDescription() |
|
|
|
|
74
|
|
|
]; |
75
|
|
|
|
76
|
|
|
if ($this->hasExecuted()) { |
77
|
|
|
$state['executedAt'] = $this->executedAt->format('c'); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
return $state; |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|