Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions src/Database/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -251,8 +251,7 @@ class Database
],
[
'$id' => '$tenant',
'type' => self::VAR_INTEGER,
//'type' => self::VAR_ID, // Inconsistency with other VAR_ID since this is an INT
'type' => self::VAR_ID,
'size' => 0,
Comment on lines 253 to 255
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟠 Major

INTERNAL_ATTRIBUTES now says ID, but SQL _tenant DDL is still hardcoded as INT.

This change at Line 254 does not propagate to MariaDB table creation, where _tenant is still INT(11) UNSIGNED (src/Database/Adapter/MariaDB.php:161-186, bypassing getSQLType). That leaves a schema/metadata mismatch and keeps _tenant narrower than VAR_ID semantics.

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

In `@src/Database/Database.php` around lines 253 - 255, The metadata now declares
INTERNAL_ATTRIBUTES['_tenant'] as type VAR_ID but the MariaDB table DDL in
Adapter\MariaDB still hardcodes `_tenant` as INT(11) UNSIGNED, causing a
schema/metadata mismatch; update the CREATE TABLE column generation in the
MariaDB adapter (look for the code that builds the `_tenant` column in
src/Database/Adapter/MariaDB.php) to derive the SQL type via getSQLType(...)
rather than using a hardcoded INT; specifically, replace the INT(11) UNSIGNED
usage with a call to $this->getSQLType(Database::VAR_ID) (or the equivalent call
available in that class) so `_tenant` follows VAR_ID semantics, and ensure any
size/unsigned attributes or default/null handling produced by getSQLType are
preserved.

⚠️ Potential issue | 🔴 Critical

Changing $tenant to VAR_ID can break tenant matching for int tenants.

At Line 254, VAR_ID is cast to string in casting() (Line 8872+), while tenant checks use strict !== (for example, Line 1859 and Line 1895). setTenant(1) can then mismatch against stored "1" and incorrectly return “Collection not found”.

💡 Safe immediate fix
-            'type' => self::VAR_ID,
+            'type' => self::VAR_INTEGER,
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
'$id' => '$tenant',
'type' => self::VAR_INTEGER,
//'type' => self::VAR_ID, // Inconsistency with other VAR_ID since this is an INT
'type' => self::VAR_ID,
'size' => 0,
'$id' => '$tenant',
'type' => self::VAR_INTEGER,
'size' => 0,
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/Database/Database.php` around lines 253 - 255, The mapping that replaces
the tenant's actual type with self::VAR_ID causes integer tenants to be stored
as strings and later mismatched by strict checks in setTenant(); modify the code
that builds the tenant field (the array containing '$id' => '$tenant', 'type' =>
...) to preserve the tenant's native type instead of always using self::VAR_ID —
e.g., detect is_int($tenant) and set 'type' to the integer type constant (or
otherwise choose the proper VAR_* constant) and ensure the stored value remains
$tenant (so casting() and comparisons in setTenant() see the same type).

'required' => false,
'default' => null,
Expand Down Expand Up @@ -6055,7 +6054,7 @@ public function updateDocument(string $collection, string $id, Document $documen
$document['$createdAt'] = ($createdAt === null || !$this->preserveDates) ? $old->getCreatedAt() : $createdAt;

if ($this->adapter->getSharedTables()) {
$document['$tenant'] = $old->getTenant(); // Make sure user doesn't switch tenant
$document['$tenant'] = $old->getAttribute('$tenant'); // Make sure user doesn't switch tenant
}
$document = new Document($document);

Expand Down
10 changes: 7 additions & 3 deletions src/Database/Validator/Sequence.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,17 @@ public function isValid($value): bool
return false;
}

if (!\is_string($value)) {
if (!\is_string($value) && !\is_int($value)) {
return false;
}

switch ($this->idAttributeType) {
case Database::VAR_UUID7: //UUID7
return preg_match('/^[a-f0-9]{8}-[a-f0-9]{4}-7[a-f0-9]{3}-[89ab][a-f0-9]{3}-[a-f0-9]{12}$/i', $value) === 1;
case Database::VAR_UUID7:
if (\is_string($value) && preg_match('/^[a-f0-9]{8}-[a-f0-9]{4}-7[a-f0-9]{3}-[89ab][a-f0-9]{3}-[a-f0-9]{12}$/i', $value) === 1) {
return true;
}
// Also accept integer IDs (e.g. $tenant may be an integer in any adapter)
// no break
case Database::VAR_INTEGER:
$start = ($this->primary) ? 1 : 0;
$validator = new Range($start, Database::MAX_BIG_INT, Database::VAR_INTEGER);
Expand Down
6 changes: 3 additions & 3 deletions src/Database/Validator/Structure.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,11 @@ class Structure extends Validator
],
[
'$id' => '$tenant',
'type' => Database::VAR_INTEGER, // ? VAR_ID
'size' => 8,
'type' => Database::VAR_ID,
'size' => 0,
'required' => false,
'default' => null,
'signed' => false,
'signed' => true,
'array' => false,
'filters' => [],
],
Expand Down
Loading