Skip to content

feat(client): let the current database change after construction - #64

Merged
ChiragAgg5k merged 1 commit into
mainfrom
feat/set-database
Dec 4, 2026
Merged

ChiragAgg5k merged 1 commit into
mainfrom
feat/set-database

Conversation

@ChiragAgg5k

@ChiragAgg5k ChiragAgg5k commented Dec 4, 2026 •

Copy link
Copy Markdown
Member

What

Adds Client::setDatabase(string $name) and Client::getDatabase(). Every operation that takes no explicit database (find, insert, update, delete, count, aggregate, createCollection, dropDatabase() without a name, …) already resolves its target through query()'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 after Database::update() renamed a database the adapter kept reading and writing the old name, contradicting Database::update()'s promise that a current database stays current under its new name. The adapter change follows in utopia-php/database.

Design

  • One settable current database on the client instead of a database parameter on every method: no signature changes, fully backward compatible (a minor release, 1.6.0).
  • selectDatabase() stays the existing no-op shim so callers that call it with no arguments keep working.
  • An empty name is refused, matching the constructor.

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.
  • Pint and PHPStan pass locally.

Summary by CodeRabbit

  • New Features
    • Added the ability to select and retrieve the database used by a client. Operations without an explicitly specified database use the selected database.
    • Empty database names are rejected.

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.
@appwrite

appwrite Bot commented Dec 4, 2026 •

Copy link
Copy Markdown

Code review

Commit Status Tier Verdict Details
a1c6e54 Reviewed Reviewed 🟢 S · Ready to merge ✅ Approved View review

The setter updates the existing default used by query operations, and the added test covers the core switch behavior and empty-name validation.

Adds getDatabase() and a fluent setDatabase() to Client, allowing default-database operations to use a database name changed after construction. Adds a Mongo integration test for switching databases and rejecting an empty name.

Walkthrough (2)
File Change
src/Client.php Adds accessors for the current database and rejects an empty name when changing it.
tests/MongoTest.php Checks that writes and reads use the newly selected database and that empty names are rejected.

Reviewed a1c6e54

Tip

Comment @appwrite review to re-run, or mention @appwrite with a question.

@coderabbitai

coderabbitai Bot commented Dec 4, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

The client now exposes getDatabase() and setDatabase(string $name) to read and change its current database. Tests verify database selection, isolation between databases, cleanup, and rejection of an empty database name.

Changes: Client Database Selection

Layer / File(s) Summary
Database selection API
src/Client.php, tests/MongoTest.php
Client adds getDatabase() and setDatabase(string $name). Operations without an explicit database use the selected name. The setter rejects an empty name and returns the client for chaining. Tests check reads and writes across two databases, cleanup, and empty-name rejection.

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Feature

Suggested reviewers: fogelito

Merge Risk: Minimal · up to a1c6e

This adds the ability to read and change a client's current database after construction. Existing method signatures are unchanged. No concrete merge-blocking risk was identified.

Pre-merge checks: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title names the main change: the client's current database can now be changed after construction. The summary and objectives both describe adding Client::setDatabase() and Client::getDatabase() fo…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Title check

Explanation

The title names the main change: the client's current database can now be changed after construction. The summary and objectives both describe adding Client::setDatabase() and Client::getDatabase() for this purpose, so the title matches the changeset. It is concise and specific, and it uses the conventional type(scope) prefix.

Finishing Touches

Generate unit tests (beta)

  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

Commits

Reviewing files that changed from the base of the PR and between 90c29bb and a1c6e54.

Files selected for processing: 2

  • src/Client.php
  • tests/MongoTest.php

Included review availability: This review used your included allowance. Your plan provides up to 1 included review per hour; 0 remain after this review.

@appwrite appwrite Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Tier S · Looks good to merge. Summary

@ChiragAgg5k
ChiragAgg5k merged commit 57313a3 into main Dec 4, 2026
5 checks passed
@ChiragAgg5k
ChiragAgg5k deleted the feat/set-database branch December 4, 2026 20:28
JadeCong pushed a commit to CloudEngineHub/appwrite that referenced this pull request Dec 5, 2026
…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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant