Completed
Pull Request — master (#973)
by Asmir
02:18
created

DumpSchemaCommand::configure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 39
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 26
CRAP Score 1

Importance

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

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