Question

I'm trying to make a user friendly way of testing via lime in symfony 1. I want to load a specific sql dump for each test I write (if required). The problem I face is that I don't know how to make dump loading independent of database type. Currently I'm using the shell exec() command. Here is the code :

  public function loadSql()
  {
    $this->diag("Loading dump...");
    if ($this->_sql_is_set)
    {
      if (file_exists($this->_getSqlPath()))
      {
        $this->_emptyDataBase();
        $options   = $this->_connection_manager->connection()->getOptions();
        $dsn_parts = $this->_connection_manager->parsePdoDsn($options['dsn']);
        exec("mysql -u{$options['username']} -p{$options['password']} {$dsn_parts['dbname']} < {$this->_getSqlPath()}");
        return $this;
      }
      else
      {
        $this->error("Nothing to load : sql file was not found in ".$this->_getDataDir());
        exit;
      }
    }
    else
    {
      $this->error("Nothing to load : sql dump was not set");
      exit;
    }
  }

$this->_connection_manager is an instance of Doctrine_Manager. Any help will with that?

Was it helpful?

Solution

Try with something like that:

public function loadSqlFiles(sfEvent $event)
{
    $task = $event->getSubject();
    $taskName = $task->getName();
    if ($taskName == 'insert-sql') {
        $conn = Doctrine_Manager::connection();
        $filesPath = sfConfig::get('sf_data_dir') . '/sql/full-data';

        // get all files
        $files = sfFinder::type('file')->sort_by_name()->name('*.sql')->in($filesPath);
        foreach ($files as $file) {
            $task->logSection('custom-sql', sprintf('Inserting custom sql file (%s)', $file));
            $res = $conn->getDbh()->exec(file_get_contents($file));
        }
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top