-
Notifications
You must be signed in to change notification settings - Fork 1.5k
PHPORM-278 Introduce Connection::getDatabase()
and getClient()
#3289
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,8 +22,11 @@ | |
use function implode; | ||
use function is_array; | ||
use function preg_match; | ||
use function sprintf; | ||
use function str_contains; | ||
use function trigger_error; | ||
|
||
use const E_USER_DEPRECATED; | ||
use const FILTER_FLAG_IPV6; | ||
use const FILTER_VALIDATE_IP; | ||
|
||
|
@@ -65,9 +68,10 @@ public function __construct(array $config) | |
|
||
// Create the connection | ||
$this->connection = $this->createConnection($dsn, $config, $options); | ||
$this->database = $this->getDefaultDatabaseName($dsn, $config); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this dynamically assigning a class property, or is this declared on BaseConnection? Either way, this appears to be the first time it's being used. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The property is defined in the parent class, it was not set previously. |
||
|
||
// Select database | ||
$this->db = $this->connection->selectDatabase($this->getDefaultDatabaseName($dsn, $config)); | ||
$this->db = $this->connection->getDatabase($this->database); | ||
|
||
$this->tablePrefix = $config['prefix'] ?? ''; | ||
|
||
|
@@ -114,29 +118,53 @@ public function getSchemaBuilder() | |
/** | ||
* Get the MongoDB database object. | ||
* | ||
* @deprecated since mongodb/laravel-mongodb:5.2, use getDatabase() instead | ||
* | ||
* @return Database | ||
*/ | ||
public function getMongoDB() | ||
{ | ||
trigger_error(sprintf('Since mongodb/laravel-mongodb:5.2, Method "%s()" is deprecated, use "getDatabase()" instead.', __FUNCTION__), E_USER_DEPRECATED); | ||
|
||
return $this->db; | ||
} | ||
|
||
/** | ||
* Get the MongoDB database object. | ||
* | ||
* @param string|null $name Name of the database, if not provided the default database will be returned. | ||
* | ||
* @return Database | ||
*/ | ||
public function getDatabase(?string $name = null): Database | ||
{ | ||
if ($name && $name !== $this->database) { | ||
return $this->connection->getDatabase($name); | ||
} | ||
|
||
return $this->db; | ||
} | ||
|
||
/** | ||
* return MongoDB object. | ||
* Return MongoDB object. | ||
* | ||
* @deprecated since mongodb/laravel-mongodb:5.2, use getClient() instead | ||
* | ||
* @return Client | ||
*/ | ||
public function getMongoClient() | ||
{ | ||
return $this->connection; | ||
trigger_error(sprintf('Since mongodb/laravel-mongodb:5.2, method "%s()" is deprecated, use "getClient()" instead.', __FUNCTION__), E_USER_DEPRECATED); | ||
|
||
return $this->getClient(); | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
* Get the MongoDB client. | ||
*/ | ||
public function getDatabaseName() | ||
public function getClient(): ?Client | ||
{ | ||
return $this->getMongoDB()->getDatabaseName(); | ||
return $this->connection; | ||
} | ||
|
||
public function enableQueryLog() | ||
|
@@ -233,7 +261,7 @@ protected function createConnection(string $dsn, array $config, array $options): | |
*/ | ||
public function ping(): void | ||
{ | ||
$this->getMongoClient()->getManager()->selectServer(new ReadPreference(ReadPreference::PRIMARY_PREFERRED)); | ||
$this->getClient()->getManager()->selectServer(new ReadPreference(ReadPreference::PRIMARY_PREFERRED)); | ||
} | ||
|
||
/** @inheritdoc */ | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -48,23 +48,23 @@ public function testDisconnectAndCreateNewConnection() | |
{ | ||
$connection = DB::connection('mongodb'); | ||
$this->assertInstanceOf(Connection::class, $connection); | ||
$client = $connection->getMongoClient(); | ||
$client = $connection->getClient(); | ||
$this->assertInstanceOf(Client::class, $client); | ||
$connection->disconnect(); | ||
$client = $connection->getMongoClient(); | ||
$client = $connection->getClient(); | ||
$this->assertNull($client); | ||
DB::purge('mongodb'); | ||
$connection = DB::connection('mongodb'); | ||
$this->assertInstanceOf(Connection::class, $connection); | ||
$client = $connection->getMongoClient(); | ||
$client = $connection->getClient(); | ||
$this->assertInstanceOf(Client::class, $client); | ||
} | ||
|
||
public function testDb() | ||
{ | ||
$connection = DB::connection('mongodb'); | ||
$this->assertInstanceOf(Database::class, $connection->getMongoDB()); | ||
$this->assertInstanceOf(Client::class, $connection->getMongoClient()); | ||
$this->assertInstanceOf(Client::class, $connection->getClient()); | ||
} | ||
|
||
public static function dataConnectionConfig(): Generator | ||
|
@@ -196,14 +196,51 @@ public static function dataConnectionConfig(): Generator | |
public function testConnectionConfig(string $expectedUri, string $expectedDatabaseName, array $config): void | ||
{ | ||
$connection = new Connection($config); | ||
$client = $connection->getMongoClient(); | ||
$client = $connection->getClient(); | ||
|
||
$this->assertSame($expectedUri, (string) $client); | ||
$this->assertSame($expectedDatabaseName, $connection->getMongoDB()->getDatabaseName()); | ||
$this->assertSame('foo', $connection->getCollection('foo')->getCollectionName()); | ||
$this->assertSame('foo', $connection->table('foo')->raw()->getCollectionName()); | ||
} | ||
|
||
public function testLegacyGetMongoClient(): void | ||
{ | ||
$connection = DB::connection('mongodb'); | ||
$expected = $connection->getClient(); | ||
|
||
$this->assertSame($expected, $connection->getMongoClient()); | ||
} | ||
|
||
public function testLegacyGetMongoDB(): void | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do these legacy tests need to expect a deprecation message? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Something catches deprecation messages and hides them. I couldn't add an assertion. |
||
{ | ||
$connection = DB::connection('mongodb'); | ||
$expected = $connection->getDatabase(); | ||
|
||
$this->assertSame($expected, $connection->getMongoDB()); | ||
} | ||
|
||
public function testGetDatabase(): void | ||
{ | ||
$connection = DB::connection('mongodb'); | ||
$defaultName = env('MONGODB_DATABASE', 'unittest'); | ||
$database = $connection->getDatabase(); | ||
|
||
$this->assertInstanceOf(Database::class, $database); | ||
$this->assertSame($defaultName, $database->getDatabaseName()); | ||
$this->assertSame($database, $connection->getDatabase($defaultName), 'Same instance for the default database'); | ||
} | ||
|
||
public function testGetOtherDatabase(): void | ||
{ | ||
$connection = DB::connection('mongodb'); | ||
$name = 'other_random_database'; | ||
$database = $connection->getDatabase($name); | ||
|
||
$this->assertInstanceOf(Database::class, $database); | ||
$this->assertSame($name, $database->getDatabaseName($name)); | ||
} | ||
|
||
public function testConnectionWithoutConfiguredDatabase(): void | ||
{ | ||
$this->expectException(InvalidArgumentException::class); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Was this class always internal? Just curious if there's a BC concern here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We try to do better than Laravel's
ManagesTransactions
trait, that does not declare its dependencies. This trait is not meant to be used outside of theConnection
class (it should be merged in it but we kept Laravel's design).Technically, that's a breaking change that is easily overcome by adding a
getClient
method to the class that uses it.