-
-
Notifications
You must be signed in to change notification settings - Fork 392
/
Copy pathAbstractMigration.php
190 lines (157 loc) · 4.85 KB
/
AbstractMigration.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
<?php
declare(strict_types=1);
namespace Doctrine\Migrations;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Exception as DBALException;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Schema\AbstractSchemaManager;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\Exception\AbortMigration;
use Doctrine\Migrations\Exception\FrozenMigration;
use Doctrine\Migrations\Exception\IrreversibleMigration;
use Doctrine\Migrations\Exception\MigrationException;
use Doctrine\Migrations\Exception\SkipMigration;
use Doctrine\Migrations\Query\Query;
use Psr\Log\LoggerInterface;
use function sprintf;
/**
* The AbstractMigration class is for end users to extend from when creating migrations. Extend this class
* and implement the required up() and down() methods.
*/
abstract class AbstractMigration
{
/** @var Connection */
protected $connection;
/** @var AbstractSchemaManager<AbstractPlatform> */
protected $sm;
/** @var AbstractPlatform */
protected $platform;
/** @var Query[] */
private array $plannedSql = [];
/** @var Query[] */
private array $deferredSql = [];
private bool $frozen = false;
public function __construct(Connection $connection, private readonly LoggerInterface $logger)
{
$this->connection = $connection;
$this->sm = $this->connection->createSchemaManager();
$this->platform = $this->connection->getDatabasePlatform();
}
/**
* Indicates the transactional mode of this migration.
*
* If this function returns true (default) the migration will be executed
* in one transaction, otherwise non-transactional state will be used to
* execute each of the migration SQLs.
*
* Extending class should override this function to alter the return value.
*/
public function isTransactional(): bool
{
return true;
}
public function getDescription(): string
{
return '';
}
public function warnIf(bool $condition, string $message = 'Unknown Reason'): void
{
if (! $condition) {
return;
}
$this->logger->warning($message, ['migration' => $this]);
}
/** @throws AbortMigration */
public function abortIf(bool $condition, string $message = 'Unknown Reason'): void
{
if ($condition) {
throw new AbortMigration($message);
}
}
/** @throws SkipMigration */
public function skipIf(bool $condition, string $message = 'Unknown Reason'): void
{
if ($condition) {
throw new SkipMigration($message);
}
}
/** @throws MigrationException|DBALException */
public function preUp(Schema $schema): void
{
}
/** @throws MigrationException|DBALException */
public function postUp(Schema $schema): void
{
}
/** @throws MigrationException|DBALException */
public function preDown(Schema $schema): void
{
}
/** @throws MigrationException|DBALException */
public function postDown(Schema $schema): void
{
}
/** @throws MigrationException|DBALException */
abstract public function up(Schema $schema): void;
/** @throws MigrationException|DBALException */
public function down(Schema $schema): void
{
$this->abortIf(true, sprintf('No down() migration implemented for "%s"', static::class));
}
/**
* @param mixed[] $params
* @param mixed[] $types
*/
protected function addSql(
string $sql,
array $params = [],
array $types = [],
): void {
if ($this->frozen) {
throw FrozenMigration::new();
}
$this->plannedSql[] = new Query($sql, $params, $types);
}
/**
* Adds SQL queries which should be executed after schema changes
*
* @param mixed[] $params
* @param mixed[] $types
*/
protected function addDeferredSql(
string $sql,
array $params = [],
array $types = [],
): void {
if ($this->frozen) {
throw FrozenMigration::new();
}
$this->deferredSql[] = new Query($sql, $params, $types);
}
/** @return Query[] */
public function getSql(): array
{
return $this->plannedSql;
}
/** @return Query[] */
public function getDeferredSql(): array
{
return $this->deferredSql;
}
public function freeze(): void
{
$this->frozen = true;
}
protected function write(string $message): void
{
$this->logger->notice($message, ['migration' => $this]);
}
/** @throws IrreversibleMigration */
protected function throwIrreversibleMigrationException(string|null $message = null): void
{
if ($message === null) {
$message = 'This migration is irreversible and cannot be reverted.';
}
throw new IrreversibleMigration($message);
}
}