Skip to content

Pool hostname#863

Open
fogelito wants to merge 6 commits intomainfrom
setHostname
Open

Pool hostname#863
fogelito wants to merge 6 commits intomainfrom
setHostname

Conversation

@fogelito
Copy link
Copy Markdown
Contributor

@fogelito fogelito commented Apr 23, 2026

Summary by CodeRabbit

  • New Features
    • Added support for retrieving the hostname of your connected database across all supported database adapters, enabling better connection monitoring and diagnostics.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Apr 23, 2026

📝 Walkthrough

Walkthrough

Updates the utopia-php/mongo dependency to use a specific branch alias, adds getHostname() methods to Mongo and Pool adapters to expose database hostnames, and introduces an E2E test validating the hostname functionality across supported database types.

Changes

Cohort / File(s) Summary
Dependency Management
composer.json
Updates utopia-php/mongo constraint from generic stable series to branch alias mapping dev-get-host to version 1.0.3.
Adapter API Extensions
src/Database/Adapter/Mongo.php, src/Database/Adapter/Pool.php
Introduces getHostname(): string public methods; Mongo adapter exposes client host string, Pool adapter delegates to active adapter.
E2E Testing
tests/e2e/Adapter/Scopes/CollectionTests.php
Adds testDatabaseHostname() to validate adapter hostname retrieval against expected database types (mysql, mariadb, postgres, mongo).

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~12 minutes

Possibly related PRs

  • PR #806: Modifies the same composer.json dependency constraint for utopia-php/mongo, indicating parallel version management efforts.

Poem

🐰 Hop, hop! A hostname now takes flight,
From Mongo tunnels to Pool's delight,
Adapters expose their dwelling place,
Tests confirm each database's face!

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 28.57% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'Pool hostname' accurately describes the main change: adding a getHostname() method to the Pool adapter class, which is the central feature of this pull request.
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.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch setHostname

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

Comment @coderabbitai help to get the list of available commands and usage tips.

@greptile-apps
Copy link
Copy Markdown

greptile-apps Bot commented Apr 23, 2026

Greptile Summary

This PR adds hostname support to the Pool adapter (by delegating getHostname() and getSupportForHostname() through the pool) and fixes Mongo::getHostname() to return the real client host via $this->client->getHost() instead of the previously hardcoded 'mongo' string.

  • composer.json uses a dev-branch alias (dev-get-host as 1.0.3) to access the not-yet-released getHost() method on utopia-php/mongo. This bypasses minimum-stability: stable and pins the package to a mutable branch tip; a proper tagged release should be cut before merging.

Confidence Score: 4/5

Safe to review, but should not be merged to main until utopia-php/mongo has a proper stable release including getHost().

The implementation changes (Pool delegation, Mongo hostname fix) are correct. The blocking concern is the dev-get-host as 1.0.3 Composer alias: it is a mutable dev branch masquerading as a stable release and is unsuitable for a library that consumers depend on.

composer.json — the dev-get-host as 1.0.3 dependency must be replaced with a proper tagged release of utopia-php/mongo.

Important Files Changed

Filename Overview
composer.json Pins utopia-php/mongo to dev-get-host as 1.0.3 — a mutable dev-branch alias that bypasses minimum-stability and should be replaced with a tagged release.
src/Database/Adapter/Mongo.php getHostname() now correctly delegates to $this->client->getHost() instead of the previously hardcoded 'mongo' literal; getSupportForHostname() returns true.
src/Database/Adapter/Pool.php Adds getHostname() delegation through the pool, consistent with all other methods in this class.
tests/e2e/Adapter/Scopes/CollectionTests.php testDatabaseHostname() now correctly early-returns with expectNotToPerformAssertions() for unsupported adapters, and includes 'mongo' in the allowed hostname list.
composer.lock Lock file updated to reflect the dev-get-host branch for utopia-php/mongo and a minor bump to google/protobuf (v4.33.5v4.33.6).

Reviews (4): Last reviewed commit: "Mongo getHost" | Re-trigger Greptile

Comment thread tests/e2e/Adapter/Scopes/CollectionTests.php Outdated
Comment thread tests/e2e/Adapter/Scopes/CollectionTests.php Outdated
Comment thread tests/e2e/Adapter/Scopes/CollectionTests.php Outdated
Comment thread src/Database/Adapter/Mongo.php
Comment thread src/Database/Adapter/Mongo.php
Comment thread src/Database/Adapter/Mongo.php
@claude
Copy link
Copy Markdown

claude Bot commented Apr 23, 2026

