Created
February 11, 2014 21:19
-
-
Save greglamb/8944404 to your computer and use it in GitHub Desktop.
Laravel DbImportCommand
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| use Illuminate\Console\Command; | |
| use Symfony\Component\Console\Input\InputOption; | |
| use Symfony\Component\Console\Input\InputArgument; | |
| use Symfony\Component\Process\Process; | |
| class DbImportCommand extends Command { | |
| /** | |
| * The console command name. | |
| * | |
| * @var string | |
| */ | |
| protected $name = 'db:import'; | |
| /** | |
| * The console command description. | |
| * | |
| * @var string | |
| */ | |
| protected $description = 'execute sql script'; | |
| /** | |
| * Create a new command instance. | |
| * | |
| * @return void | |
| */ | |
| public function __construct() | |
| { | |
| parent::__construct(); | |
| } | |
| /** | |
| * Execute the console command. | |
| * | |
| * @return void | |
| */ | |
| public function fire() | |
| { | |
| $config = Config::get('database.connections.'.Config::get('database.default')); | |
| $command = sprintf('PGPASSWORD=%s psql -h %s -U %s -d %s -a -f %s', | |
| escapeshellarg($config['password']), | |
| escapeshellarg($config['host']), | |
| escapeshellarg($config['username']), | |
| escapeshellarg($config['database']), | |
| escapeshellarg($this->argument('sqlfile')) | |
| ); | |
| $process = new Process($command); | |
| $process->run(); | |
| echo "Executed command : ".$command."\n"; | |
| if ($process->isSuccessful()) { | |
| return true; | |
| } else { | |
| return $process->getErrorOutput(); | |
| } | |
| } | |
| /** | |
| * Get the console command arguments. | |
| * | |
| * @return array | |
| */ | |
| protected function getArguments() | |
| { | |
| return [ | |
| ['sqlfile', InputArgument::REQUIRED, 'sql backup file'] | |
| ]; | |
| } | |
| /** | |
| * Get the console command options. | |
| * | |
| * @return array | |
| */ | |
| protected function getOptions() | |
| { | |
| return []; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment