Unverified Commit 22a9e3be authored by Stas's avatar Stas Committed by GitHub

Merge pull request #1971 from divine/refactor_and_fix_db_selection

[fix] default database detection from dsn
parents 50e75d6a f2ce3d4f
......@@ -4,6 +4,7 @@ namespace Jenssegers\Mongodb;
use Illuminate\Database\Connection as BaseConnection;
use Illuminate\Support\Arr;
use InvalidArgumentException;
use MongoDB\Client;
class Connection extends BaseConnection
......@@ -37,8 +38,11 @@ class Connection extends BaseConnection
// 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();
......@@ -114,6 +118,26 @@ class Connection extends BaseConnection
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
......@@ -191,18 +215,6 @@ class Connection extends BaseConnection
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
......
......@@ -32,6 +32,15 @@ class ConnectionTest extends TestCase
$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');
......
......@@ -53,6 +53,7 @@ class TestCase extends Orchestra\Testbench\TestCase
$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');
......
......@@ -21,6 +21,11 @@ return [
'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'),
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment