Esempio n. 1
0
 /**
  * Executes command
  *
  * @param InputInterface  $input  Command input
  * @param OutputInterface $output Console output
  *
  * @throws \Exception
  *
  * @return void
  */
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     // Execute base command
     parent::execute($input, $output);
     $params = $this->getConnectionParams($input->getOption('connection'));
     $databaseName = isset($params['path']) && $params['driver'] == 'pdo_sqlite' ? $params['path'] : $params['dbname'];
     if (!$databaseName) {
         throw new \InvalidArgumentException('Connection does not contain a \'path\' or \'dbname\' ' . 'parameter and cannot be dropped.');
     }
     if ($input->getOption('force')) {
         // Only quote if we don't have a path
         if ($params['driver'] != 'pdo_sqlite') {
             $databaseName = $this->getConnection()->getDatabasePlatform()->quoteSingleIdentifier($databaseName);
         }
         try {
             $this->getConnection()->getSchemaManager()->dropDatabase($databaseName);
             $output->writeln(sprintf('<info>Dropped database <comment>%s</comment> ' . 'for connection <comment>%s</comment></info>', $databaseName, $input->getOption('connection')));
         } catch (\Exception $e) {
             $output->writeln(sprintf('<error>%s</error>', $e->getMessage()));
             $output->writeln(sprintf('<error>Could not drop database <comment>%s</comment> ' . 'for connection <comment>%s</comment></error>', $databaseName));
             throw $e;
         }
     } else {
         $output->writeln('<error>Attention:</error> This operation should not be executed ' . 'in a production environment.');
         $output->writeln('');
         $output->writeln(sprintf('<info>Would drop the database named ' . '<comment>%s</comment>.</info>', $databaseName));
         $output->writeln('Please run the operation with --force to execute');
         $output->writeln('<error>All data will be lost!</error>');
     }
     $this->getConnection()->close();
 }
 /**
  * Executes command
  *
  * @param InputInterface  $input  Command input
  * @param OutputInterface $output Console output
  *
  * @throws \Exception
  *
  * @return void
  */
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     parent::execute($input, $output);
     $params = $this->getConnectionParams($input->getOption('connection'));
     $databaseName = isset($params['path']) && $params['driver'] == 'pdo_sqlite' ? $params['path'] : $params['dbname'];
     // Only quote if we don't have a path
     if ($params['driver'] != 'pdo_sqlite') {
         $databaseName = $this->getConnection()->getDatabasePlatform()->quoteSingleIdentifier($databaseName);
     }
     try {
         $this->getConnection()->getSchemaManager()->createDatabase($databaseName);
         $output->writeln(sprintf('<info>Created database <comment>%s</comment> for connection ' . '<comment>%s</comment></info>', $databaseName, $input->getOption('connection')));
     } catch (\Exception $e) {
         $output->writeln(sprintf('<error>%s</error>', $e->getMessage()));
         $output->writeln(sprintf('<error>Could not create database <comment>%s</comment> ' . 'for connection <comment>%s</comment></error>', $databaseName, $input->getOption('connection')));
         throw $e;
     }
     $this->getConnection()->close();
 }
Esempio n. 3
0
 /**
  * Executes command
  *
  * @param InputInterface  $input  Command input
  * @param OutputInterface $output Console output
  *
  * @throws \Exception
  *
  * @return void
  */
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     parent::execute($input, $output);
     if ($this->getContainer()->offsetExists('dbs')) {
         $this->setConnection($this->getContainer()['dbs'][$input->getOption('connection')]);
     }
     $schema = new ConfigurableSchema();
     $config = $this->getContainer()['config']['dbal_schema'][$input->getOption('connection')];
     if (isset($config['tables'])) {
         $schema->createTablesFromConfig($config['tables']);
     }
     $schema->bundleMultipleSchemas($config);
     $sql = $schema->toSql($this->getConnection()->getDatabasePlatform());
     foreach ((array) $sql as $query) {
         $output->write(sprintf('Executing <info>%s</info>', $query));
         $this->getConnection()->executeUpdate($query);
         $output->write(' OK' . "\n");
     }
     // Insert fake data to database
     if ($input->getOption('fakedata')) {
         $this->_insertFakeData($schema, $output);
     }
 }