Saturday, November 10, 2012

Symfony 2: Drop, Create, and Update Database

The Problem

This is regarding symfony 2.1 CLI tasks. When trying to add a task that dropped, created, and updated my database, I received the following:


[Doctrine\DBAL\DBALException]                                                              
An exception occurred while executing 'SHOW FULL TABLES WHERE Table_type = 'BASE TABLE'':  
                                                                                           
SQLSTATE[3D000]: Invalid catalog name: 1046 No database selected                           
                                                                                                                                                                 
[PDOException]                                                    
SQLSTATE[3D000]: Invalid catalog name: 1046 No database selected

The problem, it turns out, was that doctrine was using the same connection that it used to drop the database as it was to update the database. After the drop, doctrine lost it's awareness to your database and did not re-establish it upon create.

The Fix

You need to close the connection after calling doctrine:database:drop and before doctrine:database:update --force

$connection = $this->getApplication()->getKernel()->getContainer()->get('doctrine')->getConnection();

  if ($connection->isConnected()) {
   $connection->close();
  }


And just for reference:

Drop, Create, and Update database in Symfony 2 CLI Task



  $drop = $this->getApplication()->find('doctrine:database:drop');
  $drop_args = array(
   'command'=>'doctrine:database:drop',
   '--force'=>true
   );

  $drop->run(new ArrayInput($drop_args), $output);

  //Make sure we close the original connection because it lost the reference to the database
  $connection = $this->getApplication()->getKernel()->getContainer()->get('doctrine')->getConnection();

  if ($connection->isConnected()) {
   $connection->close();
  }

  $create = $this->getApplication()->find('doctrine:database:create');
  $created_args = array(
   'command'=>'doctrine:database:create'
   );

  $create->run(new ArrayInput($created_args), $output);

  $update = $this->getApplication()->find('doctrine:schema:update');

  $update_args = array(
   'command'=>'doctrine:schema:update',
   '--force'=>true,
   '--em'=>'default'
   );

  $update->run(new ArrayInput($update_args), $output);

7 comments:

  1. Hi I have same problem but i don't know witch files to make changes.

    ReplyDelete
  2. Thanks! Helped me also to find the bug!

    ReplyDelete
  3. Thanks, saved me a lot of time. Gonna send a PR to DoctrineBundle DropDatabaseDoctrineCommand with your fix )

    ReplyDelete
  4. Thanks, your my Hero of the day ;-)

    ReplyDelete
  5. SQLite has many built-in protections which work against database corruption. But sometimes, many of these protections are disabled due to configuration options.kpi dashboards

    ReplyDelete
  6. In a record-oriented system, one record is created for each traveler in the system. In a relational database, a traveler's name would appear on several related lists. oracle dashboards

    ReplyDelete