Skip to content

fix(usage): add missing attributes and indexes in the database adapter setup - #14636

Merged
abnegate merged 1 commit into
mainfrom
fix/usage-setup-attributes
Oct 18, 2026
Merged

abnegate merged 1 commit into
mainfrom
fix/usage-setup-attributes

Conversation

@abnegate

Copy link
Copy Markdown
Member

Problem

Utopia\Usage\Adapter\Database::setup() creates the usage collection and swallows the Duplicate exception when it already exists. Nothing after that compares the existing collection with the declared schema, so a column or index added to the usage schema (for example ordinal) never reaches a collection created by an earlier version. migrate() inherits setup() for this adapter, so it had the same gap.

Fix

In packages/usage/src/Adapter/Database.php, when the collection already exists, setup() reads it and creates every declared attribute it lacks, then every declared index it lacks. Attributes go first because a new index may cover a new attribute. A Duplicate from a concurrent setup() is ignored, so a second run changes nothing.

Tests

packages/usage/tests/E2E/Adapter/DatabaseTest::testSetupAddsTheColumnsAndIndexesAnExistingCollectionLacks creates the collection from the current schema minus ordinal and the indexes on it, which is how a collection created before ordinal looks. It then runs setup() twice, checks that every declared column and index is present with the declared type, size and flags, and checks that an event written afterwards keeps its ordinal.

Before (main) After (this PR)
DatabaseTest::testSetupAddsTheColumnsAndIndexesAnExistingCollectionLacks fails: setup() must add every declared column an existing collection lacks, once (diff shows ordinal missing) passes
DatabaseTest (43 tests, MariaDB) 42 pass, 1 fails 43 pass
packages/usage unit suite 112 pass 112 pass
bin/monorepo check usage (Rector, Pint, PHPStan max) — passes

No API-level E2E is included. server-ce builds its usage connection only on the ClickHouse adapter (src/Appwrite/Usage/Connection.php), so no endpoint reaches the Database adapter. The ClickHouse path already adds missing columns through migrate() (#14553).

🤖 Generated with Claude Code

…r setup

The Database adapter's setup() returned as soon as createCollection()
reported the collection as a duplicate, so a column or index added to the
usage schema never reached a collection created by an earlier version.
setup(), and migrate() through it, now compares the existing collection
with the declared schema and creates what it lacks: attributes first, since
a new index may cover a new attribute. A duplicate from a concurrent setup()
is ignored, so running it again is a no-op.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@luxor-code-review

luxor-code-review Bot commented Oct 18, 2026 •

Copy link
Copy Markdown

Review complete. 🟡 1 medium

💬 Inline comments (1)

  • 🟡 Index creation can fail uncaught on a not-yet-ready attribute — Database.php:137

This PR makes the usage package Database adapter schema-aware on every boot: instead of returning early when the usage collection already exists, setup() now diffs the live collection against the expected schema and adds only what is missing. The e2e test pins the pre-existing-partial-schema path, and the changelog documents the change.

Files Change
packages/usage/src/Adapter/Database.php Adds an addMissing() reconciliation pass that creates missing attributes and indexes on an existing usage collection instead of skipping setup.
packages/usage/tests/E2E/Adapter/DatabaseTest.php New e2e test covering setup against a pre-existing collection that lacks the newer attributes.
packages/usage/CHANGELOG.md Documents the schema-reconciliation behavior change.

Reviewed commit: 2beb6a1

@ghost

ghost commented Oct 18, 2026 •

Copy link
Copy Markdown

🟢 Tier S · Ready to merge

The existing-collection reconciliation is consistent with the database adapter APIs, and the added integration test covers the changed behavior.

The Database usage adapter now reconciles an existing usage collection by adding missing attributes before missing indexes, while retaining the existing collection-creation path. An E2E test verifies idempotent setup, declared schema restoration, and persistence of the ordinal dimension; the package changelog documents the fix.

Verdict New comments Fixed Still open
✅ Approved 0 0 0
📂 Walkthrough · 3
File Change
packages/usage/CHANGELOG.md Documents schema reconciliation for existing usage collections.
packages/usage/src/Adapter/Database.php Adds missing attributes and then indexes after detecting an existing collection.
packages/usage/tests/E2E/Adapter/DatabaseTest.php Adds coverage for repeated setup, restored schema definitions, and ordinal persistence.

Reviewed 2beb6a1 · Details · Comment @hansi-codes review to re-run, or mention @hansi-codes with a question.

@ghost ghost 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

@github-actions

Copy link
Copy Markdown

Security rules

No new WARNING or ERROR findings from security rules.

19 existing findings tracked in .semgrep/baseline.json
  • php.appwrite.guest-write-without-abuse-limit (12)
  • php.appwrite.secret-compare-timing (5)
  • php.appwrite.weak-secret-env-default (2)

Posted by Checks / Rules. Re-runs update this comment in place. Rule details and baseline: .semgrep/README.md.

@luxor-code-review luxor-code-review 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.

Reworks the usage-metrics database adapter setup() to reconcile an existing collection instead of skipping it, backfilling missing attributes/indexes and adding an e2e test.

Key findings

  • 🟡 Index creation can fail uncaught on a not-yet-ready attribute — Database.php:137

Comment thread packages/usage/src/Adapter/Database.php Resolved
@github-actions

Copy link
Copy Markdown

✨ Benchmark results

Comparing main (before) → fix/usage-setup-attributes (after).

Metric Before After Change
🚀 Requests/sec 190.51 187.24 ⚪ -1.7%
⏱️ Latency P50 89.86 ms 91.83 ms ⚪ +2.2%
⏱️ Latency P95 213.01 ms 213.23 ms ⚪ +0.1%
Per-scenario breakdown & investigation details

Metrics below reflect the current branch (after). Δ P95 compares against the base.

Scenario P50 (ms) P95 (ms) Requests RPS Δ P95 (ms)
API total 91.83 213.23 11,628 187.24 +0.21
Account 171.23 334.59 612 10.1 -5.36
TablesDB 88.5 166.95 6,324 103.73 -0.59
Storage 84.84 192.1 3,060 52.34 +6.22
Functions 132.13 249.69 1,632 28.72 +3.57

Top API waits (after)

API request Max wait (ms)
functions.create 506.71
account.name.update 502.97
account.prefs.update 435
tablesdb.rows.create 408.94
functions.delete 386.53

@abnegate
abnegate merged commit c8b9760 into main Oct 18, 2026
72 checks passed
@abnegate
abnegate deleted the fix/usage-setup-attributes branch October 19, 2026 00:03
@ChiragAgg5k ChiragAgg5k mentioned this pull request Oct 21, 2026
6 tasks
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