-
Notifications
You must be signed in to change notification settings - Fork 11.5k
[8.x] Add DB command to drop into the database CLI #35304
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
d051970
[8.x] Add DB command to open the database CLI
paras-malhotra 696a1d3
fix styleci
paras-malhotra 6d401d2
fix php 7.3 compatibility with spread operator
paras-malhotra 3bf0ee5
fix typo in docblock
paras-malhotra 1b348b4
Change database option to argument and rename to connection
paras-malhotra a6385ce
formatting
paras-malhotra e40ce91
styleci
paras-malhotra 94d0caa
typo fix
paras-malhotra File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,195 @@ | ||
<?php | ||
|
||
namespace Illuminate\Database\Console; | ||
|
||
use Illuminate\Console\Command; | ||
use Symfony\Component\Process\Process; | ||
use UnexpectedValueException; | ||
|
||
class DbCommand extends Command | ||
{ | ||
/** | ||
* The name and signature of the console command. | ||
* | ||
* @var string | ||
*/ | ||
protected $signature = 'db {connection? : The database connection to use}'; | ||
|
||
/** | ||
* The console command description. | ||
* | ||
* @var string | ||
*/ | ||
protected $description = 'Drop into the database CLI.'; | ||
|
||
/** | ||
* Execute the console command. | ||
* | ||
* @return int | ||
*/ | ||
public function handle() | ||
{ | ||
$connection = $this->getConnection(); | ||
|
||
(new Process( | ||
array_merge([$this->getCommand($connection)], $this->getArgs($connection)), | ||
null, $this->getEnv($connection) | ||
))->setTimeout(null)->setTty(true)->mustRun(function ($type, $buffer) { | ||
$this->output->write($buffer); | ||
}); | ||
|
||
return 0; | ||
} | ||
|
||
/** | ||
* Get the database connection configuration. | ||
* | ||
* @return array | ||
*/ | ||
public function getConnection() | ||
{ | ||
$connection = $this->laravel['config']['database.connections.'. | ||
(($db = $this->argument('connection')) ?? $this->laravel['config']['database.default']) | ||
]; | ||
|
||
if (empty($connection)) { | ||
throw new UnexpectedValueException("Invalid database connection: [{$db}]."); | ||
} | ||
|
||
return $connection; | ||
} | ||
|
||
/** | ||
* Get the arguments for the database client command. | ||
* | ||
* @param array $connection | ||
* @return array | ||
*/ | ||
public function getArgs(array $connection) | ||
{ | ||
$driver = ucfirst($connection['driver']); | ||
|
||
return $this->{"get{$driver}Args"}($connection); | ||
} | ||
|
||
/** | ||
* Get the environmental variables for the database client command. | ||
* | ||
* @param array $connection | ||
* @return array|null | ||
*/ | ||
public function getEnv(array $connection) | ||
{ | ||
$driver = ucfirst($connection['driver']); | ||
|
||
if (method_exists($this, "get{$driver}Env")) { | ||
return $this->{"get{$driver}Env"}($connection); | ||
} | ||
|
||
return null; | ||
} | ||
|
||
/** | ||
* Get the database client command to run. | ||
* | ||
* @param array $connection | ||
* @return string | ||
*/ | ||
public function getCommand(array $connection) | ||
{ | ||
return [ | ||
'mysql' => 'mysql', | ||
'pgsql' => 'psql', | ||
'sqlite' => 'sqlite3', | ||
'sqlsrv' => 'sqlcmd', | ||
][$connection['driver']]; | ||
} | ||
|
||
/** | ||
* Get the arguments for the mysql CLI. | ||
* | ||
* @param array $connection | ||
* @return array | ||
*/ | ||
protected function getMysqlArgs(array $connection) | ||
{ | ||
return array_merge([ | ||
'--host='.$connection['host'], | ||
'--port='.$connection['port'], | ||
'--user='.$connection['username'], | ||
], $this->buildOptionalArguments([ | ||
'password' => '--password='.$connection['password'], | ||
'unix_socket' => '--socket='.$connection['unix_socket'], | ||
'charset' => '--default-character-set='.$connection['charset'], | ||
], $connection), [$connection['database']]); | ||
} | ||
|
||
/** | ||
* Get the arguments for the pgsql CLI. | ||
* | ||
* @param array $connection | ||
* @return array | ||
*/ | ||
protected function getPgsqlArgs(array $connection) | ||
{ | ||
return [$connection['database']]; | ||
} | ||
|
||
/** | ||
* Get the arguments for the sqlite CLI. | ||
* | ||
* @param array $connection | ||
* @return array | ||
*/ | ||
protected function getSqliteArgs(array $connection) | ||
{ | ||
return [$connection['database']]; | ||
} | ||
|
||
/** | ||
* Get the arguments for the SQL Server CLI. | ||
*< 8000 /td> | ||
* @param array $connection | ||
* @return array | ||
*/ | ||
protected function getSqlsrvArgs(array $connection) | ||
{ | ||
return $this->buildOptionalArguments([ | ||
'database' => '-d '.$connection['database'], | ||
'username' => '-U '.$connection['username'], | ||
'password' => '-P '.$connection['password'], | ||
'host' => '-S tcp:'.$connection['host'] | ||
.($connection['port'] ? ','.$connection['port'] : ''), | ||
], $connection); | ||
} | ||
|
||
/** | ||
* Get the environmental variables for the pgsql CLI. | ||
* | ||
* @param array $connection | ||
* @return array|null | ||
*/ | ||
protected function getPgsqlEnv(array $connection) | ||
{ | ||
return array_merge(...$this->buildOptionalArguments([ | ||
'username' => ['PGUSER' => $connection['username']], | ||
'host' => ['PGHOST' => $connection['host']], | ||
'port' => ['PGPORT' => $connection['port']], | ||
'password' => ['PGPASSWORD' => $connection['password']], | ||
], $connection)); | ||
} | ||
|
||
/** | ||
* Build optional arguments based on the connection configuration. | ||
* | ||
* @param array $args | ||
* @param array $connection | ||
* @return array | ||
*/ | ||
protected function buildOptionalArguments(array $args, array $connection) | ||
{ | ||
return array_values(array_filter($args, function ($key) use ($connection) { | ||
return ! empty($connection[$key]); | ||
}, ARRAY_FILTER_USE_KEY)); | ||
} | ||
} |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.