Completed
Push — master ( ba3223...b47a39 )
by Luís
17s
created

UpdateCommand   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 118
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 118
rs 10
c 0
b 0
f 0
ccs 0
cts 43
cp 0
wmc 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 8 1
C executeSchemaCommand() 0 65 9
1
<?php
2
/*
3
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14
 *
15
 * This software consists of voluntary contributions made by many individuals
16
 * and is licensed under the MIT license. For more information, see
17
 * <http://www.doctrine-project.org>.
18
 */
19
20
namespace Doctrine\ORM\Tools\Console\Command\SchemaTool;
21
22
use Doctrine\ORM\Tools\SchemaTool;
23
use Symfony\Component\Console\Input\InputInterface;
24
use Symfony\Component\Console\Input\InputOption;
25
use Symfony\Component\Console\Output\OutputInterface;
26
use Symfony\Component\Console\Style\SymfonyStyle;
27
28
/**
29
 * Command to generate the SQL needed to update the database schema to match
30
 * the current mapping information.
31
 *
32
 * @link    www.doctrine-project.org
33
 * @since   2.0
34
 * @author  Benjamin Eberlei <[email protected]>
35
 * @author  Guilherme Blanco <[email protected]>
36
 * @author  Jonathan Wage <[email protected]>
37
 * @author  Roman Borschel <[email protected]>
38
 * @author  Ryan Weaver <[email protected]>
39
 */
40
class UpdateCommand extends AbstractCommand
41
{
42
    /**
43
     * @var string
44
     */
45
    protected $name = 'orm:schema-tool:update';
46
47
    /**
48
     * {@inheritdoc}
49
     */
50
    protected function configure()
51
    {
52
        $this->setName($this->name)
53
             ->setDescription('Executes (or dumps) the SQL needed to update the database schema to match the current mapping metadata')
54
             ->addOption('complete', null, InputOption::VALUE_NONE, 'If defined, all assets of the database which are not relevant to the current metadata will be dropped.')
55
             ->addOption('dump-sql', null, InputOption::VALUE_NONE, 'Dumps the generated SQL statements to the screen (does not execute them).')
56
             ->addOption('force', 'f', InputOption::VALUE_NONE, 'Causes the generated SQL statements to be physically executed against your database.')
57
             ->setHelp(<<<EOT
58
The <info>%command.name%</info> command generates the SQL needed to
59
synchronize the database schema with the current mapping metadata of the
60
default entity manager.
61
62
For example, if you add metadata for a new column to an entity, this command
63
would generate and output the SQL needed to add the new column to the database:
64
65
<info>%command.name% --dump-sql</info>
66
67
Alternatively, you can execute the generated queries:
68
69
<info>%command.name% --force</info>
70
71
If both options are specified, the queries are output and then executed:
72
73
<info>%command.name% --dump-sql --force</info>
74
75
Finally, be aware that if the <info>--complete</info> option is passed, this
76
task will drop all database assets (e.g. tables, etc) that are *not* described
77
by the current metadata. In other words, without this option, this task leaves
78
untouched any "extra" tables that exist in the database, but which aren't
79
described by any metadata.
80
81
<comment>Hint:</comment> If you have a database with tables that should not be managed
82
by the ORM, you can use a DBAL functionality to filter the tables and sequences down
83
on a global level:
84
85
    \$config->setFilterSchemaAssetsExpression(\$regexp);
86
EOT
87
             );
88
    }
89
90
    /**
91
     * {@inheritdoc}
92
     */
93
    protected function executeSchemaCommand(InputInterface $input, OutputInterface $output, SchemaTool $schemaTool, array $metadatas, SymfonyStyle $ui)
94
    {
95
        // Defining if update is complete or not (--complete not defined means $saveMode = true)
96
        $saveMode = ! $input->getOption('complete');
97
98
        $sqls = $schemaTool->getUpdateSchemaSql($metadatas, $saveMode);
99
100
        if (empty($sqls)) {
101
            $ui->success('Nothing to update - your database is already in sync with the current entity metadata.');
102
103
            return 0;
104
        }
105
106
        $dumpSql = true === $input->getOption('dump-sql');
107
        $force   = true === $input->getOption('force');
108
109
        if ($dumpSql) {
110
            $ui->text('The following SQL statements will be executed:');
111
            $ui->newLine();
112
113
            foreach ($sqls as $sql) {
114
                $ui->text(sprintf('    %s;', $sql));
115
            }
116
        }
117
118
        if ($force) {
119
            if ($dumpSql) {
120
                $ui->newLine();
121
            }
122
            $ui->text('Updating database schema...');
123
            $ui->newLine();
124
125
            $schemaTool->updateSchema($metadatas, $saveMode);
126
127
            $pluralization = (1 === count($sqls)) ? 'query was' : 'queries were';
128
129
            $ui->text(sprintf('    <info>%s</info> %s executed', count($sqls), $pluralization));
130
            $ui->success('Database schema updated successfully!');
131
        }
132
133
        if ($dumpSql || $force) {
134
            return 0;
135
        }
136
137
        $ui->caution(
138
            [
139
                'This operation should not be executed in a production environment!',
140
                '',
141
                'Use the incremental update to detect changes during development and use',
142
                'the SQL DDL provided to manually update your database in production.',
143
            ]
144
        );
145
146
        $ui->text(
147
            [
148
                sprintf('The Schema-Tool would execute <info>"%s"</info> queries to update the database.', count($sqls)),
149
                '',
150
                'Please run the operation by passing one - or both - of the following options:',
151
                '',
152
                sprintf('    <info>%s --force</info> to execute the command', $this->getName()),
153
                sprintf('    <info>%s --dump-sql</info> to dump the SQL statements to the screen', $this->getName()),
154
            ]
155
        );
156
157
        return 1;
158
    }
159
}
160