|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Doctrine\Migrations; |
|
6
|
|
|
|
|
7
|
|
|
use Doctrine\DBAL\Connection; |
|
8
|
|
|
use Doctrine\DBAL\Platforms\AbstractPlatform; |
|
9
|
|
|
use Doctrine\DBAL\Schema\AbstractSchemaManager; |
|
10
|
|
|
use Doctrine\DBAL\Schema\Schema; |
|
11
|
|
|
use function sprintf; |
|
12
|
|
|
|
|
13
|
|
|
abstract class AbstractMigration |
|
14
|
|
|
{ |
|
15
|
|
|
/** @var Version */ |
|
16
|
|
|
protected $version; |
|
17
|
|
|
|
|
18
|
|
|
/** @var Connection */ |
|
19
|
|
|
protected $connection; |
|
20
|
|
|
|
|
21
|
|
|
/** @var AbstractSchemaManager */ |
|
22
|
|
|
protected $sm; |
|
23
|
|
|
|
|
24
|
|
|
/** @var AbstractPlatform */ |
|
25
|
|
|
protected $platform; |
|
26
|
|
|
|
|
27
|
|
|
/** @var OutputWriter */ |
|
28
|
|
|
private $outputWriter; |
|
29
|
|
|
|
|
30
|
118 |
|
public function __construct(Version $version) |
|
31
|
|
|
{ |
|
32
|
118 |
|
$config = $version->getConfiguration(); |
|
33
|
|
|
|
|
34
|
118 |
|
$this->version = $version; |
|
35
|
118 |
|
$this->connection = $config->getConnection(); |
|
36
|
118 |
|
$this->sm = $this->connection->getSchemaManager(); |
|
37
|
118 |
|
$this->platform = $this->connection->getDatabasePlatform(); |
|
38
|
118 |
|
$this->outputWriter = $config->getOutputWriter(); |
|
39
|
118 |
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Indicates the transactional mode of this migration. |
|
43
|
|
|
* |
|
44
|
|
|
* If this function returns true (default) the migration will be executed |
|
45
|
|
|
* in one transaction, otherwise non-transactional state will be used to |
|
46
|
|
|
* execute each of the migration SQLs. |
|
47
|
|
|
* |
|
48
|
|
|
* Extending class should override this function to alter the return value. |
|
49
|
|
|
*/ |
|
50
|
50 |
|
public function isTransactional() : bool |
|
51
|
|
|
{ |
|
52
|
50 |
|
return true; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
2 |
|
public function getDescription() : string |
|
56
|
|
|
{ |
|
57
|
2 |
|
return ''; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
3 |
|
public function warnIf(bool $condition, string $message = '') : void |
|
61
|
|
|
{ |
|
62
|
3 |
|
if (! $condition) { |
|
63
|
1 |
|
return; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
2 |
|
$message = $message ?: 'Unknown Reason'; |
|
67
|
|
|
|
|
68
|
2 |
|
$this->outputWriter->write(sprintf( |
|
69
|
2 |
|
' <comment>Warning during %s: %s</comment>', |
|
70
|
2 |
|
$this->version->getExecutionState(), |
|
71
|
2 |
|
$message |
|
72
|
|
|
)); |
|
73
|
2 |
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @throws AbortMigrationException |
|
77
|
|
|
*/ |
|
78
|
3 |
|
public function abortIf(bool $condition, string $message = '') : void |
|
79
|
|
|
{ |
|
80
|
3 |
|
if ($condition) { |
|
81
|
2 |
|
throw new AbortMigrationException($message ?: 'Unknown Reason'); |
|
82
|
|
|
} |
|
83
|
1 |
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* @throws SkipMigrationException |
|
87
|
|
|
*/ |
|
88
|
8 |
|
public function skipIf(bool $condition, string $message = '') : void |
|
89
|
|
|
{ |
|
90
|
8 |
|
if ($condition) { |
|
91
|
7 |
|
throw new SkipMigrationException($message ?: 'Unknown Reason'); |
|
92
|
|
|
} |
|
93
|
1 |
|
} |
|
94
|
|
|
|
|
95
|
42 |
|
public function preUp(Schema $schema) : void |
|
|
|
|
|
|
96
|
|
|
{ |
|
97
|
42 |
|
} |
|
98
|
|
|
|
|
99
|
41 |
|
public function postUp(Schema $schema) : void |
|
|
|
|
|
|
100
|
|
|
{ |
|
101
|
41 |
|
} |
|
102
|
|
|
|
|
103
|
6 |
|
public function preDown(Schema $schema) : void |
|
|
|
|
|
|
104
|
|
|
{ |
|
105
|
6 |
|
} |
|
106
|
|
|
|
|
107
|
6 |
|
public function postDown(Schema $schema) : void |
|
|
|
|
|
|
108
|
|
|
{ |
|
109
|
6 |
|
} |
|
110
|
|
|
|
|
111
|
|
|
abstract public function up(Schema $schema) : void; |
|
112
|
|
|
abstract public function down(Schema $schema) : void; |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* @param mixed[] $params |
|
116
|
|
|
* @param mixed[] $types |
|
117
|
|
|
*/ |
|
118
|
29 |
|
protected function addSql( |
|
119
|
|
|
string $sql, |
|
120
|
|
|
array $params = [], |
|
121
|
|
|
array $types = [] |
|
122
|
|
|
) : void { |
|
123
|
29 |
|
$this->version->addSql($sql, $params, $types); |
|
124
|
29 |
|
} |
|
125
|
|
|
|
|
126
|
1 |
|
protected function write(string $message) : void |
|
127
|
|
|
{ |
|
128
|
1 |
|
$this->outputWriter->write($message); |
|
129
|
1 |
|
} |
|
130
|
|
|
|
|
131
|
2 |
|
protected function throwIrreversibleMigrationException(?string $message = null) : void |
|
132
|
|
|
{ |
|
133
|
2 |
|
if ($message === null) { |
|
134
|
1 |
|
$message = 'This migration is irreversible and cannot be reverted.'; |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
2 |
|
throw new IrreversibleMigrationException($message); |
|
138
|
|
|
} |
|
139
|
|
|
} |
|
140
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.