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