1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Doctrine\Migrations\Tools\Console\Command; |
6
|
|
|
|
7
|
|
|
use Doctrine\Migrations\Tools\Console\Exception\SchemaDumpRequiresNoMigrations; |
8
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
9
|
|
|
use Symfony\Component\Console\Input\InputOption; |
10
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
11
|
|
|
use function count; |
12
|
|
|
use function sprintf; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* The DumpSchemaCommand class is responsible for dumping your current database schema to a migration class. This is |
16
|
|
|
* intended to be used in conjuction with the RollupCommand. |
17
|
|
|
* |
18
|
|
|
* @see Doctrine\Migrations\Tools\Console\Command\RollupCommand |
19
|
|
|
*/ |
20
|
|
|
class DumpSchemaCommand extends AbstractCommand |
21
|
|
|
{ |
22
|
10 |
|
protected function configure() : void |
23
|
|
|
{ |
24
|
10 |
|
parent::configure(); |
25
|
|
|
|
26
|
|
|
$this |
27
|
10 |
|
->setName('migrations:dump-schema') |
28
|
10 |
|
->setAliases(['dump-schema']) |
29
|
10 |
|
->setDescription('Dump the schema for your database to a migration.') |
30
|
10 |
|
->setHelp(<<<EOT |
31
|
10 |
|
The <info>%command.name%</info> command dumps the schema for your database to a migration: |
32
|
|
|
|
33
|
|
|
<info>%command.full_name%</info> |
34
|
|
|
|
35
|
|
|
After dumping your schema to a migration, you can rollup your migrations using the <info>migrations:rollup</info> command. |
36
|
|
|
EOT |
37
|
|
|
) |
38
|
10 |
|
->addOption( |
39
|
10 |
|
'editor-cmd', |
40
|
10 |
|
null, |
41
|
10 |
|
InputOption::VALUE_OPTIONAL, |
42
|
10 |
|
'Open file with this command upon creation.' |
43
|
|
|
) |
44
|
10 |
|
->addOption( |
45
|
10 |
|
'formatted', |
46
|
10 |
|
null, |
47
|
10 |
|
InputOption::VALUE_NONE, |
48
|
10 |
|
'Format the generated SQL.' |
49
|
|
|
) |
50
|
10 |
|
->addOption( |
51
|
10 |
|
'line-length', |
52
|
10 |
|
null, |
53
|
10 |
|
InputOption::VALUE_OPTIONAL, |
54
|
10 |
|
'Max line length of unformatted lines.', |
55
|
10 |
|
120 |
56
|
|
|
) |
57
|
|
|
; |
58
|
10 |
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @throws SchemaDumpRequiresNoMigrations |
62
|
|
|
*/ |
63
|
3 |
|
public function execute( |
64
|
|
|
InputInterface $input, |
65
|
|
|
OutputInterface $output |
66
|
|
|
) : ?int { |
67
|
3 |
|
$formatted = (bool) $input->getOption('formatted'); |
68
|
3 |
|
$lineLength = (int) $input->getOption('line-length'); |
69
|
|
|
|
70
|
3 |
|
$schemaDumper = $this->dependencyFactory->getSchemaDumper(); |
71
|
3 |
|
$versions = $this->migrationRepository->getVersions(); |
72
|
|
|
|
73
|
3 |
|
if (count($versions) > 0) { |
74
|
2 |
|
throw SchemaDumpRequiresNoMigrations::new(); |
75
|
|
|
} |
76
|
|
|
|
77
|
1 |
|
$versionNumber = $this->configuration->generateVersionNumber(); |
78
|
|
|
|
79
|
1 |
|
$path = $schemaDumper->dump( |
80
|
1 |
|
$versionNumber, |
81
|
1 |
|
$formatted, |
82
|
1 |
|
$lineLength |
83
|
|
|
); |
84
|
|
|
|
85
|
1 |
|
$editorCommand = $input->getOption('editor-cmd'); |
86
|
|
|
|
87
|
1 |
|
if ($editorCommand !== null) { |
88
|
1 |
|
$this->procOpen($editorCommand, $path); |
89
|
|
|
} |
90
|
|
|
|
91
|
1 |
|
$output->writeln([ |
92
|
1 |
|
sprintf('Dumped your schema to a new migration class at "<info>%s</info>"', $path), |
93
|
1 |
|
'', |
94
|
1 |
|
sprintf( |
95
|
1 |
|
'To run just this migration for testing purposes, you can use <info>migrations:execute --up %s</info>', |
96
|
1 |
|
$versionNumber |
97
|
|
|
), |
98
|
1 |
|
'', |
99
|
1 |
|
sprintf( |
100
|
1 |
|
'To revert the migration you can use <info>migrations:execute --down %s</info>', |
101
|
1 |
|
$versionNumber |
102
|
|
|
), |
103
|
1 |
|
'', |
104
|
1 |
|
'To use this as a rollup migration you can use the <info>migrations:rollup</info> command.', |
105
|
|
|
]); |
106
|
|
|
|
107
|
1 |
|
return 0; |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|