Skip to content

Instantly share code, notes, and snippets.

@Palleas
Created February 11, 2010 14:14
Show Gist options
  • Save Palleas/301536 to your computer and use it in GitHub Desktop.
Save Palleas/301536 to your computer and use it in GitHub Desktop.
<?php
/**
* NOELoadSQLDumpTask
*
* @package NOECommons
* @author Romain Pouclet <[email protected]>
**/
class NOELoadSQLDumpTask extends sfBaseTask
{
/**
* configure
* Configures the task
*
* @return void
* @author Romain Pouclet
*/
protected function configure()
{
$this->addOptions(array(
new sfCommandOption('application', null, sfCommandOption::PARAMETER_REQUIRED, 'The application name'),
new sfCommandOption('env', null, sfCommandOption::PARAMETER_REQUIRED, 'The environment', 'dev'),
new sfCommandOption('connection', null, sfCommandOption::PARAMETER_REQUIRED, 'The connection name', 'doctrine'),
));
$this->addArguments(array(
new sfCommandArgument('file', sfCommandArgument::REQUIRED, 'My argument'),
));
$this->namespace = 'NOE';
$this->name = 'load-sql-dump';
$this->aliases = array('lsd');
$this->briefDescription = 'Load and Insert a SQL Dump';
$this->detailedDescription = <<<EOF
The [NOELoadSQLDump|INFO] task loads and insert a SQL file
Call it with:
[php symfony NOELoadSQLDump file|INFO]
EOF;
}
/**
* execute
* Runs the task
*
* @param string $arguments
* @param string $options
* @return void
* @author Romain Pouclet
*/
protected function execute($arguments = array(), $options = array())
{
$databaseManager = new sfDatabaseManager($this->configuration);
$connection = $databaseManager->getDatabase($options['connection'])->getConnection();
$file = $arguments['file'];
// Checking file validity
$this->logSection('File', sprintf('Checking if file "%s" exists', $file));
if (!file_exists($file))
{
throw new InvalidArgumentException(sprintf('%s : "%s" is not a valid SQL dump file', __CLASS__, $file));
}
$this->logSection('File', 'File exists');
// Loading dump file
$this->logSection('File', 'Loading file content');
$content = file_get_contents($file);
// Inserting
$this->logSection('Doctrine', 'Passing data to the ORM');
$statement = $connection->prepare($content);
$this->logSection('Doctrine', 'Executing');
$statement->execute();
$this->logSection('File', 'Dump loading done');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment