Skip to content

refactor(drizzle): extract resolvePostgresClientConfig for testable URL priority chain#890

Merged
jhaynie merged 1 commit intomainfrom
refactor/drizzle-config-tests
Feb 6, 2026
Merged

refactor(drizzle): extract resolvePostgresClientConfig for testable URL priority chain#890
jhaynie merged 1 commit intomainfrom
refactor/drizzle-config-tests

Conversation

@jhaynie
Copy link
Member

@jhaynie jhaynie commented Feb 6, 2026

Summary

Follow-up to #889 — addresses review feedback on the drizzle config tests.

  • Extracts resolvePostgresClientConfig() as a pure function so the URL priority chain (connection.url > url > connectionString > DATABASE_URL) can be tested directly without mocking the postgres constructor.
  • Rewrites URL priority tests to assert the resolved url value instead of just checking db is defined.
  • Adds new tests: DATABASE_URL fallback, url vs DATABASE_URL precedence, config forwarding (reconnect, onReconnected), and non-mutation of the original connection object.
  • Removes duplicate backward-compat tests that were identical to direct usage tests.
  • Makes all test callbacks async and await close() to avoid dropped promises.

Summary by CodeRabbit

  • New Features
    • Exposed a new PostgreSQL configuration resolver function for explicit connection setup.
    • Established transparent URL priority resolution for database connections.

…RL priority chain

- Extract config resolution logic into resolvePostgresClientConfig() so
  the URL priority chain can be tested directly without mocking
- Rewrite URL priority tests to assert the resolved URL value instead of
  just checking that db is defined
- Add tests for DATABASE_URL fallback, config forwarding, and
  non-mutation of the original connection object
- Remove duplicate backward-compat tests that were identical to direct
  usage tests
- Make all test callbacks async and await close() to avoid dropped
  promises
@coderabbitai
Copy link

coderabbitai bot commented Feb 6, 2026

📝 Walkthrough

Walkthrough

A new internal utility function resolvePostgresClientConfig is introduced to extract and centralize PostgreSQL client configuration logic. This function computes a PostgresConfig from PostgresDrizzleConfig with URL priority resolution and reconnect option handling. The createPostgresDrizzle function is refactored to delegate configuration construction to this utility. Tests are updated to validate the new function.

Changes

