protected function executeSchemaCommand(InputInterface $input, OutputInterface $output, SchemaTool $schemaTool, array $metadatas) { $isFullDatabaseDrop = $input->getOption('full-database'); if ($input->getOption('dump-sql') === true) { if ($isFullDatabaseDrop) { $sqls = $schemaTool->getDropDatabaseSQL(); } else { $sqls = $schemaTool->getDropSchemaSQL($metadatas); } $output->write(implode(';' . PHP_EOL, $sqls) . PHP_EOL); } else { if ($input->getOption('force') === true) { $output->write('Dropping database schema...' . PHP_EOL); if ($isFullDatabaseDrop) { $schemaTool->dropDatabase(); } else { $schemaTool->dropSchema($metadatas); } $output->write('Database schema dropped successfully!' . PHP_EOL); } else { $output->write('ATTENTION: This operation should not be executed in a production environment.' . PHP_EOL . PHP_EOL); if ($isFullDatabaseDrop) { $sqls = $schemaTool->getDropDatabaseSQL(); } else { $sqls = $schemaTool->getDropSchemaSQL($metadatas); } if (count($sqls)) { $output->write('Schema-Tool would execute ' . count($sqls) . ' queries to drop the database.' . PHP_EOL); $output->write('Please run the operation with --force to execute these queries or use --dump-sql to see them.' . PHP_EOL); } else { $output->write('Nothing to drop. The database is empty!' . PHP_EOL); } } } }
/** * Execute the console command. * * @param ManagerRegistry $registry */ public function fire(ManagerRegistry $registry) { $this->error('ATTENTION: This operation should not be executed in a production environment.'); $names = $this->option('em') ? [$this->option('em')] : $registry->getManagerNames(); foreach ($names as $name) { $em = $registry->getManager($name); $tool = new SchemaTool($em); $this->info(''); $this->message('Checking scheme for <info>' . $name . '</info> entity manager...'); if ($this->option('sql')) { if ($this->option('full')) { $sql = $tool->getDropDatabaseSQL(); } else { $sql = $tool->getDropSchemaSQL($em->getMetadataFactory()->getAllMetadata()); } $this->comment(' ' . implode('; ' . PHP_EOL, $sql)); } else { if ($this->option('force')) { $this->message('Dropping database schema...'); if ($this->option('full')) { $tool->dropDatabase(); } else { $tool->dropSchema($em->getMetadataFactory()->getAllMetadata()); } $this->info('Database schema dropped successfully!'); } if ($this->option('full')) { $sql = $tool->getDropDatabaseSQL(); } else { $sql = $tool->getDropSchemaSQL($em->getMetadataFactory()->getAllMetadata()); } if (count($sql)) { $pluralization = 1 === count($sql) ? 'query was' : 'queries were'; $this->message(sprintf('The Schema-Tool would execute <info>"%s"</info> %s to update the database.', count($sql), $pluralization)); $this->message('Please run the operation by passing one - or both - of the following options:'); $this->comment(sprintf(' <info>php artisan %s --force</info> to execute the command', $this->getName())); $this->comment(sprintf(' <info>php artisan %s --sql</info> to dump the SQL statements to the screen', $this->getName())); } else { $this->error('Nothing to drop. The database is empty!'); } } } }
/** * {@inheritdoc} */ protected function executeSchemaCommand(InputInterface $input, OutputInterface $output, SchemaTool $schemaTool, array $metadatas) { $isFullDatabaseDrop = $input->getOption('full-database'); if ($input->getOption('dump-sql')) { if ($isFullDatabaseDrop) { $sqls = $schemaTool->getDropDatabaseSQL(); } else { $sqls = $schemaTool->getDropSchemaSQL($metadatas); } $output->writeln(implode(';' . PHP_EOL, $sqls)); return 0; } if ($input->getOption('force')) { $output->writeln('Dropping database schema...'); if ($isFullDatabaseDrop) { $schemaTool->dropDatabase(); } else { $schemaTool->dropSchema($metadatas); } $output->writeln('Database schema dropped successfully!'); return 0; } $output->writeln('<comment>ATTENTION</comment>: This operation should not be executed in a production environment.' . PHP_EOL); if ($isFullDatabaseDrop) { $sqls = $schemaTool->getDropDatabaseSQL(); } else { $sqls = $schemaTool->getDropSchemaSQL($metadatas); } if (count($sqls)) { $output->writeln(sprintf('The Schema-Tool would execute <info>"%s"</info> queries to update the database.', count($sqls))); $output->writeln('Please run the operation by passing one - or both - of the following options:'); $output->writeln(sprintf(' <info>%s --force</info> to execute the command', $this->getName())); $output->writeln(sprintf(' <info>%s --dump-sql</info> to dump the SQL statements to the screen', $this->getName())); return 1; } $output->writeln('Nothing to drop. The database is empty!'); return 0; }
/** * {@inheritdoc} */ protected function execute(InputInterface $input, OutputInterface $output) { $force = $input->getOption('force'); $drop = $input->getOption('drop'); $this->bbapp = $this->getContainer()->get('bbapp'); $this->em = $this->initConnection($input, $output); $this->checkBeforeUpdate(); $this->em->getConfiguration()->getMetadataDriverImpl()->addPaths([$this->bbapp->getBBDir() . DIRECTORY_SEPARATOR . 'Bundle', $this->bbapp->getBBDir() . DIRECTORY_SEPARATOR . 'Cache' . DIRECTORY_SEPARATOR . 'DAO', $this->bbapp->getBBDir() . DIRECTORY_SEPARATOR . 'ClassContent', $this->bbapp->getBBDir() . DIRECTORY_SEPARATOR . 'ClassContent' . DIRECTORY_SEPARATOR . 'Indexes', $this->bbapp->getBBDir() . DIRECTORY_SEPARATOR . 'Logging', $this->bbapp->getBBDir() . DIRECTORY_SEPARATOR . 'NestedNode', $this->bbapp->getBBDir() . DIRECTORY_SEPARATOR . 'Security', $this->bbapp->getBBDir() . DIRECTORY_SEPARATOR . 'Site', $this->bbapp->getBBDir() . DIRECTORY_SEPARATOR . 'Stream' . DIRECTORY_SEPARATOR . 'ClassWrapper', $this->bbapp->getBBDir() . DIRECTORY_SEPARATOR . 'Util' . DIRECTORY_SEPARATOR . 'Sequence' . DIRECTORY_SEPARATOR . 'Entity', $this->bbapp->getBBDir() . DIRECTORY_SEPARATOR . 'Workflow']); $this->em->getConfiguration()->getMetadataDriverImpl()->addExcludePaths([$this->bbapp->getBBDir() . DIRECTORY_SEPARATOR . 'ClassContent' . DIRECTORY_SEPARATOR . 'Tests', $this->bbapp->getBBDir() . DIRECTORY_SEPARATOR . 'NestedNode' . DIRECTORY_SEPARATOR . 'Tests', $this->bbapp->getBBDir() . DIRECTORY_SEPARATOR . 'Security' . DIRECTORY_SEPARATOR . 'Tests', $this->bbapp->getBBDir() . DIRECTORY_SEPARATOR . 'Util' . DIRECTORY_SEPARATOR . 'Tests', $this->bbapp->getBBDir() . DIRECTORY_SEPARATOR . 'Workflow' . DIRECTORY_SEPARATOR . 'Tests']); if (is_dir($this->bbapp->getBBDir() . DIRECTORY_SEPARATOR . 'vendor')) { $this->em->getConfiguration()->getMetadataDriverImpl()->addExcludePaths([$this->bbapp->getBBDir() . DIRECTORY_SEPARATOR . 'vendor']); } $sqls = $this->getUpdateQueries(); if ($force || $drop) { $metadata = $this->em->getMetadataFactory()->getAllMetadata(); $schema = new SchemaTool($this->em); if ($drop) { $sqls = array_merge($schema->getDropDatabaseSQL(), $sqls); } if ($force) { $output->writeln('<info>Running drop/update</info>'); $this->em->getConnection()->executeUpdate('SET FOREIGN_KEY_CHECKS=0'); $drop ? $schema->dropDatabase() : ''; $schema->updateSchema($metadata, true); $this->em->getConnection()->executeUpdate('SET FOREIGN_KEY_CHECKS=1'); } } $output->writeln(($force ? '<info>SQL executed: </info>' : '<info>SQL to be executed: </info>') . PHP_EOL . implode(";" . PHP_EOL, $sqls) . ''); }