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