1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Doctrine\Migrations; |
6
|
|
|
|
7
|
|
|
use Doctrine\DBAL\Connection; |
8
|
|
|
use Doctrine\DBAL\DBALException; |
9
|
|
|
use Doctrine\DBAL\Platforms\AbstractPlatform; |
10
|
|
|
use Doctrine\DBAL\Schema\AbstractSchemaManager; |
11
|
|
|
use Doctrine\DBAL\Schema\Schema; |
12
|
|
|
use Doctrine\Migrations\Exception\AbortMigration; |
13
|
|
|
use Doctrine\Migrations\Exception\IrreversibleMigration; |
14
|
|
|
use Doctrine\Migrations\Exception\MigrationException; |
15
|
|
|
use Doctrine\Migrations\Exception\SkipMigration; |
16
|
|
|
use Doctrine\Migrations\Query\Query; |
17
|
|
|
use Psr\Log\LoggerInterface; |
18
|
|
|
use function sprintf; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* The AbstractMigration class is for end users to extend from when creating migrations. Extend this class |
22
|
|
|
* and implement the required up() and down() methods. |
23
|
|
|
*/ |
24
|
|
|
abstract class AbstractMigration |
25
|
|
|
{ |
26
|
|
|
/** @var Connection */ |
27
|
|
|
protected $connection; |
28
|
|
|
|
29
|
|
|
/** @var AbstractSchemaManager */ |
30
|
|
|
protected $sm; |
31
|
|
|
|
32
|
|
|
/** @var AbstractPlatform */ |
33
|
|
|
protected $platform; |
34
|
|
|
|
35
|
|
|
/** @var LoggerInterface */ |
36
|
|
|
private $logger; |
37
|
|
|
|
38
|
|
|
/** @var Query[] */ |
39
|
|
|
private $plannedSql = []; |
40
|
|
|
|
41
|
31 |
|
public function __construct(Connection $connection, LoggerInterface $logger) |
42
|
|
|
{ |
43
|
31 |
|
$this->connection = $connection; |
44
|
31 |
|
$this->sm = $this->connection->getSchemaManager(); |
45
|
31 |
|
$this->platform = $this->connection->getDatabasePlatform(); |
46
|
31 |
|
$this->logger = $logger; |
47
|
31 |
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Indicates the transactional mode of this migration. |
51
|
|
|
* |
52
|
|
|
* If this function returns true (default) the migration will be executed |
53
|
|
|
* in one transaction, otherwise non-transactional state will be used to |
54
|
|
|
* execute each of the migration SQLs. |
55
|
|
|
* |
56
|
|
|
* Extending class should override this function to alter the return value. |
57
|
|
|
*/ |
58
|
15 |
|
public function isTransactional() : bool |
59
|
|
|
{ |
60
|
15 |
|
return true; |
61
|
|
|
} |
62
|
|
|
|
63
|
5 |
|
public function getDescription() : string |
64
|
|
|
{ |
65
|
5 |
|
return ''; |
66
|
|
|
} |
67
|
|
|
|
68
|
3 |
|
public function warnIf(bool $condition, string $message = 'Unknown Reason') : void |
69
|
|
|
{ |
70
|
3 |
|
if (! $condition) { |
71
|
1 |
|
return; |
72
|
|
|
} |
73
|
|
|
|
74
|
2 |
|
$this->logger->warning($message, ['migration' => $this]); |
75
|
2 |
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @throws AbortMigration |
79
|
|
|
*/ |
80
|
11 |
|
public function abortIf(bool $condition, string $message = 'Unknown Reason') : void |
81
|
|
|
{ |
82
|
11 |
|
if ($condition) { |
83
|
4 |
|
throw new AbortMigration($message); |
84
|
|
|
} |
85
|
7 |
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @throws SkipMigration |
89
|
|
|
*/ |
90
|
10 |
|
public function skipIf(bool $condition, string $message = 'Unknown Reason') : void |
91
|
|
|
{ |
92
|
10 |
|
if ($condition) { |
93
|
2 |
|
throw new SkipMigration($message); |
94
|
|
|
} |
95
|
8 |
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @throws MigrationException|DBALException |
99
|
|
|
*/ |
100
|
10 |
|
public function preUp(Schema $schema) : void |
|
|
|
|
101
|
|
|
{ |
102
|
10 |
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @throws MigrationException|DBALException |
106
|
|
|
*/ |
107
|
7 |
|
public function postUp(Schema $schema) : void |
|
|
|
|
108
|
|
|
{ |
109
|
7 |
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @throws MigrationException|DBALException |
113
|
|
|
*/ |
114
|
2 |
|
public function preDown(Schema $schema) : void |
|
|
|
|
115
|
|
|
{ |
116
|
2 |
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @throws MigrationException|DBALException |
120
|
|
|
*/ |
121
|
2 |
|
public function postDown(Schema $schema) : void |
|
|
|
|
122
|
|
|
{ |
123
|
2 |
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @throws MigrationException|DBALException |
127
|
|
|
*/ |
128
|
|
|
abstract public function up(Schema $schema) : void; |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @throws MigrationException|DBALException |
132
|
|
|
*/ |
133
|
1 |
|
public function down(Schema $schema) : void |
|
|
|
|
134
|
|
|
{ |
135
|
1 |
|
$this->abortIf(true, sprintf('No down() migration implemented for "%s"', static::class)); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* @param mixed[] $params |
140
|
|
|
* @param mixed[] $types |
141
|
|
|
*/ |
142
|
11 |
|
protected function addSql( |
143
|
|
|
string $sql, |
144
|
|
|
array $params = [], |
145
|
|
|
array $types = [] |
146
|
|
|
) : void { |
147
|
11 |
|
$this->plannedSql[] = new Query($sql, $params, $types); |
148
|
11 |
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* @return Query[] |
152
|
|
|
*/ |
153
|
12 |
|
public function getSql() : array |
154
|
|
|
{ |
155
|
12 |
|
return $this->plannedSql; |
156
|
|
|
} |
157
|
|
|
|
158
|
1 |
|
protected function write(string $message) : void |
159
|
|
|
{ |
160
|
1 |
|
$this->logger->notice($message, ['migration' => $this]); |
161
|
1 |
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* @throws IrreversibleMigration |
165
|
|
|
*/ |
166
|
2 |
|
protected function throwIrreversibleMigrationException(?string $message = null) : void |
167
|
|
|
{ |
168
|
2 |
|
if ($message === null) { |
169
|
1 |
|
$message = 'This migration is irreversible and cannot be reverted.'; |
170
|
|
|
} |
171
|
|
|
|
172
|
2 |
|
throw new IrreversibleMigration($message); |
173
|
|
|
} |
174
|
|
|
} |
175
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.