Failed Conditions
Pull Request — master (#923)
by
unknown
11:18
created

DumpSchemaCommand::configure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 45
Code Lines 37

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 32
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 37
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 45
ccs 32
cts 32
cp 1
crap 1
rs 9.328
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 addslashes;
13
use function assert;
14
use function class_exists;
15
use function is_string;
16
use function key;
17
use function sprintf;
18
use function strpos;
19
20
/**
21
 * The DumpSchemaCommand class is responsible for dumping your current database schema to a migration class. This is
22
 * intended to be used in conjuction with the RollupCommand.
23
 *
24
 * @see Doctrine\Migrations\Tools\Console\Command\RollupCommand
25
 */
26
class DumpSchemaCommand extends DoctrineCommand
27
{
28
    /** @var string */
29
    protected static $defaultName = 'migrations:dump-schema';
30
31 1
    protected function configure() : void
32
    {
33 1
        parent::configure();
34
35
        $this
36 1
            ->setAliases(['dump-schema'])
37 1
            ->setDescription('Dump the schema for your database to a migration.')
38 1
            ->setHelp(<<<EOT
39 1
The <info>%command.name%</info> command dumps the schema for your database to a migration:
40
41
    <info>%command.full_name%</info>
42
43
After dumping your schema to a migration, you can rollup your migrations using the <info>migrations:rollup</info> command.
44
EOT
45
            )
46 1
            ->addOption(
47 1
                'editor-cmd',
48 1
                null,
49 1
                InputOption::VALUE_OPTIONAL,
50 1
                'Open file with this command upon creation.'
51
            )
52 1
            ->addOption(
53 1
                'formatted',
54 1
                null,
55 1
                InputOption::VALUE_NONE,
56 1
                'Format the generated SQL.'
57
            )
58 1
            ->addOption(
59 1
                'namespace',
60 1
                null,
61 1
                InputOption::VALUE_REQUIRED,
62 1
                'Namespace to use for the generated migrations (defaults to the first namespace definition).'
63
            )
64 1
            ->addOption(
65 1
                'filter-tables',
66 1
                null,
67 1
                InputOption::VALUE_REQUIRED|InputOption::VALUE_IS_ARRAY,
68 1
                'Filter the tables to dump via Regex.'
69
            )
70 1
            ->addOption(
71 1
                'line-length',
72 1
                null,
73 1
                InputOption::VALUE_OPTIONAL,
74 1
                'Max line length of unformatted lines.',
75 1
                120
76
            );
77 1
    }
78
79
    /**
80
     * @throws SchemaDumpRequiresNoMigrations
81
     */
82
    protected function execute(
83
        InputInterface $input,
84
        OutputInterface $output
85
    ) : ?int {
86
        $formatted  = $input->getOption('formatted');
87
        $lineLength = (int) $input->getOption('line-length');
88
89
        $schemaDumper = $this->getDependencyFactory()->getSchemaDumper();
90
91
        if ($formatted) {
92
            if (! class_exists('SqlFormatter')) {
93
                throw InvalidOptionUsage::new(
94
                    'The "--formatted" option can only be used if the sql formatter is installed. Please run "composer require jdorn/sql-formatter".'
95
                );
96
            }
97
        }
98
99
        $configuration = $this->getDependencyFactory()->getConfiguration();
100
101
        $namespace = $input->getOption('namespace');
102
        if ($namespace === null) {
103
            $dirs      = $configuration->getMigrationDirectories();
104
            $namespace = key($dirs);
105
        }
106
107
        assert(is_string($namespace));
108
109
        $this->checkNoPreviousDumpExistsForNamespace($namespace);
110
111
        $fqcn = $this->getDependencyFactory()->getClassNameGenerator()->generateClassName($namespace);
112
113
        $path = $schemaDumper->dump(
114
            $fqcn,
115
            $input->getOption('filter-tables'),
0 ignored issues
show
Bug introduced by
It seems like $input->getOption('filter-tables') can also be of type boolean and null and string; however, parameter $excludedTablesRegexes of Doctrine\Migrations\SchemaDumper::dump() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

115
            /** @scrutinizer ignore-type */ $input->getOption('filter-tables'),
Loading history...
116
            $formatted,
117
            $lineLength
118
        );
119
120
        $editorCommand = $input->getOption('editor-cmd');
121
122
        if ($editorCommand !== null) {
123
            assert(is_string($editorCommand));
124
            $this->procOpen($editorCommand, $path);
125
        }
126
127
        $output->writeln([
128
            sprintf('Dumped your schema to a new migration class at "<info>%s</info>"', $path),
129
            '',
130
            sprintf(
131
                'To run just this migration for testing purposes, you can use <info>migrations:execute --up \'%s\'</info>',
132
                addslashes($fqcn)
133
            ),
134
            '',
135
            sprintf(
136
                'To revert the migration you can use <info>migrations:execute --down \'%s\'</info>',
137
                addslashes($fqcn)
138
            ),
139
            '',
140
            'To use this as a rollup migration you can use the <info>migrations:rollup</info> command.',
141
        ]);
142
143
        return 0;
144
    }
145
146
    private function checkNoPreviousDumpExistsForNamespace(string $namespace) : void
147
    {
148
        $migrations = $this->getDependencyFactory()->getMigrationRepository()->getMigrations();
149
        foreach ($migrations->getItems() as $migration) {
150
            if (strpos((string) $migration->getVersion(), $namespace) !== false) {
151
                throw SchemaDumpRequiresNoMigrations::new($namespace);
152
            }
153
        }
154
    }
155
}
156