Skip to content
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

[8.x] Allow connecting to read or write connections with the db command #37530

Merged
merged 1 commit into from
May 31, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 24 additions & 3 deletions src/Illuminate/Database/Console/DbCommand.php
Original file line number Diff line number Diff line change
@@ -4,6 +4,7 @@

use Illuminate\Console\Command;
use Illuminate\Support\ConfigurationUrlParser;
use Illuminate\Support\Str;
use Symfony\Component\Process\Process;
use UnexpectedValueException;

@@ -52,9 +53,11 @@ public function handle()
*/
public function getConnection()
{
$connection = $this->laravel['config']['database.connections.'.
(($db = $this->argument('connection')) ?? $this->laravel['config']['database.default'])
];
[$db, $type] = $this->parseConnectionName(
$this->argument('connection') ?? $this->laravel['config']['database.default']
);

$connection = $this->laravel['config']['database.connections.'.$db];

if (empty($connection)) {
throw new UnexpectedValueException("Invalid database connection [{$db}].");
@@ -64,9 +67,27 @@ public function getConnection()
$connection = (new ConfigurationUrlParser)->parseConfiguration($connection);
}

if (isset($type)) {
$connection = array_merge($connection, $connection[$type]);
}

return $connection;
}

/**
* Parse the connection into an array of the name and read / write type.
*
* @param string $name
* @return array
*/
protected function parseConnectionName($name)
{
$name = $name ?: $this->getDefaultConnection();

return Str::endsWith($name, ['::read', '::write'])
? explode('::', $name, 2) : [$name, null];
}

/**
* Get the arguments for the database client command.
*