Repository navigation
fix(usage): add missing attributes and indexes in the database adapter setup - #14636
Conversation
…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]>
|
Review complete. 🟡 1 medium 💬 Inline comments (1)
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.
Reviewed commit: 2beb6a1 |
🟢 Tier S · Ready to merge
The Database usage adapter now reconciles an existing
📂 Walkthrough · 3
Reviewed |
Security rulesNo new WARNING or ERROR findings from security rules. 19 existing findings tracked in
|
There was a problem hiding this comment.
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
✨ Benchmark resultsComparing
Per-scenario breakdown & investigation detailsMetrics below reflect the current branch (after). Δ P95 compares against the base.
Top API waits (after)
|
Problem
Utopia\Usage\Adapter\Database::setup()creates theusagecollection and swallows theDuplicateexception 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 exampleordinal) never reaches a collection created by an earlier version.migrate()inheritssetup()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. ADuplicatefrom a concurrentsetup()is ignored, so a second run changes nothing.Tests
packages/usage/tests/E2E/Adapter/DatabaseTest::testSetupAddsTheColumnsAndIndexesAnExistingCollectionLackscreates the collection from the current schema minusordinaland the indexes on it, which is how a collection created beforeordinallooks. It then runssetup()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 itsordinal.DatabaseTest::testSetupAddsTheColumnsAndIndexesAnExistingCollectionLackssetup() must add every declared column an existing collection lacks, once(diff showsordinalmissing)DatabaseTest(43 tests, MariaDB)packages/usageunit suitebin/monorepo check usage(Rector, Pint, PHPStan max)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 throughmigrate()(#14553).🤖 Generated with Claude Code