1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Doctrine\Migrations\Tools\Console\Command; |
6
|
|
|
|
7
|
|
|
use Doctrine\Migrations\Tools\Console\Exception\InvalidOptionUsage; |
8
|
|
|
use Doctrine\Migrations\Tools\Console\Exception\SchemaDumpRequiresNoMigrations; |
9
|
|
|
use Doctrine\SqlFormatter\SqlFormatter; |
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 is_string; |
17
|
|
|
use function key; |
18
|
|
|
use function sprintf; |
19
|
|
|
use function strpos; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* The DumpSchemaCommand class is responsible for dumping your current database schema to a migration class. This is |
23
|
|
|
* intended to be used in conjunction with the RollupCommand. |
24
|
|
|
* |
25
|
|
|
* @see Doctrine\Migrations\Tools\Console\Command\RollupCommand |
26
|
|
|
*/ |
27
|
|
|
final class DumpSchemaCommand extends DoctrineCommand |
28
|
|
|
{ |
29
|
|
|
/** @var string */ |
30
|
|
|
protected static $defaultName = 'migrations:dump-schema'; |
31
|
|
|
|
32
|
2 |
|
protected function configure() : void |
33
|
|
|
{ |
34
|
2 |
|
parent::configure(); |
35
|
|
|
|
36
|
|
|
$this |
37
|
2 |
|
->setAliases(['dump-schema']) |
38
|
2 |
|
->setDescription('Dump the schema for your database to a migration.') |
39
|
2 |
|
->setHelp(<<<EOT |
40
|
2 |
|
The <info>%command.name%</info> command dumps the schema for your database to a migration: |
41
|
|
|
|
42
|
|
|
<info>%command.full_name%</info> |
43
|
|
|
|
44
|
|
|
After dumping your schema to a migration, you can rollup your migrations using the <info>migrations:rollup</info> command. |
45
|
|
|
EOT |
46
|
|
|
) |
47
|
2 |
|
->addOption( |
48
|
2 |
|
'formatted', |
49
|
2 |
|
null, |
50
|
2 |
|
InputOption::VALUE_NONE, |
51
|
2 |
|
'Format the generated SQL.' |
52
|
|
|
) |
53
|
2 |
|
->addOption( |
54
|
2 |
|
'namespace', |
55
|
2 |
|
null, |
56
|
2 |
|
InputOption::VALUE_REQUIRED, |
57
|
2 |
|
'Namespace to use for the generated migrations (defaults to the first namespace definition).' |
58
|
|
|
) |
59
|
2 |
|
->addOption( |
60
|
2 |
|
'filter-tables', |
61
|
2 |
|
null, |
62
|
2 |
|
InputOption::VALUE_REQUIRED|InputOption::VALUE_IS_ARRAY, |
63
|
2 |
|
'Filter the tables to dump via Regex.' |
64
|
|
|
) |
65
|
2 |
|
->addOption( |
66
|
2 |
|
'line-length', |
67
|
2 |
|
null, |
68
|
2 |
|
InputOption::VALUE_OPTIONAL, |
69
|
2 |
|
'Max line length of unformatted lines.', |
70
|
2 |
|
120 |
71
|
|
|
); |
72
|
2 |
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @throws SchemaDumpRequiresNoMigrations |
76
|
|
|
*/ |
77
|
2 |
|
public function execute( |
78
|
|
|
InputInterface $input, |
79
|
|
|
OutputInterface $output |
80
|
|
|
) : int { |
81
|
2 |
|
$formatted = $input->getOption('formatted'); |
82
|
2 |
|
$lineLength = (int) $input->getOption('line-length'); |
83
|
|
|
|
84
|
2 |
|
$schemaDumper = $this->getDependencyFactory()->getSchemaDumper(); |
85
|
|
|
|
86
|
2 |
|
if ($formatted) { |
87
|
1 |
|
if (! class_exists(SqlFormatter::class)) { |
88
|
|
|
throw InvalidOptionUsage::new( |
89
|
|
|
'The "--formatted" option can only be used if the sql formatter is installed. Please run "composer require doctrine/sql-formatter".' |
90
|
|
|
); |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
94
|
2 |
|
$configuration = $this->getDependencyFactory()->getConfiguration(); |
95
|
|
|
|
96
|
2 |
|
$namespace = $input->getOption('namespace'); |
97
|
2 |
|
if ($namespace === null) { |
98
|
2 |
|
$dirs = $configuration->getMigrationDirectories(); |
99
|
2 |
|
$namespace = key($dirs); |
100
|
|
|
} |
101
|
|
|
|
102
|
2 |
|
assert(is_string($namespace)); |
103
|
|
|
|
104
|
2 |
|
$this->checkNoPreviousDumpExistsForNamespace($namespace); |
105
|
|
|
|
106
|
1 |
|
$fqcn = $this->getDependencyFactory()->getClassNameGenerator()->generateClassName($namespace); |
107
|
|
|
|
108
|
1 |
|
$path = $schemaDumper->dump( |
109
|
1 |
|
$fqcn, |
110
|
1 |
|
$input->getOption('filter-tables'), |
111
|
1 |
|
$formatted, |
112
|
1 |
|
$lineLength |
113
|
|
|
); |
114
|
|
|
|
115
|
1 |
|
$this->io->text([ |
116
|
1 |
|
sprintf('Dumped your schema to a new migration class at "<info>%s</info>"', $path), |
117
|
1 |
|
'', |
118
|
1 |
|
sprintf( |
119
|
1 |
|
'To run just this migration for testing purposes, you can use <info>migrations:execute --up \'%s\'</info>', |
120
|
1 |
|
addslashes($fqcn) |
121
|
|
|
), |
122
|
1 |
|
'', |
123
|
1 |
|
sprintf( |
124
|
1 |
|
'To revert the migration you can use <info>migrations:execute --down \'%s\'</info>', |
125
|
1 |
|
addslashes($fqcn) |
126
|
|
|
), |
127
|
1 |
|
'', |
128
|
1 |
|
'To use this as a rollup migration you can use the <info>migrations:rollup</info> command.', |
129
|
1 |
|
'', |
130
|
|
|
]); |
131
|
|
|
|
132
|
1 |
|
return 0; |
133
|
|
|
} |
134
|
|
|
|
135
|
2 |
|
private function checkNoPreviousDumpExistsForNamespace(string $namespace) : void |
136
|
|
|
{ |
137
|
2 |
|
$migrations = $this->getDependencyFactory()->getMigrationRepository()->getMigrations(); |
138
|
2 |
|
foreach ($migrations->getItems() as $migration) { |
139
|
1 |
|
if (strpos((string) $migration->getVersion(), $namespace) !== false) { |
140
|
1 |
|
throw SchemaDumpRequiresNoMigrations::new($namespace); |
141
|
|
|
} |
142
|
|
|
} |
143
|
1 |
|
} |
144
|
|
|
} |
145
|
|
|
|