Skip to content

Commit

Permalink
allow connecting to read or write connections with the db command (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
themsaid authored May 31, 2021
1 parent b3eafbd commit fdd6a3a
Showing 1 changed file with 24 additions and 3 deletions.
27 changes: 24 additions & 3 deletions src/Illuminate/Database/Console/DbCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

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

Expand Down Expand Up @@ -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}].");
Expand All @@ -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.
*
Expand Down

0 comments on commit fdd6a3a

Please sign in to comment.