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