Last active
December 27, 2015 22:54
-
-
Save greglamb/8927175 to your computer and use it in GitHub Desktop.
Laravel DbSchemadumpCommand
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 DbSchemadumpCommand extends Command { | |
/** | |
* The console command name. | |
* | |
* @var string | |
*/ | |
protected $name = 'db:schemadump'; | |
/** | |
* The console command description. | |
* | |
* @var string | |
*/ | |
protected $description = 'save schema'; | |
/** | |
* 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 pg_dump --no-acl --schema-only --verbose -N "public" --no-owner -h %s -U %s %s > %s', | |
escapeshellarg($config['password']), | |
escapeshellarg($config['host']), | |
escapeshellarg($config['username']), | |
escapeshellarg($config['database']), | |
escapeshellarg(app_path().'/database/backup/pgschema_'.date("YmdHis").'.'.uniqid().'.sql') | |
); | |
$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 []; | |
} | |
/** | |
* 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