gpilotino
gpilotino

Reputation: 13302

Doctrine ORM: drop all tables without dropping database

I have to work with an existing database (not managed with Doctrine) and I want to use doctrine only for new tables in this db.

is there a way to tell Doctrine to not drop the entire DB on reload but only the models defined in the yaml file ?

Upvotes: 24

Views: 51061

Answers (8)

qwertz
qwertz

Reputation: 2289

To run the command bin/console doctrine:schema:drop --force from inside another command, try this:

$command = $this->getApplication()->find('doctrine:schema:drop');
$returnCode = $command->run(new ArrayInput(['--force' => true]), $output);

Upvotes: 0

Sergio Barbosa
Sergio Barbosa

Reputation: 459

I this Symfony command to drop all tables:

bin/console doctrine:schema:drop --full-database --force

I added flag --force after get a caution message.

Upvotes: 14

rkeet
rkeet

Reputation: 3468

Old question, but adding for a future soul.

Based on Meezaan-ud-Din's answer quickly found it for Zend Framework 3 + Doctrine 2

./vendor/bin/doctrine-module orm:schema-tool:drop --full-database -f --dump-sql

Do NOT use in production

  • orm:schema-tool:drop to "drop" database
  • --full-database to wipe out everything in database which is managed by Doctrine!
  • To execute, you must use --force (or -f)
  • --dump-sql to show the SQL being executed. May be combined with the -f flag.

Complete code for execution found in class: \Doctrine\ORM\Tools\Console\Command\SchemaTool\DropCommand

Upvotes: 4

Gebus
Gebus

Reputation: 281

For Symfony 3:

$entityManager = $container->get('doctrine.orm.entity_manager'); 

$entityManager->getConnection()->getConfiguration()->setSQLLogger(null);

$entityManager->getConnection()->prepare("SET FOREIGN_KEY_CHECKS = 0;")->execute();

foreach ($entityManager->getConnection()->getSchemaManager()->listTableNames() as $tableNames) {
        $sql = 'DROP TABLE ' . $tableNames;
        $entityManager->getConnection()->prepare($sql)->execute();
}
$entityManager->getConnection()->prepare("SET FOREIGN_KEY_CHECKS = 1;")->execute();

Upvotes: 7

Thomas Decaux
Thomas Decaux

Reputation: 22661

My 2 cents, if you want do this via Php:

    $em = ....getContainer()->get('doctrine')->getManager();

    $metaData = $em->getMetadataFactory()->getAllMetadata();

    $tool = new \Doctrine\ORM\Tools\SchemaTool($em);
    $tool->dropSchema($metaData);
    $tool->createSchema($metaData);

This is a bit slow, you can also delete all data from all tables:

$em = getContainer()->get('doctrine.dbal.default_connection');

foreach($em->getSchemaManager()->listTableNames() as $tableName)
{
  $em->exec('DELETE FROM ' . $tableName);
}

Upvotes: 5

Meezaan-ud-Din
Meezaan-ud-Din

Reputation: 1253

I know this a very old question, and it appears you are using symfony.

Try:

app/console --env=prod doctrine:schema:drop --full-database

This will drop all the tables in the DB.

Upvotes: 49

user327312
user327312

Reputation: 9

@ gpilotino Yes, I'm having a similar problem. There seems to be NO WAY to drop and rebuild the database from within PHPUnit, (The future of Symfony testing).

Maybe it's possible in 'lime',I don't know.

So, I am having to write a reverse ->save() function that backs all the data out of the database, and then resets all the sequences so that I can do automated testing.

For those who don't want to follow in my frustration I tried both:

1) using a Task from inside of symfony:

  $optionsArray=array();
  $argumentsArray=array();

  $optionsArray[]="--all";
  $optionsArray[]="--and-load";
  $optionsArray[]="--no-confirmation";

  $task = new sfDoctrineBuildTask($configuration->getEventDispatcher(), new sfFormatter());
  $task->run($argumentsArray, $optionsArray);

2)Executing it from outside of symfony while inside of PHP:

  Doctrine_Manager::getInstance()->getCurrentConnection()->close();
  exec('./symfony doctrine:build --all --and-load --no-confirmation');

The reason that I closed the connection is that Postgres, MDBOC (my db of choice) will not drop a database that has a connection. Probably is STILL some kind of problem. I tell ya, it's NEVER as easy as the simple tutorials show. And it's even WORSE with microslop products.

Upvotes: 0

Flevour
Flevour

Reputation: 139

You can find a task I use to truncate all tables from a db at this gist: https://gist.github.com/1154458

The core code is:

        $this->dbh = $connection->getDbh();
        $this->dbh->query(sprintf('SET FOREIGN_KEY_CHECKS = 0;'));
        $tables = $connection->import->listTables();
        foreach ($tables as $table)
        {
          $this->dbh->query(sprintf('TRUNCATE TABLE %s', $tableName));
        }
        $this->dbh->query(sprintf('SET FOREIGN_KEY_CHECKS = 1;'));

Upvotes: 1

Related Questions