1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Doctrine\Migrations\Tools\Console\Command; |
6
|
|
|
|
7
|
|
|
use Doctrine\Migrations\Generator\Exception\NoChangesDetected; |
8
|
|
|
use Doctrine\Migrations\Tools\Console\Exception\InvalidOptionUsage; |
9
|
|
|
use OutOfBoundsException; |
10
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
11
|
|
|
use Symfony\Component\Console\Input\InputOption; |
12
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
13
|
|
|
use function addslashes; |
14
|
|
|
use function assert; |
15
|
|
|
use function class_exists; |
16
|
|
|
use function filter_var; |
17
|
|
|
use function is_string; |
18
|
|
|
use function key; |
19
|
|
|
use function sprintf; |
20
|
|
|
use const FILTER_VALIDATE_BOOLEAN; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* The DiffCommand class is responsible for generating a migration by comparing your current database schema to |
24
|
|
|
* your mapping information. |
25
|
|
|
*/ |
26
|
|
|
final class DiffCommand extends DoctrineCommand |
27
|
|
|
{ |
28
|
|
|
/** @var string */ |
29
|
|
|
protected static $defaultName = 'migrations:diff'; |
30
|
|
|
|
31
|
1 |
|
protected function configure() : void |
32
|
|
|
{ |
33
|
1 |
|
parent::configure(); |
34
|
|
|
|
35
|
|
|
$this |
36
|
1 |
|
->setAliases(['diff']) |
37
|
1 |
|
->setDescription('Generate a migration by comparing your current database to your mapping information.') |
38
|
1 |
|
->setHelp(<<<EOT |
39
|
1 |
|
The <info>%command.name%</info> command generates a migration by comparing your current database to your mapping information: |
40
|
|
|
|
41
|
|
|
<info>%command.full_name%</info> |
42
|
|
|
|
43
|
|
|
EOT |
44
|
|
|
) |
45
|
1 |
|
->addOption( |
46
|
1 |
|
'namespace', |
47
|
1 |
|
null, |
48
|
1 |
|
InputOption::VALUE_REQUIRED, |
49
|
1 |
|
'The namespace to use for the migration (must be in the list of configured namespaces)' |
50
|
|
|
) |
51
|
1 |
|
->addOption( |
52
|
1 |
|
'filter-expression', |
53
|
1 |
|
null, |
54
|
1 |
|
InputOption::VALUE_REQUIRED, |
55
|
1 |
|
'Tables which are filtered by Regular Expression.' |
56
|
|
|
) |
57
|
1 |
|
->addOption( |
58
|
1 |
|
'formatted', |
59
|
1 |
|
null, |
60
|
1 |
|
InputOption::VALUE_NONE, |
61
|
1 |
|
'Format the generated SQL.' |
62
|
|
|
) |
63
|
1 |
|
->addOption( |
64
|
1 |
|
'line-length', |
65
|
1 |
|
null, |
66
|
1 |
|
InputOption::VALUE_REQUIRED, |
67
|
1 |
|
'Max line length of unformatted lines.', |
68
|
1 |
|
120 |
69
|
|
|
) |
70
|
1 |
|
->addOption( |
71
|
1 |
|
'check-database-platform', |
72
|
1 |
|
null, |
73
|
1 |
|
InputOption::VALUE_OPTIONAL, |
74
|
1 |
|
'Check Database Platform to the generated code.', |
75
|
1 |
|
false |
76
|
|
|
) |
77
|
1 |
|
->addOption( |
78
|
1 |
|
'allow-empty-diff', |
79
|
1 |
|
null, |
80
|
1 |
|
InputOption::VALUE_NONE, |
81
|
1 |
|
'Do not throw an exception when no changes are detected.' |
82
|
|
|
); |
83
|
1 |
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @throws InvalidOptionUsage |
87
|
|
|
*/ |
88
|
1 |
|
public function execute( |
89
|
|
|
InputInterface $input, |
90
|
|
|
OutputInterface $output |
91
|
|
|
) : ?int { |
92
|
1 |
|
$filterExpression = (string) $input->getOption('filter-expression') ?: null; |
93
|
1 |
|
$formatted = filter_var($input->getOption('formatted'), FILTER_VALIDATE_BOOLEAN); |
94
|
1 |
|
$lineLength = (int) $input->getOption('line-length'); |
95
|
1 |
|
$allowEmptyDiff = $input->getOption('allow-empty-diff'); |
96
|
1 |
|
$checkDbPlatform = filter_var($input->getOption('check-database-platform'), FILTER_VALIDATE_BOOLEAN); |
97
|
1 |
|
$namespace = $input->getOption('namespace') ?: null; |
98
|
1 |
|
if ($formatted) { |
99
|
1 |
|
if (! class_exists('SqlFormatter')) { |
100
|
|
|
throw InvalidOptionUsage::new( |
101
|
|
|
'The "--formatted" option can only be used if the sql formatter is installed. Please run "composer require jdorn/sql-formatter".' |
102
|
|
|
); |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
106
|
1 |
|
$configuration = $this->getDependencyFactory()->getConfiguration(); |
107
|
|
|
|
108
|
1 |
|
$dirs = $configuration->getMigrationDirectories(); |
109
|
1 |
|
if ($namespace === null) { |
110
|
|
|
$namespace = key($dirs); |
111
|
1 |
|
} elseif (! isset($dirs[$namespace])) { |
112
|
|
|
throw new OutOfBoundsException(sprintf('Path not defined for the namespace %s', $namespace)); |
113
|
|
|
} |
114
|
|
|
|
115
|
1 |
|
assert(is_string($namespace)); |
116
|
|
|
|
117
|
1 |
|
$fqcn = $this->getDependencyFactory()->getClassNameGenerator()->generateClassName($namespace); |
118
|
|
|
|
119
|
1 |
|
$diffGenerator = $this->getDependencyFactory()->getDiffGenerator(); |
120
|
|
|
|
121
|
|
|
try { |
122
|
1 |
|
$path = $diffGenerator->generate( |
123
|
1 |
|
$fqcn, |
124
|
1 |
|
$filterExpression, |
125
|
1 |
|
$formatted, |
126
|
1 |
|
$lineLength, |
127
|
1 |
|
$checkDbPlatform |
128
|
|
|
); |
129
|
|
|
} catch (NoChangesDetected $exception) { |
130
|
|
|
if ($allowEmptyDiff) { |
131
|
|
|
$output->writeln($exception->getMessage()); |
132
|
|
|
|
133
|
|
|
return 0; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
throw $exception; |
137
|
|
|
} |
138
|
|
|
|
139
|
1 |
|
$output->writeln([ |
140
|
1 |
|
sprintf('Generated new migration class to "<info>%s</info>"', $path), |
141
|
1 |
|
'', |
142
|
1 |
|
sprintf( |
143
|
1 |
|
'To run just this migration for testing purposes, you can use <info>migrations:execute --up \'%s\'</info>', |
144
|
1 |
|
addslashes($fqcn) |
145
|
|
|
), |
146
|
1 |
|
'', |
147
|
1 |
|
sprintf( |
148
|
1 |
|
'To revert the migration you can use <info>migrations:execute --down \'%s\'</info>', |
149
|
1 |
|
addslashes($fqcn) |
150
|
|
|
), |
151
|
|
|
]); |
152
|
|
|
|
153
|
1 |
|
return 0; |
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
|