示例#1
0
 /**
  * {@inheritdoc}
  */
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $connection = Propel::getConnection($input->getOption('connection'));
     $adapter = Propel::getAdapter($connection->getName());
     if (!$adapter instanceof MysqlAdapter) {
         return $output->writeln('<error>This command is MySQL only.</error>');
     }
     if (!$input->getOption('force')) {
         return $output->writeln('<error>You have to use the "--force" option to drop some tables.</error>');
     }
     $tablesToDelete = $input->getArgument('table');
     $nbTable = count($tablesToDelete);
     $tablePlural = $nbTable > 1 || $nbTable == 0 ? 's' : '';
     if ('prod' === $this->getApplication()->getKernel()->getEnvironment()) {
         $count = $nbTable ?: 'all';
         $this->writeSection($output, 'WARNING: you are about to drop ' . $count . ' table' . $tablePlural . ' in production !', 'bg=red;fg=white');
         if (false === $this->askConfirmation($input, $output, 'Are you sure ? (y/n) ', false)) {
             $output->writeln('<info>Aborted, nice decision !</info>');
             return -2;
         }
     }
     $showStatement = $connection->prepare('SHOW TABLES;');
     $showStatement->execute();
     $allTables = $showStatement->fetchAll(\PDO::FETCH_COLUMN);
     if ($nbTable) {
         foreach ($tablesToDelete as $tableToDelete) {
             if (!array_search($tableToDelete, $allTables)) {
                 throw new \InvalidArgumentException(sprintf('Table %s doesn\'t exist in the database.', $tableToDelete));
             }
         }
     } else {
         $tablesToDelete = $allTables;
     }
     $connection->exec('SET FOREIGN_KEY_CHECKS = 0;');
     array_walk($tablesToDelete, function (&$table, $key, $dbAdapter) {
         $table = $dbAdapter->quoteIdentifierTable($table);
     }, $adapter);
     $tablesToDelete = join(', ', $tablesToDelete);
     if ('' !== $tablesToDelete) {
         $connection->exec('DROP TABLE ' . $tablesToDelete . ' ;');
         $output->writeln(sprintf('Table' . $tablePlural . ' <info><comment>%s</comment> has been dropped.</info>', $tablesToDelete));
     } else {
         $output->writeln('<info>No tables have been dropped</info>');
     }
     $connection->exec('SET FOREIGN_KEY_CHECKS = 1;');
 }
 public function testNumberOfQueriesForMakeUniqSlug()
 {
     Table13Query::create()->deleteAll();
     $con = Propel::getServiceContainer()->getConnection(Table13TableMap::DATABASE_NAME);
     $adapter = Propel::getAdapter(Table13TableMap::DATABASE_NAME);
     $expectedCount = 4;
     if ($adapter instanceof PgsqlAdapter) {
         $expectedCount++;
         //because of the SELECT nextval(...) query
     }
     for ($i = 0; $i < 5; $i++) {
         $nbQuery = $con->getQueryCount();
         $t = new Table13();
         $t->setTitle('Hello, World');
         $t->save($con);
         $this->assertLessThanOrEqual($expectedCount, $con->getQueryCount() - $nbQuery, "no more than {$expectedCount} query to get a slug when it already exist");
     }
 }