CI failure on run 24836281128 is a flaky, timing-dependent test unrelated to this branch.

Failed test: Tests\E2E\Adapter\SharedTables\MariaDBTest::testSingleDocumentDateOperations
Assertion: Failed asserting that '2026-04-23T12:55:07.088+00:00' is not equal to '2026-04-23T12:55:07.088+00:00'. (DocumentTests.php:6029)

Root cause: The test creates doc11 with setPreserveDates(true) (providing $createdAt but no $updatedAt), captures $newUpdatedAt = $doc11->getUpdatedAt(), then immediately updates the document and asserts the new $updatedAt differs. Timestamps are stored at millisecond precision, so when create and update complete within the same millisecond the two values are identical and the assertion fires — which is exactly what happened here (both read 12:55:07.088+00:00).

Why it is not this branch's fault:

  • The failing test was added in commit 667878ca ("Refactor DocumentTests to improve test coverage and structure"), long before this branch.
  • This branch's only diff vs. main (git diff 9a1165f7..HEAD) is a 3-line tweak to tests/e2e/Adapter/Scopes/CollectionTests.php in the testHostName path — no src/ changes, no changes to date/timestamp handling.

No code changes made — exiting without commit per healing protocol for flaky failures. A retry of the CI run should be green. The underlying flakiness in testSingleDocumentDateOperations is worth fixing separately (e.g. a brief sleep or assertGreaterThanOrEqual) but is out of scope for this PR.

Comment thread composer.json
"utopia-php/cache": "1.*",
"utopia-php/pools": "1.*",
"utopia-php/mongo": "1.*"
"utopia-php/mongo": "dev-get-host as 1.0.3"
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Dev-branch alias used as production dependency

"utopia-php/mongo": "dev-get-host as 1.0.3" pins the entire library to an unpublished development branch aliased as a stable version. Composer's as alias bypasses the minimum-stability: stable guard and lets the branch be installed as if it were a real 1.0.3 release — but the underlying ref is a mutable branch tip. If that branch is rebased, force-pushed, or deleted, any fresh install will either break or silently pull different code. This should be replaced with a proper tagged release of utopia-php/mongo that includes the getHost() method before this PR is merged to main.

@claude
Copy link
Copy Markdown

claude Bot commented Apr 23, 2026

CI Healing — Flaky Infra, No Fix Required

The failing job was Adapter Tests (MySQL) on run 24839927756. All 535 errors are identical:

PDOException: SQLSTATE[HY000] [2002] Connection refused
    at src/Database/PDO.php:29  (the initial `new \PDO(...)` call)

Why this is flake, not a code bug:

  • The job died in 1m0s — far shorter than a normal test run.
  • Errors originate at the first PDO constructor call, meaning the MySQL service never became reachable from the test container.
  • Every other adapter job on the same commit passed, including Adapter Tests (SharedTables/MySQL) (3m48s) which uses the same MySQL service image. If the branch's changes had broken MySQL, that job would have failed identically.
  • Nothing on this branch (Mongo::getHostname(), Pool::getHostname(), testDatabaseHostname, composer bump for utopia-php/mongo) touches MySQL connection setup, DSN parsing, or test bootstrap.

Action: No code change. Re-running the workflow should clear it.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 2

🧹 Nitpick comments (1)
src/Database/Adapter/Mongo.php (1)

81-84: Consider consistency with SQL adapter's error handling pattern, but not required.

The Mongo adapter's getHostname() differs from the SQL adapter: SQL wraps its call in try/catch for defensive error handling, while Mongo returns the result directly. However, the concern about Client::getHost() potentially throwing is unfounded—it's a simple property accessor that returns a string and cannot fail. The SQL adapter's defensive pattern exists because PDO::getHostname() can actually fail; Mongo's Client::getHost() simply returns an initialized string property.

