+
+ /**
+ * Run a command.
+ *
+ * @param string $command
+ * @param array $parameters Parameters to this command (usually 'force' => true)
+ */
+ protected function runCommand($command, $parameters = array())
+ {
+ $parameters = array_merge(
+ array('command' => $command),
+ $parameters,
+ array(
+ '--no-debug' => true,
+ '--env' => $this->defaultInput->getOption('env') ?: 'dev',
+ )
+ );
+
+ if ($this->defaultInput->getOption('no-interaction')) {
+ $parameters = array_merge($parameters, array('--no-interaction' => true));
+ }
+
+ $this->getApplication()->setAutoExit(false);
+ $exitCode = $this->getApplication()->run(new ArrayInput($parameters), new NullOutput());
+
+ if (0 !== $exitCode) {
+ $this->getApplication()->setAutoExit(true);
+
+ $errorMessage = sprintf('The command "%s" terminated with an error code: %u.', $command, $exitCode);
+ $this->defaultOutput->writeln("<error>$errorMessage</error>");
+ $exception = new \Exception($errorMessage, $exitCode);
+
+ throw $exception;
+ }
+
+ // PDO does not always close the connection after Doctrine commands.
+ // See https://github.com/symfony/symfony/issues/11750.
+ $this->getContainer()->get('doctrine')->getManager()->getConnection()->close();
+
+ return $this;
+ }
+
+ /**
+ * Check if the database already exists.
+ *
+ * @return bool
+ */
+ private function isDatabasePresent()
+ {
+ $connection = $this->getContainer()->get('doctrine')->getManager()->getConnection();
+ $databaseName = $connection->getDatabase();
+
+ try {
+ $schemaManager = $connection->getSchemaManager();
+ } catch (\Exception $exception) {
+ // mysql & sqlite
+ if (false !== strpos($exception->getMessage(), sprintf("Unknown database '%s'", $databaseName))) {
+ return false;
+ }
+
+ // pgsql
+ if (false !== strpos($exception->getMessage(), sprintf('database "%s" does not exist', $databaseName))) {
+ return false;
+ }
+
+ throw $exception;
+ }
+
+ // custom verification for sqlite, since `getListDatabasesSQL` doesn't work for sqlite
+ if ('sqlite' == $schemaManager->getDatabasePlatform()->getName()) {
+ $params = $this->getContainer()->get('doctrine.dbal.default_connection')->getParams();
+
+ if (isset($params['path']) && file_exists($params['path'])) {
+ return true;
+ }
+
+ return false;
+ }
+
+ return in_array($databaseName, $schemaManager->listDatabases());
+ }
+
+ /**
+ * Check if the schema is already created.
+ * If we found at least oen table, it means the schema exists.
+ *
+ * @return bool
+ */
+ private function isSchemaPresent()
+ {
+ $schemaManager = $this->getContainer()->get('doctrine')->getManager()->getConnection()->getSchemaManager();
+
+ return count($schemaManager->listTableNames()) > 0 ? true : false;
+ }