Command/UpdateSchemaDoctrineCommand.php
changeset 42 0e57c730bb18
parent 25 11fd79666374
equal deleted inserted replaced
39:b403086580f7 42:0e57c730bb18
    19 use Symfony\Component\Console\Output\OutputInterface;
    19 use Symfony\Component\Console\Output\OutputInterface;
    20 use Symfony\Component\Console\Output\Output;
    20 use Symfony\Component\Console\Output\Output;
    21 use Doctrine\ORM\Tools\Console\Command\SchemaTool\UpdateCommand;
    21 use Doctrine\ORM\Tools\Console\Command\SchemaTool\UpdateCommand;
    22 use Doctrine\ORM\Tools\SchemaTool;
    22 use Doctrine\ORM\Tools\SchemaTool;
    23 use Symfony\Bundle\DoctrineBundle\Command\Proxy\DoctrineCommandHelper;
    23 use Symfony\Bundle\DoctrineBundle\Command\Proxy\DoctrineCommandHelper;
       
    24 use IRI\Bundle\WikiTagBundle\Utils\FilteredSchemaTool;
    24 
    25 
    25 
    26 
    26 /**
    27 /**
    27  * Command to generate the SQL needed to update the database schema to match
    28  * Command to generate the SQL needed to update the database schema to match
    28  * the current mapping information.
    29  * the current mapping information.
    88     public function setContainer(ContainerInterface $container = null)
    89     public function setContainer(ContainerInterface $container = null)
    89     {
    90     {
    90         $this->container = $container;
    91         $this->container = $container;
    91     }
    92     }
    92     
    93     
    93     
    94         
    94     protected function filterUpdateSchema($sqls)
       
    95     {
       
    96     
       
    97         // get service
       
    98         $schema_utils = $this->getContainer()->get("wikitag.shema_utils");
       
    99     
       
   100         $res_sqls = $schema_utils->filter_foreign_key($sqls);
       
   101         $res_sqls = $schema_utils->filter_index_creation($res_sqls);
       
   102     
       
   103     
       
   104         return $res_sqls;
       
   105     }
       
   106     
       
   107     protected function executeSchemaCommand(InputInterface $input, OutputInterface $output, SchemaTool $schemaTool, array $metadatas)
    95     protected function executeSchemaCommand(InputInterface $input, OutputInterface $output, SchemaTool $schemaTool, array $metadatas)
   108     {
    96     {
   109         // Defining if update is complete or not (--complete not defined means $saveMode = true)
    97         $filteredSchemaTool = new FilteredSchemaTool($this->getHelper("em")->getEntityManager(), $this->getContainer());
   110         $saveMode = ($input->getOption('complete') !== true);
    98         
   111     
    99         return parent::executeSchemaCommand($input, $output, $filteredSchemaTool, $metadatas);
   112         $sqls = $schemaTool->getUpdateSchemaSql($metadatas, $saveMode);
   100         
   113         $sqls = $this->filterUpdateSchema($sqls);
       
   114         if (0 == count($sqls)) {
       
   115             $output->writeln('Nothing to update - your database is already in sync with the current entity metadata.');
       
   116     
       
   117             return;
       
   118         }
       
   119     
       
   120         $dumpSql = (true === $input->getOption('dump-sql'));
       
   121         $force = (true === $input->getOption('force'));
       
   122         if ($dumpSql && $force) {
       
   123             throw new \InvalidArgumentException('You can pass either the --dump-sql or the --force option (but not both simultaneously).');
       
   124         }
       
   125     
       
   126         if ($dumpSql) {
       
   127             $output->writeln(implode(';' . PHP_EOL, $sqls));
       
   128         } else if ($force) {
       
   129             $output->writeln('Updating database schema...');
       
   130             
       
   131             $emHelper = $this->getHelper('em');
       
   132             
       
   133             $conn = $emHelper->getEntityManager()->getConnection();
       
   134             
       
   135             foreach ($sqls as $sql) {
       
   136                 $conn->executeQuery($sql);
       
   137             }
       
   138             $output->writeln(sprintf('Database schema updated successfully! "<info>%s</info>" queries were executed', count($sqls)));
       
   139         } else {
       
   140             $output->writeln('<comment>ATTENTION</comment>: This operation should not be executed in a production environment.');
       
   141             $output->writeln('           Use the incremental update to detect changes during development and use');
       
   142             $output->writeln('           the SQL DDL provided to manually update your database in production.');
       
   143             $output->writeln('');
       
   144     
       
   145             $output->writeln(sprintf('The Schema-Tool would execute <info>"%s"</info> queries to update the database.', count($sqls)));
       
   146             $output->writeln('Please run the operation by passing one of the following options:');
       
   147     
       
   148             $output->writeln(sprintf('    <info>%s --force</info> to execute the command', $this->getName()));
       
   149             $output->writeln(sprintf('    <info>%s --dump-sql</info> to dump the SQL statements to the screen', $this->getName()));
       
   150         }
       
   151     }
   101     }
   152     
   102     
   153     
   103     
   154 }
   104 }