Skip to content

Commit

Permalink
formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
taylorotwell committed Dec 16, 2024
1 parent 3565ab0 commit acc15c9
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 7 deletions.
24 changes: 18 additions & 6 deletions src/Illuminate/Database/DatabaseManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Illuminate\Database\Connectors\ConnectionFactory;
use Illuminate\Database\Events\ConnectionEstablished;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
use Illuminate\Support\ConfigurationUrlParser;
use Illuminate\Support\Str;
use Illuminate\Support\Traits\Macroable;
Expand Down Expand Up @@ -43,9 +44,9 @@ class DatabaseManager implements ConnectionResolverInterface
protected $connections = [];

/**
* Connection configuration for dynamic connections.
* The dynamically configured (DB::build) connection configurations.
*
* @var array<string, \Illuminate\Database\Connection>
* @var array<string, array>
*/
protected $dynamicConnections = [];

Expand Down Expand Up @@ -117,14 +118,27 @@ public function connection($name = null)
public function build(array $config)
{
if (! isset($config['name'])) {
$config['name'] = 'dynamic_'.Str::uuid();
$config['name'] = static::calculateDynamicConnectionName($config);
}

$this->dynamicConnections[$config['name']] = $config;

return $this->connectUsing($config['name'], $config, true);
}

/**
* Calculate the dynamic connection name for an on-demand connection based on its configuration.
*
* @param array $config
* @return string
*/
public static function calculateDynamicConnectionName(array $config)
{
return 'dynamic_'.md5((new Collection($config))->map(function ($value, $key) {
return $key.(is_string($value) || is_int($value) ? $value : '');
})->implode(''));
}

/**
* Get a database connection instance from the given configuration.
*
Expand Down Expand Up @@ -204,11 +218,9 @@ protected function makeConnection($name)
protected function configuration($name)
{
$name = $name ?: $this->getDefaultConnection();

$connections = $this->app['config']['database.connections'];

// Attempt to find the config in dynamicConnections first,
// otherwise fallback to the regular connections config.
// If the configuration doesn't exist, we'll throw an exception and bail.
$config = $this->dynamicConnections[$name] ?? Arr::get($connections, $name);

if (is_null($config)) {
Expand Down
20 changes: 19 additions & 1 deletion tests/Integration/Database/DatabaseConnectionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ public function testTablePrefix()
$this->assertSame('', DB::getTablePrefix());
}

public function testDynamicConnectionFailsOnReconnect()
public function testDynamicConnectionDoesntFailOnReconnect()
{
$connection = DB::build([
'name' => 'projects',
Expand All @@ -154,4 +154,22 @@ public function testDynamicConnectionFailsOnReconnect()
}
}
}

public function testDynamicConnectionWithNoNameDoesntFailOnReconnect()
{
$connection = DB::build([
'driver' => 'sqlite',
'database' => ':memory:',
]);

$this->expectNotToPerformAssertions();

try {
$connection->reconnect();
} catch (InvalidArgumentException $e) {
if ($e->getMessage() === 'Database connection [projects] not configured.') {
$this->fail('Dynamic connection should not throw an exception on reconnect.');
}
}
}
}

0 comments on commit acc15c9

Please sign in to comment.