1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Doctrine\Migrations\Tools\Console\Command; |
6
|
|
|
|
7
|
|
|
use Doctrine\Migrations\Generator\DiffGenerator; |
8
|
|
|
use Doctrine\Migrations\Generator\Exception\NoChangesDetected; |
9
|
|
|
use Doctrine\Migrations\Provider\OrmSchemaProvider; |
10
|
|
|
use Doctrine\Migrations\Provider\SchemaProviderInterface; |
11
|
|
|
use Doctrine\Migrations\Tools\Console\Exception\InvalidOptionUsage; |
12
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
13
|
|
|
use Symfony\Component\Console\Input\InputOption; |
14
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
15
|
|
|
use const FILTER_VALIDATE_BOOLEAN; |
16
|
|
|
use function class_exists; |
17
|
|
|
use function filter_var; |
18
|
|
|
use function sprintf; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* The DiffCommand class is responsible for generating a migration by comparing your current database schema to |
22
|
|
|
* your mapping information. |
23
|
|
|
*/ |
24
|
|
|
class DiffCommand extends AbstractCommand |
25
|
|
|
{ |
26
|
|
|
/** @var string */ |
27
|
|
|
protected static $defaultName = 'migrations:diff'; |
28
|
|
|
|
29
|
|
|
/** @var SchemaProviderInterface|null */ |
30
|
|
|
protected $schemaProvider; |
31
|
|
|
|
32
|
6 |
|
public function __construct(?SchemaProviderInterface $schemaProvider = null) |
33
|
|
|
{ |
34
|
6 |
|
$this->schemaProvider = $schemaProvider; |
35
|
|
|
|
36
|
6 |
|
parent::__construct(); |
37
|
6 |
|
} |
38
|
|
|
|
39
|
6 |
|
protected function configure() : void |
40
|
|
|
{ |
41
|
6 |
|
parent::configure(); |
42
|
|
|
|
43
|
|
|
$this |
44
|
6 |
|
->setAliases(['diff']) |
45
|
6 |
|
->setDescription('Generate a migration by comparing your current database to your mapping information.') |
46
|
6 |
|
->setHelp(<<<EOT |
47
|
6 |
|
The <info>%command.name%</info> command generates a migration by comparing your current database to your mapping information: |
48
|
|
|
|
49
|
|
|
<info>%command.full_name%</info> |
50
|
|
|
|
51
|
|
|
You can optionally specify a <comment>--editor-cmd</comment> option to open the generated file in your favorite editor: |
52
|
|
|
|
53
|
|
|
<info>%command.full_name% --editor-cmd=mate</info> |
54
|
|
|
EOT |
55
|
|
|
) |
56
|
6 |
|
->addOption( |
57
|
6 |
|
'editor-cmd', |
58
|
6 |
|
null, |
59
|
6 |
|
InputOption::VALUE_OPTIONAL, |
60
|
6 |
|
'Open file with this command upon creation.' |
61
|
|
|
) |
62
|
6 |
|
->addOption( |
63
|
6 |
|
'filter-expression', |
64
|
6 |
|
null, |
65
|
6 |
|
InputOption::VALUE_OPTIONAL, |
66
|
6 |
|
'Tables which are filtered by Regular Expression.' |
67
|
|
|
) |
68
|
6 |
|
->addOption( |
69
|
6 |
|
'formatted', |
70
|
6 |
|
null, |
71
|
6 |
|
InputOption::VALUE_NONE, |
72
|
6 |
|
'Format the generated SQL.' |
73
|
|
|
) |
74
|
6 |
|
->addOption( |
75
|
6 |
|
'line-length', |
76
|
6 |
|
null, |
77
|
6 |
|
InputOption::VALUE_OPTIONAL, |
78
|
6 |
|
'Max line length of unformatted lines.', |
79
|
6 |
|
120 |
80
|
|
|
) |
81
|
6 |
|
->addOption( |
82
|
6 |
|
'check-database-platform', |
83
|
6 |
|
null, |
84
|
6 |
|
InputOption::VALUE_OPTIONAL, |
85
|
6 |
|
'Check Database Platform to the generated code.', |
86
|
6 |
|
true |
87
|
|
|
) |
88
|
6 |
|
->addOption( |
89
|
6 |
|
'allow-empty-diff', |
90
|
6 |
|
null, |
91
|
6 |
|
InputOption::VALUE_NONE, |
92
|
6 |
|
'Do not throw an exception when no changes are detected.' |
93
|
|
|
); |
94
|
6 |
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @throws InvalidOptionUsage |
98
|
|
|
*/ |
99
|
6 |
|
public function execute( |
100
|
|
|
InputInterface $input, |
101
|
|
|
OutputInterface $output |
102
|
|
|
) : ?int { |
103
|
6 |
|
$filterExpression = $input->getOption('filter-expression') ?? null; |
104
|
6 |
|
$formatted = (bool) $input->getOption('formatted'); |
105
|
6 |
|
$lineLength = (int) $input->getOption('line-length'); |
106
|
6 |
|
$allowEmptyDiff = (bool) $input->getOption('allow-empty-diff'); |
107
|
6 |
|
$checkDbPlatform = filter_var($input->getOption('check-database-platform'), FILTER_VALIDATE_BOOLEAN); |
108
|
|
|
|
109
|
6 |
|
if ($formatted) { |
110
|
2 |
|
if (! class_exists('SqlFormatter')) { |
111
|
|
|
throw InvalidOptionUsage::new( |
112
|
|
|
'The "--formatted" option can only be used if the sql formatter is installed. Please run "composer require jdorn/sql-formatter".' |
113
|
|
|
); |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|
117
|
6 |
|
$versionNumber = $this->configuration->generateVersionNumber(); |
118
|
|
|
|
119
|
|
|
try { |
120
|
6 |
|
$path = $this->createMigrationDiffGenerator()->generate( |
121
|
6 |
|
$versionNumber, |
122
|
6 |
|
$filterExpression, |
123
|
6 |
|
$formatted, |
124
|
6 |
|
$lineLength, |
125
|
6 |
|
$checkDbPlatform |
126
|
|
|
); |
127
|
|
|
} catch (NoChangesDetected $exception) { |
128
|
|
|
if ($allowEmptyDiff) { |
129
|
|
|
$output->writeln($exception->getMessage()); |
130
|
|
|
|
131
|
|
|
return 0; |
132
|
|
|
} |
133
|
|
|
throw $exception; |
134
|
|
|
} |
135
|
|
|
|
136
|
6 |
|
$editorCommand = $input->getOption('editor-cmd'); |
137
|
|
|
|
138
|
6 |
|
if ($editorCommand !== null) { |
139
|
1 |
|
$this->procOpen($editorCommand, $path); |
140
|
|
|
} |
141
|
|
|
|
142
|
6 |
|
$output->writeln([ |
143
|
6 |
|
sprintf('Generated new migration class to "<info>%s</info>"', $path), |
144
|
6 |
|
'', |
145
|
6 |
|
sprintf( |
146
|
6 |
|
'To run just this migration for testing purposes, you can use <info>migrations:execute --up %s</info>', |
147
|
6 |
|
$versionNumber |
148
|
|
|
), |
149
|
6 |
|
'', |
150
|
6 |
|
sprintf( |
151
|
6 |
|
'To revert the migration you can use <info>migrations:execute --down %s</info>', |
152
|
6 |
|
$versionNumber |
153
|
|
|
), |
154
|
|
|
]); |
155
|
|
|
|
156
|
6 |
|
return 0; |
157
|
|
|
} |
158
|
|
|
|
159
|
5 |
|
protected function createMigrationDiffGenerator() : DiffGenerator |
160
|
|
|
{ |
161
|
5 |
|
return new DiffGenerator( |
162
|
5 |
|
$this->connection->getConfiguration(), |
163
|
5 |
|
$this->connection->getSchemaManager(), |
164
|
5 |
|
$this->getSchemaProvider(), |
165
|
5 |
|
$this->connection->getDatabasePlatform(), |
166
|
5 |
|
$this->dependencyFactory->getMigrationGenerator(), |
167
|
5 |
|
$this->dependencyFactory->getMigrationSqlGenerator() |
168
|
|
|
); |
169
|
|
|
} |
170
|
|
|
|
171
|
5 |
|
private function getSchemaProvider() : SchemaProviderInterface |
172
|
|
|
{ |
173
|
5 |
|
if ($this->schemaProvider === null) { |
174
|
1 |
|
$this->schemaProvider = new OrmSchemaProvider( |
175
|
1 |
|
$this->getHelper('entityManager')->getEntityManager() |
176
|
|
|
); |
177
|
|
|
} |
178
|
|
|
|
179
|
5 |
|
return $this->schemaProvider; |
180
|
|
|
} |
181
|
|
|
} |
182
|
|
|
|