Completed
Pull Request — master (#689)
by Jonathan
02:35
created

DumpSchemaCommand::execute()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 42
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 27
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 26
nc 3
nop 2
dl 0
loc 42
ccs 27
cts 27
cp 1
crap 3
rs 8.8571
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\Migrations\Tools\Console\Command;
6
7
use RuntimeException;
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
class DumpSchemaCommand extends AbstractCommand
15
{
16 10
    protected function configure() : void
17
    {
18 10
        parent::configure();
19
20
        $this
21 10
            ->setName('migrations:dump-schema')
22 10
            ->setAliases(['dump-schema'])
23 10
            ->setDescription('Dump the schema for your database to a migration.')
24 10
            ->setHelp(<<<EOT
25 10
The <info>%command.name%</info> command dumps the schema for your database to a migration:
26
27
    <info>%command.full_name%</info>
28
29
After dumping your schema to a migration, you can rollup your migrations using the <info>migrations:rollup</info> command.
30
EOT
31
            )
32 10
            ->addOption(
33 10
                'editor-cmd',
34 10
                null,
35 10
                InputOption::VALUE_OPTIONAL,
36 10
                'Open file with this command upon creation.'
37
            )
38 10
            ->addOption(
39 10
                'formatted',
40 10
                null,
41 10
                InputOption::VALUE_NONE,
42 10
                'Format the generated SQL.'
43
            )
44 10
            ->addOption(
45 10
                'line-length',
46 10
                null,
47 10
                InputOption::VALUE_OPTIONAL,
48 10
                'Max line length of unformatted lines.',
49 10
                120
50
            )
51
        ;
52 10
    }
53
54 3
    public function execute(
55
        InputInterface $input,
56
        OutputInterface $output
57
    ) : void {
58 3
        $formatted  = (bool) $input->getOption('formatted');
59 3
        $lineLength = (int) $input->getOption('line-length');
60
61 3
        $schemaDumper = $this->dependencyFactory->getSchemaDumper();
62 3
        $versions     = $this->migrationRepository->getVersions();
63
64 3
        if (count($versions) > 0) {
65 2
            throw new RuntimeException('Delete your old historical migrations before dumping your schema.');
66
        }
67
68 1
        $versionNumber = $this->configuration->generateVersionNumber();
69
70 1
        $path = $schemaDumper->dump(
71 1
            $versionNumber,
72 1
            $formatted,
73 1
            $lineLength
74
        );
75
76 1
        $editorCommand = $input->getOption('editor-cmd');
77
78 1
        if ($editorCommand !== null) {
79 1
            $this->procOpen($editorCommand, $path);
80
        }
81
82 1
        $output->writeln([
83 1
            sprintf('Dumped your schema to a new migration class at "<info>%s</info>"', $path),
84 1
            '',
85 1
            sprintf(
86 1
                'To run just this migration for testing purposes, you can use <info>migrations:execute --up %s</info>',
87 1
                $versionNumber
88
            ),
89 1
            '',
90 1
            sprintf(
91 1
                'To revert the migration you can use <info>migrations:execute --down %s</info>',
92 1
                $versionNumber
93
            ),
94 1
            '',
95 1
            'To use this as a rollup migration you can use the <info>migrations:rollup</info> command.',
96
        ]);
97 1
    }
98
}
99