Code snippets for symfony 1.x


Refine Tags

Snippets tagged "droptables"

doctrine:drop-db task replacement for restricted servers/hosts

Instead of dropping the database all tables in it are dropped.

 * Replacement for drop-db where one has no rights to drop a db.
 * @package    symfony
 * @subpackage task
 * @author     Maik Riechert <>
class sfDoctrineDropTablesTask extends sfDoctrineBaseTask
     * @see sfTask
    protected function configure()
        new sfCommandOption('application', null, sfCommandOption::PARAMETER_OPTIONAL, 'The application name', true),
        new sfCommandOption('env', null, sfCommandOption::PARAMETER_REQUIRED, 'The environment', 'dev'),
        new sfCommandOption('no-confirmation', null, sfCommandOption::PARAMETER_NONE, 'Whether to force dropping the tables of the database')
        $this->aliases = array('doctrine-drop-tables');
        $this->namespace = 'doctrine';
        $this->name = 'drop-tables';
        $this->briefDescription = 'Drops tables of the database for current model';
        $this->detailedDescription = <<<EOF
The [doctrine:drop-tables|INFO] task drops the tables of the database:
  [./symfony doctrine:drop-tables|INFO]
The task read connection information in [config/doctrine/databases.yml|COMMENT]:
    protected function execute($arguments = array(), $options = array())
        if (
        !$this->askConfirmation(array('This command will remove all data in your database.', 'Are you sure you want to proceed? (y/N)'), null, false)
            $this->logSection('doctrine', 'task aborted');
            return 1;
        $this->logSection('doctrine', 'dropping tables in database');
        $databaseManager = new sfDatabaseManager($this->configuration);
        // find out db name
        $dsn = Doctrine_Manager::connection()->getOption('dsn');
        $info = Doctrine_Manager::getInstance()->parsePdoDsn($dsn);
        $dbname = $info['dbname'];
        $pdo = Doctrine_Manager::connection()->getDbh();
        // disable constraint checking for mysql when innodb tables are used (TODO: more abstraction)
        $pdo->exec('SET foreign_key_checks = 0');
        // get all tables of db from information_schema db
        $sql = 'SELECT table_name FROM information_schema.tables WHERE information_schema.tables.table_schema = "'.$dbname.'"';
        $stmt = $pdo->query($sql);
        // drop all tables
        foreach ($stmt as $row) {
            $sql = 'DROP TABLE IF EXISTS `'.$dbname.'`.`'.$row[0].'`';
            $this->logSection('doctrine', 'dropping '.$dbname.'.'.$row[0]);

Sorry for non-sf indentation / codestyle, but you will get that right..;)

by neo on 2009-10-04, tagged doctrine  dropdb  droptables