Skip to content

fix(databases): restore the column type guard on attribute updates - #13536

Merged
HarshMN2345 merged 3 commits into
mainfrom
fix/column-type-guard-parentheses
Sep 8, 2026
Merged

HarshMN2345 merged 3 commits into
mainfrom
fix/column-type-guard-parentheses

Conversation

@HarshMN2345

@HarshMN2345 HarshMN2345 commented Sep 8, 2026 •

Copy link
Copy Markdown
Member

Relates to #12969

What does this PR do?

Both type checks in the shared attribute/column update base closed their parentheses one token early:

if ($attribute->getAttribute(('type') !== $type)) {

That passes the comparison as the argument. getAttribute() takes a string, the file has no declare(strict_types=1), so PHP coerces the bool to '1' or '' — neither is a key on the attribute, getAttribute() returns its null default, and if (null) is never true:

$d = new Document(['type' => 'string']);
$d->getAttribute(('type') !== 'boolean');   // NULL   <- guard never fires
$d->getAttribute('type') !== 'boolean';     // true   <- intended

So the guard that rejects "update this column as a boolean when it is really a string" never fired, on any of the update endpoints that funnel through this base. Instead of a clean 400 column_type_invalid / attribute_type_invalid, the request proceeded and handed a mistyped default down to Database::updateAttribute(). The same slip is in the adjacent string-filter check.

Relationship to #12969

I could not reproduce the reported xdefault: false → NULL as a falsy-drop bug, and I traced every hop looking for one: the param is bound with array_key_exists rather than truthiness, Nullable(Boolean) accepts false, both guards in the create path already use isset() (the 2022 fix in d45464c still stands), the casting filter round-trips false/0/0.0/"" losslessly, and the worker guards on !== null. Same result for every column type on both the /v1/databases and /v1/tablesdb surfaces.

What is real in that report is that adding a column never backfills rows that already exist — the physical column is created without a SQL default and reads do not apply one. That is identical for true and false, so it is not a boolean bug, and it is a product decision (backfilling is expensive on large tables) rather than something to fix here.

This PR ships the one genuine defect the investigation did turn up in that code path.

Test Plan

tests/e2e/Services/Databases/DatabasesBase::testUpdateAttributeRejectsMismatchedType creates a string column and updates it through the boolean endpoint, expecting 400 and the surface's own error type. It lives in the shared base, so it runs for the legacy, DocumentsDB and TablesDB suites.

Both type checks in updateAttribute() closed their parentheses one token early,
so the comparison was evaluated as the argument:

    $attribute->getAttribute(('type') !== $type)

That passes a bool where a key is expected. Without strict_types PHP coerces it
to '1' or '', neither of which is a key on the attribute, so getAttribute()
returned null and the guard never fired - on any of the update endpoints that
funnel through this base.

The result was that updating a column through the wrong type's endpoint, or
changing the filter of a string column, skipped validation instead of returning
column_type_invalid, and handed a mistyped default to the database adapter.
@greptile-apps

greptile-apps Bot commented Sep 8, 2026 •

Copy link
Copy Markdown
Contributor

Greptile Summary

Restores attribute update validation so requests must use both the correct column type and, for strings, the correct persisted format.

  • Corrects the misplaced parentheses in the type guard.
  • Compares string endpoints against the persisted format discriminator, normalizing plain strings to an empty format.
  • Adds regression coverage for mismatched types, mismatched string formats, and valid formatted/plain string updates.

Confidence Score: 5/5

The PR appears safe to merge; the current guards match attribute persistence and the resolved previous findings are covered by the expanded tests.

No new actionable failures remain. Both previous threads are resolved, and the current code fixes the incorrect field comparison while adding direct coverage for the type and format paths.

Important Files Changed

Filename Overview
src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Attributes/Action.php Correctly restores type validation and aligns formatted-string validation with the persisted format field.
tests/e2e/Services/Databases/DatabasesBase.php Adds coverage for rejected cross-type and cross-format updates and successful matching updates.

Reviews (3): Last reviewed commit: "Merge branch 'main' into fix/column-type..." | Re-trigger Greptile

Restoring the parentheses alone made the second guard read 'filter', which is
never stored on an attribute - the create path persists the discriminator as
'format', empty for a plain string. Every email, URL, IP and enum update would
have been rejected.

Compare against 'format', treating the plain string endpoint's absent filter as
the empty string it is stored as, and cover both directions in the test.
@github-actions

github-actions Bot commented Sep 8, 2026 •

Copy link
Copy Markdown

✨ Benchmark results

Comparing main (before) → fix/column-type-guard-parentheses (after).

Metric Before After Change
🚀 Requests/sec 428.29 441.08 ⚪ +3%
⏱️ Latency P50 40.57 ms 39.47 ms ⚪ -2.7%
⏱️ Latency P95 96 ms 91.51 ms ⚪ -4.7%
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 39.47 91.51 27,075 441.08 -4.49
Account 77.37 132.27 1,425 23.72 -10.24
TablesDB 38.23 69.84 14,725 242.3 -2.04
Storage 36.04 82.39 7,125 118.69 -2.69
Functions 59.91 108.15 3,800 64 -7.63

Top API waits (after)

API request Max wait (ms)
account.name.update 237.17
storage.buckets.create 192.17
functions.create 191.78
functions.variables.update 186.48
storage.files.create 174.68

@HarshMN2345
HarshMN2345 merged commit da59c14 into main Sep 8, 2026
6 checks passed
@ChiragAgg5k ChiragAgg5k mentioned this pull request Sep 10, 2026
2 of 4 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.

2 participants