Failed Conditions
Push — master ( 85e30d...9d20a3 )
by Andreas
02:08
created

DumpSchemaCommand   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 97
Duplicated Lines 0 %

Test Coverage

Coverage 96.36%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 6
eloc 59
c 2
b 0
f 0
dl 0
loc 97
ccs 53
cts 55
cp 0.9636
rs 10

2 Methods

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