/** * @see sfTask */ protected function execute($arguments = array(), $options = array()) { $databaseManager = new sfDatabaseManager($this->configuration); $databases = $this->getDoctrineDatabases($databaseManager, isset($options['database']) ? array($options['database']) : null); // get database to dump if (count($databases) > 1) { $this->logSection('doctrine', 'As you have multiple databases a database must be specified', null, 'ERROR'); return; } $databaseName = key($databases); $database = current($databases); // check connection is mysql if (!$database->getDoctrineConnection() instanceof Doctrine_Connection_Mysql) { $this->logSection('doctrine', 'Database connection must be MySQL', null, 'ERROR'); return; } $files = array(); // get the files foreach ($arguments['dir_or_file'] as $target) { if (!file_exists($target)) { $this->logSection('mysql', 'The path ' . $target . ' does not exist', null, 'ERROR'); return; } // add files to array if (is_dir($target)) { $this->logSection('finder', 'loading .sql files from ' . $target); $files = array_merge($files, sfFinder::type('file')->name('*.sql')->in($target)); } else { $files[] = $target; } } // no files if (!$files) { $this->logSection('mysql', 'There are no files to load data from', null, 'ERROR'); return; } if (!$options['append'] && !$options['no-confirmation']) { // check if we need to confirm database drop $confirmation = $this->askConfirmation(array('This task will replace data in the database with whats specified ', 'in the SQL files. This will drop all data currently stored', '', 'Are you sure you want to proceed? (y/N)'), 'QUESTION_LARGE', false); if (!$confirmation) { $this->logSection('mysql', 'SQL load task aborted'); return 1; } } if (!$options['append']) { $this->logSection('doctrine', 'Dropping database'); $database->getDoctrineConnection()->dropDatabase(); $this->logSection('doctrine', 'Creating empty database'); $database->getDoctrineConnection()->createDatabase(); } $importer = new sfDoctrineMysqlSafeMigrateMysqlCliImport($database->getDoctrineConnection(), sfConfig::get('app_sfDoctrineMysqlSafeMigratePlugin_mysql_path'), sfConfig::get('app_sfDoctrineMysqlSafeMigratePlugin_mysql_arguments')); foreach ($files as $file) { $this->logSection('mysql', 'Importing ' . $file); $importer->importFrom($file); $this->logSection('mysql', 'Completed importing ' . $file . ' successfully'); } $this->logSection('mysql', 'SQL load completed'); }
/** * * @param Doctrine_Connection_Mysql $connection * @param string $backupPath The path to the sql file * to restore * @return void */ protected function restoreBackup(Doctrine_Connection_Mysql $connection, $backupPath) { if (!file_exists($backupPath)) { throw new Exception('Backup to restore database doesn\'t exist'); } $connection->dropDatabase(); $connection->createDatabase(); $importer = new sfDoctrineMysqlSafeMigrateMysqlCliImport($connection, sfConfig::get('app_sfDoctrineMysqlSafeMigratePlugin_mysql_path'), sfConfig::get('app_sfDoctrineMysqlSafeMigratePlugin_mysql_arguments')); try { $importer->importFrom($backupPath); } catch (Exception $e) { throw $e; } }