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

DropCommand::configure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 15
nc 1
nop 0
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
ccs 0
cts 7
cp 0
crap 2
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 drop the database schema for a set of classes based on their mappings.
30
 *
31
 * @link    www.doctrine-project.org
32
 * @since   2.0
33
 * @author  Benjamin Eberlei <[email protected]>
34
 * @author  Guilherme Blanco <[email protected]>
35
 * @author  Jonathan Wage <[email protected]>
36
 * @author  Roman Borschel <[email protected]>
37
 */
38
class DropCommand extends AbstractCommand
39
{
40
    /**
41
     * {@inheritdoc}
42
     */
43
    protected function configure()
44
    {
45
        $this->setName('orm:schema-tool:drop')
46
             ->setDescription('Drop the complete database schema of EntityManager Storage Connection or generate the corresponding SQL output')
47
             ->addOption('dump-sql', null, InputOption::VALUE_NONE, 'Instead of trying to apply generated SQLs into EntityManager Storage Connection, output them.')
48
             ->addOption('force', 'f', InputOption::VALUE_NONE, "Don't ask for the deletion of the database, but force the operation to run.")
49
             ->addOption('full-database', null, InputOption::VALUE_NONE, 'Instead of using the Class Metadata to detect the database table schema, drop ALL assets that the database contains.')
50
             ->setHelp(<<<EOT
51
Processes the schema and either drop the database schema of EntityManager Storage Connection or generate the SQL output.
52
Beware that the complete database is dropped by this command, even tables that are not relevant to your metadata model.
53
54
<comment>Hint:</comment> If you have a database with tables that should not be managed
55
by the ORM, you can use a DBAL functionality to filter the tables and sequences down
56
on a global level:
57
58
    \$config->setFilterSchemaAssetsExpression(\$regexp);
59
EOT
60
             );
61
    }
62
63
    /**
64
     * {@inheritdoc}
65
     */
66
    protected function executeSchemaCommand(InputInterface $input, OutputInterface $output, SchemaTool $schemaTool, array $metadatas, SymfonyStyle $ui)
67
    {
68
        $isFullDatabaseDrop = $input->getOption('full-database');
69
        $dumpSql = true === $input->getOption('dump-sql');
70
        $force   = true === $input->getOption('force');
71
72
        if ($dumpSql) {
73
            if ($isFullDatabaseDrop) {
74
                $sqls = $schemaTool->getDropDatabaseSQL();
75
            } else {
76
                $sqls = $schemaTool->getDropSchemaSQL($metadatas);
77
            }
78
            $ui->text('The following SQL statements will be executed:');
79
            $ui->newLine();
80
81
            foreach ($sqls as $sql) {
82
                $ui->text(sprintf('    %s;', $sql));
83
            }
84
85
            return 0;
86
        }
87
88
        if ($force) {
89
            $ui->text('Dropping database schema...');
90
            $ui->newLine();
91
92
            if ($isFullDatabaseDrop) {
93
                $schemaTool->dropDatabase();
94
            } else {
95
                $schemaTool->dropSchema($metadatas);
96
            }
97
98
            $ui->success('Database schema dropped successfully!');
99
100
            return 0;
101
        }
102
103
        $ui->caution('This operation should not be executed in a production environment!');
104
105
        if ($isFullDatabaseDrop) {
106
            $sqls = $schemaTool->getDropDatabaseSQL();
107
        } else {
108
            $sqls = $schemaTool->getDropSchemaSQL($metadatas);
109
        }
110
111
        if (empty($sqls)) {
112
            $ui->success('Nothing to drop. The database is empty!');
113
114
            return 0;
115
        }
116
117
        $ui->text(
118
            [
119
                sprintf('The Schema-Tool would execute <info>"%s"</info> queries to update the database.', count($sqls)),
120
                '',
121
                'Please run the operation by passing one - or both - of the following options:',
122
                '',
123
                sprintf('    <info>%s --force</info> to execute the command', $this->getName()),
124
                sprintf('    <info>%s --dump-sql</info> to dump the SQL statements to the screen', $this->getName()),
125
            ]
126
        );
127
128
        return 1;
129
    }
130
}
131