Skip to content

[fix] default database detection from dsn #1971

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 2 commits into from
Feb 26, 2020
Merged
Show file tree
Hide file tree
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
38 changes: 25 additions & 13 deletions src/Jenssegers/Mongodb/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Illuminate\Database\Connection as BaseConnection;
use Illuminate\Support\Arr;
use InvalidArgumentException;
use MongoDB\Client;

class Connection extends BaseConnection
Expand Down Expand Up @@ -37,8 +38,11 @@ public function __construct(array $config)
// Create the connection
$this->connection = $this->createConnection($dsn, $config, $options);

// Get default database name
$default_db = $this->getDefaultDatabaseName($dsn, $config);

// Select database
$this->db = $this->connection->selectDatabase($this->getDatabaseDsn($dsn, $config['database']));
$this->db = $this->connection->selectDatabase($default_db);

$this->useDefaultPostProcessor();

Expand Down Expand Up @@ -114,6 +118,26 @@ public function getDatabaseName()
return $this->getMongoDB()->getDatabaseName();
}

/**
* Get the name of the default database based on db config or try to detect it from dsn
* @param string $dsn
* @param array $config
* @return string
* @throws InvalidArgumentException
*/
protected function getDefaultDatabaseName($dsn, $config)
{
if (empty($config['database'])) {
if (preg_match('/^mongodb:\\/\\/.+\\/([^?&]+)/s', $dsn, $matches)) {
$config['database'] = $matches[1];
} else {
throw new InvalidArgumentException("Database is not properly configured.");
}
}

return $config['database'];
}

/**
* Create a new MongoDB connection.
* @param string $dsn
Expand Down Expand Up @@ -191,18 +215,6 @@ protected function getHostDsn(array $config)
return 'mongodb://' . implode(',', $hosts) . ($auth_database ? '/' . $auth_database : '');
}

/**
* Get database name from DSN string, if there is no database in DSN path - returns back $database argument.
* @param string $dsn
* @param $database
* @return string
*/
protected function getDatabaseDsn($dsn, $database)
{
$dsnDatabase = trim(parse_url($dsn, PHP_URL_PATH), '/');
return trim($dsnDatabase) ? $dsnDatabase : $database;
}

/**
* Create a DSN string from a configuration.
* @param array $config
Expand Down
9 changes: 9 additions & 0 deletions tests/ConnectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,15 @@ public function testDb()
$this->assertInstanceOf(\MongoDB\Client::class, $connection->getMongoClient());
}

public function testDsnDb()
{
$connection = DB::connection('dsn_mongodb_db');
$this->assertInstanceOf(\MongoDB\Database::class, $connection->getMongoDB());

$connection = DB::connection('dsn_mongodb_db');
$this->assertInstanceOf(\MongoDB\Client::class, $connection->getMongoClient());
}

public function testCollection()
{
$collection = DB::connection('mongodb')->getCollection('unittest');
Expand Down
1 change: 1 addition & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ protected function getEnvironmentSetUp($app)
$app['config']->set('database.connections.mongodb', $config['connections']['mongodb']);
$app['config']->set('database.connections.mongodb2', $config['connections']['mongodb']);
$app['config']->set('database.connections.dsn_mongodb', $config['connections']['dsn_mongodb']);
$app['config']->set('database.connections.dsn_mongodb_db', $config['connections']['dsn_mongodb_db']);

$app['config']->set('auth.model', 'User');
$app['config']->set('auth.providers.users.model', 'User');
Expand Down
5 changes: 5 additions & 0 deletions tests/config/database.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@
'database' => env('MONGO_DATABASE', 'unittest'),
],

'dsn_mongodb_db' => [
'driver' => 'mongodb',
'dsn' => "mongodb://$mongoHost:$mongoPort/" . env('MONGO_DATABASE', 'unittest'),
],

'mysql' => [
'driver' => 'mysql',
'host' => env('MYSQL_HOST', 'mysql'),
Expand Down