UpdateCommand::executeSchemaCommand()   B
last analyzed

Complexity

Conditions 9
Paths 21

Size

Total Lines 65
Code Lines 36

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 90

Importance

Changes 0
Metric Value
cc 9
eloc 36
nc 21
nop 5
dl 0
loc 65
ccs 0
cts 34
cp 0
crap 90
rs 8.0555
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\ORM\Tools\Console\Command\SchemaTool;
6
7
use Doctrine\ORM\Tools\SchemaTool;
8
use Symfony\Component\Console\Input\InputInterface;
9
use Symfony\Component\Console\Input\InputOption;
10
use Symfony\Component\Console\Output\OutputInterface;
11
use Symfony\Component\Console\Style\SymfonyStyle;
12
use function count;
13
use function sprintf;
14
15
/**
16
 * Command to generate the SQL needed to update the database schema to match
17
 * the current mapping information.
18
 */
19
class UpdateCommand extends AbstractCommand
20
{
21
    /** @var string */
22
    protected $name = 'orm:schema-tool:update';
23
24
    /**
25
     * {@inheritdoc}
26
     */
27
    protected function configure()
28
    {
29
        $this->setName($this->name)
30
             ->setDescription('Executes (or dumps) the SQL needed to update the database schema to match the current mapping metadata')
31
             ->addOption('complete', null, InputOption::VALUE_NONE, 'If defined, all assets of the database which are not relevant to the current metadata will be dropped.')
32
             ->addOption('dump-sql', null, InputOption::VALUE_NONE, 'Dumps the generated SQL statements to the screen (does not execute them).')
33
             ->addOption('force', 'f', InputOption::VALUE_NONE, 'Causes the generated SQL statements to be physically executed against your database.')
34
             ->setHelp(<<<'EOT'
35
The <info>%command.name%</info> command generates the SQL needed to
36
synchronize the database schema with the current mapping metadata of the
37
default entity manager.
38
39
For example, if you add metadata for a new column to an entity, this command
40
would generate and output the SQL needed to add the new column to the database:
41
42
<info>%command.name% --dump-sql</info>
43
44
Alternatively, you can execute the generated queries:
45
46
<info>%command.name% --force</info>
47
48
If both options are specified, the queries are output and then executed:
49
50
<info>%command.name% --dump-sql --force</info>
51
52
Finally, be aware that if the <info>--complete</info> option is passed, this
53
task will drop all database assets (e.g. tables, etc) that are *not* described
54
by the current metadata. In other words, without this option, this task leaves
55
untouched any "extra" tables that exist in the database, but which aren't
56
described by any metadata.
57
58
<comment>Hint:</comment> If you have a database with tables that should not be managed
59
by the ORM, you can use a DBAL functionality to filter the tables and sequences down
60
on a global level:
61
62
    $config->setFilterSchemaAssetsExpression($regexp);
63
EOT
64
             );
65
    }
66
67
    /**
68
     * {@inheritdoc}
69
     */
70
    protected function executeSchemaCommand(InputInterface $input, OutputInterface $output, SchemaTool $schemaTool, array $metadatas, SymfonyStyle $ui)
71
    {
72
        // Defining if update is complete or not (--complete not defined means $saveMode = true)
73
        $saveMode = ! $input->getOption('complete');
74
75
        $sqls = $schemaTool->getUpdateSchemaSql($metadatas, $saveMode);
76
77
        if (empty($sqls)) {
78
            $ui->success('Nothing to update - your database is already in sync with the current entity metadata.');
79
80
            return 0;
81
        }
82
83
        $dumpSql = $input->getOption('dump-sql') === true;
84
        $force   = $input->getOption('force') === true;
85
86
        if ($dumpSql) {
87
            $ui->text('The following SQL statements will be executed:');
88
            $ui->newLine();
89
90
            foreach ($sqls as $sql) {
91
                $ui->text(sprintf('    %s;', $sql));
92
            }
93
        }
94
95
        if ($force) {
96
            if ($dumpSql) {
97
                $ui->newLine();
98
            }
99
            $ui->text('Updating database schema...');
100
            $ui->newLine();
101
102
            $schemaTool->updateSchema($metadatas, $saveMode);
103
104
            $pluralization = count($sqls) === 1 ? 'query was' : 'queries were';
105
106
            $ui->text(sprintf('    <info>%s</info> %s executed', count($sqls), $pluralization));
107
            $ui->success('Database schema updated successfully!');
108
        }
109
110
        if ($dumpSql || $force) {
111
            return 0;
112
        }
113
114
        $ui->caution(
115
            [
116
                'This operation should not be executed in a production environment!',
117
                '',
118
                'Use the incremental update to detect changes during development and use',
119
                'the SQL DDL provided to manually update your database in production.',
120
            ]
121
        );
122
123
        $ui->text(
124
            [
125
                sprintf('The Schema-Tool would execute <info>"%s"</info> queries to update the database.', count($sqls)),
126
                '',
127
                'Please run the operation by passing one - or both - of the following options:',
128
                '',
129
                sprintf('    <info>%s --force</info> to execute the command', $this->getName()),
130
                sprintf('    <info>%s --dump-sql</info> to dump the SQL statements to the screen', $this->getName()),
131
            ]
132
        );
133
134
        return 1;
135
    }
136
}
137