Rajout de doctrine/orm
[zf2.biz/galerie.git] / vendor / doctrine / orm / lib / Doctrine / ORM / Tools / Console / Command / SchemaTool / UpdateCommand.php
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 Symfony\Component\Console\Input\InputArgument,
23     Symfony\Component\Console\Input\InputOption,
24     Symfony\Component\Console\Input\InputInterface,
25     Symfony\Component\Console\Output\OutputInterface,
26     Doctrine\ORM\Tools\SchemaTool;
27
28 /**
29  * Command to generate the SQL needed to update the database schema to match
30  * the current mapping information.
31  *
32  *
33  * @link    www.doctrine-project.org
34  * @since   2.0
35  * @author  Benjamin Eberlei <kontakt@beberlei.de>
36  * @author  Guilherme Blanco <guilhermeblanco@hotmail.com>
37  * @author  Jonathan Wage <jonwage@gmail.com>
38  * @author  Roman Borschel <roman@code-factory.org>
39  * @author  Ryan Weaver <ryan@thatsquality.com>
40  */
41 class UpdateCommand extends AbstractCommand
42 {
43     protected $name = 'orm:schema-tool:update';
44
45     /**
46      * @see Console\Command\Command
47      */
48     protected function configure()
49     {
50         $this
51         ->setName($this->name)
52         ->setDescription(
53             'Executes (or dumps) the SQL needed to update the database schema to match the current mapping metadata.'
54         )
55         ->setDefinition(array(
56             new InputOption(
57                 'complete', null, InputOption::VALUE_NONE,
58                 'If defined, all assets of the database which are not relevant to the current metadata will be dropped.'
59             ),
60
61             new InputOption(
62                 'dump-sql', null, InputOption::VALUE_NONE,
63                 'Dumps the generated SQL statements to the screen (does not execute them).'
64             ),
65             new InputOption(
66                 'force', null, InputOption::VALUE_NONE,
67                 'Causes the generated SQL statements to be physically executed against your database.'
68             ),
69         ));
70
71         $this->setHelp(<<<EOT
72 The <info>%command.name%</info> command generates the SQL needed to
73 synchronize the database schema with the current mapping metadata of the
74 default entity manager.
75
76 For example, if you add metadata for a new column to an entity, this command
77 would generate and output the SQL needed to add the new column to the database:
78
79 <info>%command.name% --dump-sql</info>
80
81 Alternatively, you can execute the generated queries:
82
83 <info>%command.name% --force</info>
84
85 Finally, be aware that if the <info>--complete</info> option is passed, this
86 task will drop all database assets (e.g. tables, etc) that are *not* described
87 by the current metadata. In other words, without this option, this task leaves
88 untouched any "extra" tables that exist in the database, but which aren't
89 described by any metadata.
90 EOT
91         );
92     }
93
94     protected function executeSchemaCommand(InputInterface $input, OutputInterface $output, SchemaTool $schemaTool, array $metadatas)
95     {
96         // Defining if update is complete or not (--complete not defined means $saveMode = true)
97         $saveMode = ($input->getOption('complete') !== true);
98
99         $sqls = $schemaTool->getUpdateSchemaSql($metadatas, $saveMode);
100         if (0 == count($sqls)) {
101             $output->writeln('Nothing to update - your database is already in sync with the current entity metadata.');
102
103             return;
104         }
105
106         $dumpSql = (true === $input->getOption('dump-sql'));
107         $force = (true === $input->getOption('force'));
108         if ($dumpSql && $force) {
109             throw new \InvalidArgumentException('You can pass either the --dump-sql or the --force option (but not both simultaneously).');
110         }
111
112         if ($dumpSql) {
113             $output->writeln(implode(';' . PHP_EOL, $sqls));
114         } else if ($force) {
115             $output->writeln('Updating database schema...');
116             $schemaTool->updateSchema($metadatas, $saveMode);
117             $output->writeln(sprintf('Database schema updated successfully! "<info>%s</info>" queries were executed', count($sqls)));
118         } else {
119             $output->writeln('<comment>ATTENTION</comment>: This operation should not be executed in a production environment.');
120             $output->writeln('           Use the incremental update to detect changes during development and use');
121             $output->writeln('           the SQL DDL provided to manually update your database in production.');
122             $output->writeln('');
123
124             $output->writeln(sprintf('The Schema-Tool would execute <info>"%s"</info> queries to update the database.', count($sqls)));
125             $output->writeln('Please run the operation by passing one of the following options:');
126
127             $output->writeln(sprintf('    <info>%s --force</info> to execute the command', $this->getName()));
128             $output->writeln(sprintf('    <info>%s --dump-sql</info> to dump the SQL statements to the screen', $this->getName()));
129         }
130     }
131 }