Cohort / File(s) Summary
PostgreSQL Configuration Utility
packages/drizzle/src/postgres.ts
Introduces resolvePostgresClientConfig utility function that resolves client configuration with URL priority chain (connection.url > url > connectionString > DATABASE_URL) and attaches reconnect/onReconnected options. Refactors createPostgresDrizzle to delegate configuration construction to this function while preserving existing behavior for onConnect and Drizzle initialization.
Configuration Tests
packages/drizzle/test/config.test.ts
Updates tests to exercise URL resolution logic through resolvePostgresClientConfig instead of inline through createPostgresDrizzle. Adds tests for URL precedence, reconnect forwarding, onReconnected aliasing, input immutability, and environment variable fallback behavior. Converts test cases to async style to reflect potential async nature of client closure.
🚥 Pre-merge checks | ✅ 1
✅ Passed checks (1 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.

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


🧹 Recent nitpick comments
packages/drizzle/src/postgres.ts (2)

12-40: Clean extraction with correct priority chain logic.

The URL resolution logic correctly implements the documented priority chain, the shallow clone prevents caller mutation, and the reconnect/callback forwarding is straightforward.

One minor observation: the TSchema generic on resolvePostgresClientConfig is unused — the return type is PostgresConfig, which doesn't depend on TSchema. It works fine as-is (TypeScript just infers the default), but you could simplify the signature to (config?: PostgresDrizzleConfig<Record<string, unknown>>): PostgresConfig if you prefer.


97-101: Pre-existing: unhandled rejection if onConnect throws.

Not introduced by this PR, but worth noting: waitForConnection().then(() => config.onConnect!()) swallows any error thrown by the callback. Consider adding a .catch() or wrapping in a try/catch inside the .then() to avoid unhandled promise rejections in the future.

packages/drizzle/test/config.test.ts (2)

254-338: Good coverage of the URL priority chain and config forwarding.

The tests are well-structured and clearly verify each level of the priority chain, callback mapping, and immutability. Two optional edge cases you might consider adding:

  1. resolvePostgresClientConfig() with no args (or undefined) — to mirror the createPostgresDrizzle() no-config test on line 232 and confirm it returns {} (or picks up DATABASE_URL if set).
  2. connection.reconnect vs config.reconnect override — when the connection object already contains a reconnect property and config.reconnect is also provided, verifying that config.reconnect wins.

These are minor gaps and can be deferred.


279-292: Env mutation is safe here, but consider a helper to reduce boilerplate.

The save/restore pattern for process.env.DATABASE_URL is repeated in three places (lines 233–250, 279–292, 294–309). A small helper (or beforeEach/afterEach in a nested describe) could reduce duplication and ensure cleanup consistency. Not urgent — just a readability nit.

Also applies to: 294-309

📜 Recent review details

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between aa40796 and b8e5193.

📒 Files selected for processing (2)
  • packages/drizzle/src/postgres.ts
  • packages/drizzle/test/config.test.ts
🧰 Additional context used
📓 Path-based instructions (6)
packages/drizzle/**/*.{ts,tsx}

📄 CodeRabbit inference engine (packages/drizzle/AGENTS.md)

packages/drizzle/**/*.{ts,tsx}: Use createPostgresDrizzle() factory function to create Drizzle ORM instances
Define schemas using Drizzle's pgTable and column types rather than other schema definition methods
Use full TypeScript support with schema inference from Drizzle ORM for type-safe queries
Use drizzleAdapter from better-auth when integrating with @agentuity/auth

Files:

  • packages/drizzle/test/config.test.ts
  • packages/drizzle/src/postgres.ts
packages/drizzle/**/*.{test,spec}.{ts,tsx}

📄 CodeRabbit inference engine (packages/drizzle/AGENTS.md)

Ensure tests run against a PostgreSQL instance and use @agentuity/test-utils for mocking

Files:

  • packages/drizzle/test/config.test.ts
**/*.{ts,tsx,js,jsx,json,md}

📄 CodeRabbit inference engine (AGENTS.md)

Use Prettier for code formatting with tabs (width 3), single quotes, and semicolons

Files:

  • packages/drizzle/test/config.test.ts
  • packages/drizzle/src/postgres.ts
**/*.{ts,tsx}

📄 CodeRabbit inference engine (AGENTS.md)

Use TypeScript in strict mode with ESNext target and bundler moduleResolution

Files:

  • packages/drizzle/test/config.test.ts
  • packages/drizzle/src/postgres.ts
packages/**/*.{ts,tsx}

📄 CodeRabbit inference engine (AGENTS.md)

Use StructuredError from @agentuity/core for error handling

Files:

  • packages/drizzle/test/config.test.ts
  • packages/drizzle/src/postgres.ts
packages/*/test/**/*.{ts,tsx}

📄 CodeRabbit inference engine (AGENTS.md)

packages/*/test/**/*.{ts,tsx}: Place test files in test/ folder, never in src/ or __tests__/
Import from ../src/ in test files
Use @agentuity/test-utils for mocks in tests

Files:

  • packages/drizzle/test/config.test.ts
🧠 Learnings (1)
📚 Learning: 2025-12-21T00:31:41.858Z
Learnt from: jhaynie
Repo: agentuity/sdk PR: 274
File: packages/cli/src/cmd/build/vite/server-bundler.ts:12-41
Timestamp: 2025-12-21T00:31:41.858Z
Learning: In Bun runtime, BuildMessage and ResolveMessage are global types and are not exported from the bun module. Do not import { BuildMessage } from 'bun' or similar; these types are available globally and should be used without import. This applies to all TypeScript files that target the Bun runtime within the repository.

Applied to files:

  • packages/drizzle/test/config.test.ts
  • packages/drizzle/src/postgres.ts
🧬 Code graph analysis (2)
packages/drizzle/test/config.test.ts (1)
packages/drizzle/src/postgres.ts (1)
  • resolvePostgresClientConfig (12-40)
packages/drizzle/src/postgres.ts (2)
packages/drizzle/src/types.ts (1)
  • PostgresDrizzleConfig (10-58)
packages/postgres/src/types.ts (1)
  • PostgresConfig (126-219)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (10)
  • GitHub Check: Playwright E2E Smoke Test
  • GitHub Check: Pack & Upload
  • GitHub Check: Template Integration Tests
  • GitHub Check: Framework Integration Tests (TanStack & Next.js)
  • GitHub Check: Queue CLI Tests
  • GitHub Check: Sandbox CLI Tests
  • GitHub Check: Build
  • GitHub Check: Cloud Deployment Tests
  • GitHub Check: Package Installation & Usage Test
  • GitHub Check: SDK Integration Test Suite
🔇 Additional comments (1)
packages/drizzle/test/config.test.ts (1)

112-251: LGTM on async close handling.

The conversion to async callbacks with await close() in finally blocks is correctly applied across all direct-usage tests, preventing dropped promises.

✏️ Tip: You can disable this entire section by setting review_details to false in your review settings.


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

@github-actions
Copy link

github-actions bot commented Feb 6, 2026

📦 Canary Packages Published

version: 1.0.3-b8e5193

Packages
Package Version URL
@agentuity/server 1.0.3-b8e5193 https://agentuity-sdk-objects.t3.storageapi.dev/npm/1.0.3-b8e5193/agentuity-server-1.0.3-b8e5193.tgz
@agentuity/auth 1.0.3-b8e5193 https://agentuity-sdk-objects.t3.storageapi.dev/npm/1.0.3-b8e5193/agentuity-auth-1.0.3-b8e5193.tgz
@agentuity/drizzle 1.0.3-b8e5193 https://agentuity-sdk-objects.t3.storageapi.dev/npm/1.0.3-b8e5193/agentuity-drizzle-1.0.3-b8e5193.tgz
@agentuity/evals 1.0.3-b8e5193 https://agentuity-sdk-objects.t3.storageapi.dev/npm/1.0.3-b8e5193/agentuity-evals-1.0.3-b8e5193.tgz
@agentuity/cli 1.0.3-b8e5193 https://agentuity-sdk-objects.t3.storageapi.dev/npm/1.0.3-b8e5193/agentuity-cli-1.0.3-b8e5193.tgz
@agentuity/workbench 1.0.3-b8e5193 https://agentuity-sdk-objects.t3.storageapi.dev/npm/1.0.3-b8e5193/agentuity-workbench-1.0.3-b8e5193.tgz
@agentuity/opencode 1.0.3-b8e5193 https://agentuity-sdk-objects.t3.storageapi.dev/npm/1.0.3-b8e5193/agentuity-opencode-1.0.3-b8e5193.tgz
@agentuity/react 1.0.3-b8e5193 https://agentuity-sdk-objects.t3.storageapi.dev/npm/1.0.3-b8e5193/agentuity-react-1.0.3-b8e5193.tgz
@agentuity/runtime 1.0.3-b8e5193 https://agentuity-sdk-objects.t3.storageapi.dev/npm/1.0.3-b8e5193/agentuity-runtime-1.0.3-b8e5193.tgz
@agentuity/postgres 1.0.3-b8e5193 https://agentuity-sdk-objects.t3.storageapi.dev/npm/1.0.3-b8e5193/agentuity-postgres-1.0.3-b8e5193.tgz
@agentuity/schema 1.0.3-b8e5193 https://agentuity-sdk-objects.t3.storageapi.dev/npm/1.0.3-b8e5193/agentuity-schema-1.0.3-b8e5193.tgz
@agentuity/frontend 1.0.3-b8e5193 https://agentuity-sdk-objects.t3.storageapi.dev/npm/1.0.3-b8e5193/agentuity-frontend-1.0.3-b8e5193.tgz
@agentuity/core 1.0.3-b8e5193 https://agentuity-sdk-objects.t3.storageapi.dev/npm/1.0.3-b8e5193/agentuity-core-1.0.3-b8e5193.tgz
Install

Add to your package.json:

{
  "dependencies": {
    "@agentuity/server": "https://agentuity-sdk-objects.t3.storageapi.dev/npm/1.0.3-b8e5193/agentuity-server-1.0.3-b8e5193.tgz",
    "@agentuity/auth": "https://agentuity-sdk-objects.t3.storageapi.dev/npm/1.0.3-b8e5193/agentuity-auth-1.0.3-b8e5193.tgz",
    "@agentuity/drizzle": "https://agentuity-sdk-objects.t3.storageapi.dev/npm/1.0.3-b8e5193/agentuity-drizzle-1.0.3-b8e5193.tgz",
    "@agentuity/evals": "https://agentuity-sdk-objects.t3.storageapi.dev/npm/1.0.3-b8e5193/agentuity-evals-1.0.3-b8e5193.tgz",
    "@agentuity/cli": "https://agentuity-sdk-objects.t3.storageapi.dev/npm/1.0.3-b8e5193/agentuity-cli-1.0.3-b8e5193.tgz",
    "@agentuity/workbench": "https://agentuity-sdk-objects.t3.storageapi.dev/npm/1.0.3-b8e5193/agentuity-workbench-1.0.3-b8e5193.tgz",
    "@agentuity/opencode": "https://agentuity-sdk-objects.t3.storageapi.dev/npm/1.0.3-b8e5193/agentuity-opencode-1.0.3-b8e5193.tgz",
    "@agentuity/react": "https://agentuity-sdk-objects.t3.storageapi.dev/npm/1.0.3-b8e5193/agentuity-react-1.0.3-b8e5193.tgz",
    "@agentuity/runtime": "https://agentuity-sdk-objects.t3.storageapi.dev/npm/1.0.3-b8e5193/agentuity-runtime-1.0.3-b8e5193.tgz",
    "@agentuity/postgres": "https://agentuity-sdk-objects.t3.storageapi.dev/npm/1.0.3-b8e5193/agentuity-postgres-1.0.3-b8e5193.tgz",
    "@agentuity/schema": "https://agentuity-sdk-objects.t3.storageapi.dev/npm/1.0.3-b8e5193/agentuity-schema-1.0.3-b8e5193.tgz",
    "@agentuity/frontend": "https://agentuity-sdk-objects.t3.storageapi.dev/npm/1.0.3-b8e5193/agentuity-frontend-1.0.3-b8e5193.tgz",
    "@agentuity/core": "https://agentuity-sdk-objects.t3.storageapi.dev/npm/1.0.3-b8e5193/agentuity-core-1.0.3-b8e5193.tgz"
  }
}

Or install directly:

bun add https://agentuity-sdk-objects.t3.storageapi.dev/npm/1.0.3-b8e5193/agentuity-server-1.0.3-b8e5193.tgz
bun add https://agentuity-sdk-objects.t3.storageapi.dev/npm/1.0.3-b8e5193/agentuity-auth-1.0.3-b8e5193.tgz
bun add https://agentuity-sdk-objects.t3.storageapi.dev/npm/1.0.3-b8e5193/agentuity-drizzle-1.0.3-b8e5193.tgz
bun add https://agentuity-sdk-objects.t3.storageapi.dev/npm/1.0.3-b8e5193/agentuity-evals-1.0.3-b8e5193.tgz
bun add https://agentuity-sdk-objects.t3.storageapi.dev/npm/1.0.3-b8e5193/agentuity-cli-1.0.3-b8e5193.tgz
bun add https://agentuity-sdk-objects.t3.storageapi.dev/npm/1.0.3-b8e5193/agentuity-workbench-1.0.3-b8e5193.tgz
bun add https://agentuity-sdk-objects.t3.storageapi.dev/npm/1.0.3-b8e5193/agentuity-opencode-1.0.3-b8e5193.tgz
bun add https://agentuity-sdk-objects.t3.storageapi.dev/npm/1.0.3-b8e5193/agentuity-react-1.0.3-b8e5193.tgz
bun add https://agentuity-sdk-objects.t3.storageapi.dev/npm/1.0.3-b8e5193/agentuity-runtime-1.0.3-b8e5193.tgz
bun add https://agentuity-sdk-objects.t3.storageapi.dev/npm/1.0.3-b8e5193/agentuity-postgres-1.0.3-b8e5193.tgz
bun add https://agentuity-sdk-objects.t3.storageapi.dev/npm/1.0.3-b8e5193/agentuity-schema-1.0.3-b8e5193.tgz
bun add https://agentuity-sdk-objects.t3.storageapi.dev/npm/1.0.3-b8e5193/agentuity-frontend-1.0.3-b8e5193.tgz
bun add https://agentuity-sdk-objects.t3.storageapi.dev/npm/1.0.3-b8e5193/agentuity-core-1.0.3-b8e5193.tgz

@jhaynie jhaynie merged commit c87a822 into main Feb 6, 2026
15 checks passed
@jhaynie jhaynie deleted the refactor/drizzle-config-tests branch February 6, 2026 18:21
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