Adding error handling for consistency is a reasonable stylistic choice, but it is not required for correctness.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/Database/Adapter/Mongo.php` around lines 81 - 84, The Mongo adapter's
getHostname() currently returns $this->client->getHost() directly while the SQL
adapter wraps its hostname retrieval in a try/catch; to keep handling
consistent, update Mongo::getHostname() (method getHostname in class using
Client::getHost) to optionally wrap the call in a try/catch that returns the
host on success and logs/returns an empty string or fallback on exception, or
leave as-is if you prefer the simpler direct return since Client::getHost() is a
safe accessor—choose one approach and apply it to getHostname() for consistency
with the SQL adapter's defensive pattern.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@composer.json`:
- Line 44: The dependency "utopia-php/mongo" is pinned to a dev-alias
("dev-get-host as 1.0.3") which should not be merged to main; wait for a proper
release of the utopia-php/mongo change (e.g., tag 1.0.4) and update the composer
constraint in composer.json (replace "utopia-php/mongo": "dev-get-host as
1.0.3") with a stable range such as "utopia-php/mongo": "^1.0.4" or "1.*" so
installations are stable and compatible with consumers' minimum-stability.

In `@tests/e2e/Adapter/Scopes/CollectionTests.php`:
- Around line 76-89: In testDatabaseHostname(), avoid calling getHostname()
before checking support: first call
$database->getAdapter()->getSupportForHostname() and bail with
$this->expectNotToPerformAssertions() if false, then call
$database->getAdapter()->getHostname(); replace the current
$this->assertTrue(in_array($host, [...])) with $this->assertContains($host,
['mysql','mariadb','postgres','mongo']) to get clearer failure output and better
semantics (referencing testDatabaseHostname, getAdapter(),
getSupportForHostname(), getHostname(), assertContains(), and
expectNotToPerformAssertions()).

---

Nitpick comments:
In `@src/Database/Adapter/Mongo.php`:
- Around line 81-84: The Mongo adapter's getHostname() currently returns
$this->client->getHost() directly while the SQL adapter wraps its hostname
retrieval in a try/catch; to keep handling consistent, update
Mongo::getHostname() (method getHostname in class using Client::getHost) to
optionally wrap the call in a try/catch that returns the host on success and
logs/returns an empty string or fallback on exception, or leave as-is if you
prefer the simpler direct return since Client::getHost() is a safe
accessor—choose one approach and apply it to getHostname() for consistency with
the SQL adapter's defensive pattern.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: cdb82fcc-73c1-46a3-8491-88c8cb05ca9e

📥 Commits

Reviewing files that changed from the base of the PR and between fd591ea and 6d3483a.

⛔ Files ignored due to path filters (1)
  • composer.lock is excluded by !**/*.lock
📒 Files selected for processing (4)
  • composer.json
  • src/Database/Adapter/Mongo.php
  • src/Database/Adapter/Pool.php
  • tests/e2e/Adapter/Scopes/CollectionTests.php

Comment thread composer.json
"utopia-php/cache": "1.*",
"utopia-php/pools": "1.*",
"utopia-php/mongo": "1.*"
"utopia-php/mongo": "dev-get-host as 1.0.3"
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟠 Major

Avoid merging a dev-branch dependency to main.

Pinning utopia-php/mongo to dev-get-host as 1.0.3 ties this PR to an unmerged feature branch of the Mongo package. Merging this to main will make installs brittle (branch can disappear/move) and break downstream consumers whose minimum-stability doesn't permit dev aliases transitively. Please get the corresponding utopia-php/mongo change released as a tagged version (e.g., 1.0.4) and update the constraint to a stable range ("1.*" or "^1.0.4") before merging.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@composer.json` at line 44, The dependency "utopia-php/mongo" is pinned to a
dev-alias ("dev-get-host as 1.0.3") which should not be merged to main; wait for
a proper release of the utopia-php/mongo change (e.g., tag 1.0.4) and update the
composer constraint in composer.json (replace "utopia-php/mongo": "dev-get-host
as 1.0.3") with a stable range such as "utopia-php/mongo": "^1.0.4" or "1.*" so
installations are stable and compatible with consumers' minimum-stability.

Comment on lines +76 to +89
public function testDatabaseHostname(): void
{
/** @var Database $database */
$database = $this->getDatabase();

$host = $database->getAdapter()->getHostname();

if (!$database->getAdapter()->getSupportForHostname()) {
$this->expectNotToPerformAssertions();
return;
}

$this->assertTrue(in_array($host, ['mysql', 'mariadb', 'postgres', 'mongo']));
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟡 Minor

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
# Confirm what each SQL adapter's getHostname() actually returns (PDO helper signature)
ast-grep --pattern 'function getHostname(): string { $$$ }'
rg -nP -C2 '\bgetHostname\s*\(' --type=php

Repository: utopia-php/database

Length of output: 2266


🏁 Script executed:

#!/bin/bash
# Find getSupportForHostname() implementations
rg -nP 'getSupportForHostname' --type=php -A2 -B1

Repository: utopia-php/database

Length of output: 2163


🏁 Script executed:

#!/bin/bash
# Check the test setup to understand which adapters are tested
rg -nP 'getDatabase|setUp|Provider' tests/e2e/Adapter/Scopes/CollectionTests.php -B2 -A2 | head -100

Repository: utopia-php/database

Length of output: 3641


🏁 Script executed:

#!/bin/bash
# Check what values PDO.getHostname() actually returns by examining parseDsn
rg -nP 'parseDsn' src/Database/PDO.php -A20 | head -80

Repository: utopia-php/database

Length of output: 1370


🏁 Script executed:

#!/bin/bash
# Check for test environment setup and what hostnames are configured
fd -type f -name "*.php" "tests/" | xargs rg -l "mysql|mariadb|postgres|mongo" | head -10

Repository: utopia-php/database

Length of output: 644


🏁 Script executed:

#!/bin/bash
# Look for test configuration files (docker-compose, .env, etc)
find . -maxdepth 2 \( -name "docker-compose*" -o -name ".env*" -o -name "phpunit.xml" \) -exec cat {} \;

Repository: utopia-php/database

Length of output: 6764


🏁 Script executed:

#!/bin/bash
# Check how test adapters are instantiated and what DSNs are used
rg -n "MySQLTest|MariaDBTest|PostgresTest|MongoDBTest" tests/e2e/Adapter/ -A5 | head -50

Repository: utopia-php/database

Length of output: 3103


🏁 Script executed:

#!/bin/bash
# Look for test setup/bootstrap files that might configure DSNs
find tests -name "*.php" -path "*/Adapter/*Test.php" | head -3 | xargs head -50

Repository: utopia-php/database

Length of output: 4025


🏁 Script executed:

#!/bin/bash
# Check what PDO.getHostname() actually returns by examining the DSN parsing
# For MySQL/MariaDB, host=mariadb would be parsed and returned as 'mariadb'
rg -n "host=" tests/e2e/Adapter/ --type=php | grep -E "(mysql|postgres|mongo)"

Repository: utopia-php/database

Length of output: 1188


🏁 Script executed:

#!/bin/bash
# Verify what Mongo.getHostname() returns - check the Client class
rg -n "getHost" src/ --type=php -B2 -A5

Repository: utopia-php/database

Length of output: 2800


🏁 Script executed:

#!/bin/bash
# Check PostgreSQL and MongoDB test setups
rg -n "getDatabase" tests/e2e/Adapter/PostgresTest.php -A20 | head -40

Repository: utopia-php/database

Length of output: 1485


🏁 Script executed:

#!/bin/bash
# Check MongoDB getHost implementation
rg -n "getHost\|class Client" src/ -B2 -A3 --type=php | grep -A5 "getHost"

Repository: utopia-php/database

Length of output: 45


🏁 Script executed:

#!/bin/bash
# Verify all test adapter setups for hostname values
rg -n "dbHost|Host.*=" tests/e2e/Adapter/PostgresTest.php tests/e2e/Adapter/MongoDBTest.php

Repository: utopia-php/database

Length of output: 280


Move getHostname() call after the guard and use assertContains() for clarity.

Two issues in this test:

  1. getHostname() is called before the getSupportForHostname() guard. This invokes the API unnecessarily for adapters that don't support hostname (like SQLite). Move the call after the guard—the codebase already uses this pattern elsewhere (e.g., Database.php:9344).

  2. The assertion in_array($host, ['mysql','mariadb','postgres','mongo']) should use assertContains() for a clearer failure message and better semantics. While the checked values match the test environment hostnames, this is more defensive if an unexpected value is returned.

Suggested fix
 public function testDatabaseHostname(): void
 {
     /** `@var` Database $database */
     $database = $this->getDatabase();

-    $host = $database->getAdapter()->getHostname();
-
     if (!$database->getAdapter()->getSupportForHostname()) {
         $this->expectNotToPerformAssertions();
         return;
     }

-    $this->assertTrue(in_array($host, ['mysql', 'mariadb', 'postgres', 'mongo']));
+    $host = $database->getAdapter()->getHostname();
+    $this->assertContains($host, ['mysql', 'mariadb', 'postgres', 'mongo']);
 }
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@tests/e2e/Adapter/Scopes/CollectionTests.php` around lines 76 - 89, In
testDatabaseHostname(), avoid calling getHostname() before checking support:
first call $database->getAdapter()->getSupportForHostname() and bail with
$this->expectNotToPerformAssertions() if false, then call
$database->getAdapter()->getHostname(); replace the current
$this->assertTrue(in_array($host, [...])) with $this->assertContains($host,
['mysql','mariadb','postgres','mongo']) to get clearer failure output and better
semantics (referencing testDatabaseHostname, getAdapter(),
getSupportForHostname(), getHostname(), assertContains(), and
expectNotToPerformAssertions()).

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.

2 participants