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