Repository navigation
feat(client): let the current database change after construction - #64
Conversation
Every operation without an explicit database ran against the name the client was constructed with, so a consumer that switches databases (utopia-php/database Mongo adapter setDatabase(), or a renamed database) kept reading and writing the old one. setDatabase()/getDatabase() hold the current name per client, like its socket, so pooled clients carry it to no other coroutine.
Code review
Adds Walkthrough (2)
Reviewed Tip Comment |
|
No actionable comments were generated in the recent review. Walkthrough
Merge Risk:
|
…go to 1.6.1 Brings in the Mongo adapter following its current database (utopia-php/database#1107, utopia-php/mongo#64 and appwrite#65), which the re-recorded MongoDBTest rename case expects.

What
Adds
Client::setDatabase(string $name)andClient::getDatabase(). Every operation that takes no explicit database (find,insert,update,delete,count,aggregate,createCollection,dropDatabase()without a name, …) already resolves its target throughquery()'s$db ?? $this->database, so making that field settable is the whole change.Why
The client could only ever talk to the database it was constructed with. utopia-php/database's Mongo adapter therefore ignored
setDatabase(), and afterDatabase::update()renamed a database the adapter kept reading and writing the old name, contradictingDatabase::update()'s promise that a current database stays current under its new name. The adapter change follows in utopia-php/database.Design
selectDatabase()stays the existing no-op shim so callers that call it with no arguments keep working.Concurrency
The name is a plain instance property next to the socket and session state, never static. A client is not safe to share between concurrent coroutines anyway (one socket, one in-flight request), and pools hand a client to one coroutine at a time, so switching the database on one client cannot leak to another coroutine's client. In utopia-php/database each adapter owns its client, and the adapter's own
setDatabase()is what will set it, so the client's current database always equals its adapter's.Verification
MongoTest::testSetDatabase: writes after switching land in the new database, are invisible from the original one, and an empty name throws.Summary by CodeRabbit