From d91e3543c83e0a9b45b47ee65e7b1cd2e4db1c81 Mon Sep 17 00:00:00 2001 From: Jake Barnby Date: Fri, 13 Mar 2026 19:13:07 +1300 Subject: [PATCH] (fix): use adapter ID type to determine tenant value for MongoDB compatibility --- app/cli.php | 20 ++++++++++++--- app/init/resources.php | 20 ++++++++++++--- app/realtime.php | 5 +++- app/worker.php | 25 +++++++++++++++---- composer.lock | 12 ++++----- .../Modules/Projects/Http/Projects/Create.php | 8 +++++- src/Appwrite/Platform/Workers/StatsUsage.php | 6 ++++- 7 files changed, 74 insertions(+), 22 deletions(-) diff --git a/app/cli.php b/app/cli.php index 052643f004..2a0df79b21 100644 --- a/app/cli.php +++ b/app/cli.php @@ -161,9 +161,12 @@ $setResource('getProjectDB', function (Group $pools, Database $dbForPlatform, $c $sharedTables = \explode(',', System::getEnv('_APP_DATABASE_SHARED_TABLES', '')); if (\in_array($dsn->getHost(), $sharedTables)) { + $tenant = $database->getIdAttributeType() === Database::VAR_INTEGER + ? (int)$project->getSequence() + : $project->getSequence(); $database ->setSharedTables(true) - ->setTenant((int)$project->getSequence()) + ->setTenant($tenant) ->setNamespace($dsn->getParam('namespace')); } else { $database @@ -182,9 +185,12 @@ $setResource('getProjectDB', function (Group $pools, Database $dbForPlatform, $c $sharedTables = \explode(',', System::getEnv('_APP_DATABASE_SHARED_TABLES', '')); if (\in_array($dsn->getHost(), $sharedTables)) { + $tenant = $database->getIdAttributeType() === Database::VAR_INTEGER + ? (int)$project->getSequence() + : $project->getSequence(); $database ->setSharedTables(true) - ->setTenant((int)$project->getSequence()) + ->setTenant($tenant) ->setNamespace($dsn->getParam('namespace')); } else { $database @@ -208,7 +214,10 @@ $setResource('getLogsDB', function (Group $pools, Cache $cache, Authorization $a return function (?Document $project = null) use ($pools, $cache, $database, $authorization) { if ($database !== null && $project !== null && !$project->isEmpty() && $project->getId() !== 'console') { - $database->setTenant((int)$project->getSequence()); + $tenant = $database->getIdAttributeType() === Database::VAR_INTEGER + ? (int)$project->getSequence() + : $project->getSequence(); + $database->setTenant($tenant); return $database; } @@ -225,7 +234,10 @@ $setResource('getLogsDB', function (Group $pools, Cache $cache, Authorization $a // set tenant if ($project !== null && !$project->isEmpty() && $project->getId() !== 'console') { - $database->setTenant((int)$project->getSequence()); + $tenant = $database->getIdAttributeType() === Database::VAR_INTEGER + ? (int)$project->getSequence() + : $project->getSequence(); + $database->setTenant($tenant); } return $database; diff --git a/app/init/resources.php b/app/init/resources.php index d5486c2a49..c503a9d511 100644 --- a/app/init/resources.php +++ b/app/init/resources.php @@ -582,9 +582,12 @@ Http::setResource('dbForProject', function (Group $pools, Database $dbForPlatfor $sharedTables = \explode(',', System::getEnv('_APP_DATABASE_SHARED_TABLES', '')); if (\in_array($dsn->getHost(), $sharedTables)) { + $tenant = $database->getIdAttributeType() === Database::VAR_INTEGER + ? (int) $project->getSequence() + : $project->getSequence(); $database ->setSharedTables(true) - ->setTenant((int) $project->getSequence()) + ->setTenant($tenant) ->setNamespace($dsn->getParam('namespace')); } else { $database @@ -850,9 +853,12 @@ Http::setResource('getProjectDB', function (Group $pools, Database $dbForPlatfor $sharedTables = \explode(',', System::getEnv('_APP_DATABASE_SHARED_TABLES', '')); if (\in_array($dsn->getHost(), $sharedTables)) { + $tenant = $database->getIdAttributeType() === Database::VAR_INTEGER + ? (int) $project->getSequence() + : $project->getSequence(); $database ->setSharedTables(true) - ->setTenant((int) $project->getSequence()) + ->setTenant($tenant) ->setNamespace($dsn->getParam('namespace')); } else { $database @@ -882,7 +888,10 @@ Http::setResource('getLogsDB', function (Group $pools, Cache $cache, Authorizati return function (?Document $project = null) use ($pools, $cache, $authorization, &$database) { if ($database !== null && $project !== null && !$project->isEmpty() && $project->getId() !== 'console') { - $database->setTenant((int) $project->getSequence()); + $tenant = $database->getIdAttributeType() === Database::VAR_INTEGER + ? (int) $project->getSequence() + : $project->getSequence(); + $database->setTenant($tenant); return $database; } @@ -899,7 +908,10 @@ Http::setResource('getLogsDB', function (Group $pools, Cache $cache, Authorizati // set tenant if ($project !== null && !$project->isEmpty() && $project->getId() !== 'console') { - $database->setTenant((int) $project->getSequence()); + $tenant = $database->getIdAttributeType() === Database::VAR_INTEGER + ? (int) $project->getSequence() + : $project->getSequence(); + $database->setTenant($tenant); } return $database; diff --git a/app/realtime.php b/app/realtime.php index 5addb2a78f..8d3c1e835a 100644 --- a/app/realtime.php +++ b/app/realtime.php @@ -113,9 +113,12 @@ if (!function_exists('getProjectDB')) { $sharedTables = \explode(',', System::getEnv('_APP_DATABASE_SHARED_TABLES', '')); if (\in_array($dsn->getHost(), $sharedTables)) { + $tenant = $database->getIdAttributeType() === Database::VAR_INTEGER + ? (int)$project->getSequence() + : $project->getSequence(); $database ->setSharedTables(true) - ->setTenant((int)$project->getSequence()) + ->setTenant($tenant) ->setNamespace($dsn->getParam('namespace')); } else { $database diff --git a/app/worker.php b/app/worker.php index 2ee1803ddc..fa4a534851 100644 --- a/app/worker.php +++ b/app/worker.php @@ -109,9 +109,12 @@ Server::setResource('dbForProject', function (Cache $cache, Registry $register, $sharedTables = \explode(',', System::getEnv('_APP_DATABASE_SHARED_TABLES', '')); if (\in_array($dsn->getHost(), $sharedTables)) { + $tenant = $database->getIdAttributeType() === Database::VAR_INTEGER + ? (int)$project->getSequence() + : $project->getSequence(); $database ->setSharedTables(true) - ->setTenant((int)$project->getSequence()) + ->setTenant($tenant) ->setNamespace($dsn->getParam('namespace')); } else { $database @@ -149,9 +152,12 @@ Server::setResource('getProjectDB', function (Group $pools, Database $dbForPlatf $sharedTables = \explode(',', System::getEnv('_APP_DATABASE_SHARED_TABLES', '')); if (\in_array($dsn->getHost(), $sharedTables)) { + $tenant = $database->getIdAttributeType() === Database::VAR_INTEGER + ? (int)$project->getSequence() + : $project->getSequence(); $database ->setSharedTables(true) - ->setTenant((int)$project->getSequence()) + ->setTenant($tenant) ->setNamespace($dsn->getParam('namespace')); } else { $database @@ -171,9 +177,12 @@ Server::setResource('getProjectDB', function (Group $pools, Database $dbForPlatf $sharedTables = \explode(',', System::getEnv('_APP_DATABASE_SHARED_TABLES', '')); if (\in_array($dsn->getHost(), $sharedTables)) { + $tenant = $database->getIdAttributeType() === Database::VAR_INTEGER + ? (int)$project->getSequence() + : $project->getSequence(); $database ->setSharedTables(true) - ->setTenant((int)$project->getSequence()) + ->setTenant($tenant) ->setNamespace($dsn->getParam('namespace')); } else { $database @@ -195,7 +204,10 @@ Server::setResource('getLogsDB', function (Group $pools, Cache $cache, Authoriza $database = null; return function (?Document $project = null) use ($pools, $cache, $database, $authorization) { if ($database !== null && $project !== null && !$project->isEmpty() && $project->getId() !== 'console') { - $database->setTenant((int)$project->getSequence()); + $tenant = $database->getIdAttributeType() === Database::VAR_INTEGER + ? (int)$project->getSequence() + : $project->getSequence(); + $database->setTenant($tenant); return $database; } @@ -212,7 +224,10 @@ Server::setResource('getLogsDB', function (Group $pools, Cache $cache, Authoriza // set tenant if ($project !== null && !$project->isEmpty() && $project->getId() !== 'console') { - $database->setTenant((int)$project->getSequence()); + $tenant = $database->getIdAttributeType() === Database::VAR_INTEGER + ? (int)$project->getSequence() + : $project->getSequence(); + $database->setTenant($tenant); } return $database; diff --git a/composer.lock b/composer.lock index 3f4f935382..28c2901006 100644 --- a/composer.lock +++ b/composer.lock @@ -3850,16 +3850,16 @@ }, { "name": "utopia-php/database", - "version": "5.3.9", + "version": "5.3.10", "source": { "type": "git", "url": "https://github.com/utopia-php/database.git", - "reference": "503f13d56c3a76c417f245a9474c0a3ed4c89ed2" + "reference": "1c768ad629984af7f6dbfbbb9e2e922f8cdfb3eb" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/utopia-php/database/zipball/503f13d56c3a76c417f245a9474c0a3ed4c89ed2", - "reference": "503f13d56c3a76c417f245a9474c0a3ed4c89ed2", + "url": "https://api.github.com/repos/utopia-php/database/zipball/1c768ad629984af7f6dbfbbb9e2e922f8cdfb3eb", + "reference": "1c768ad629984af7f6dbfbbb9e2e922f8cdfb3eb", "shasum": "" }, "require": { @@ -3902,9 +3902,9 @@ ], "support": { "issues": "https://github.com/utopia-php/database/issues", - "source": "https://github.com/utopia-php/database/tree/5.3.9" + "source": "https://github.com/utopia-php/database/tree/5.3.10" }, - "time": "2026-03-12T13:17:43+00:00" + "time": "2026-03-13T06:10:41+00:00" }, { "name": "utopia-php/detector", diff --git a/src/Appwrite/Platform/Modules/Projects/Http/Projects/Create.php b/src/Appwrite/Platform/Modules/Projects/Http/Projects/Create.php index 1dd9487ec7..86e078520d 100644 --- a/src/Appwrite/Platform/Modules/Projects/Http/Projects/Create.php +++ b/src/Appwrite/Platform/Modules/Projects/Http/Projects/Create.php @@ -218,9 +218,15 @@ class Create extends Action $dbForProject->setDatabase(APP_DATABASE); if ($sharedTables) { + $tenant = null; + if ($sharedTablesV1) { + $tenant = $dbForProject->getIdAttributeType() === Database::VAR_INTEGER + ? (int)$project->getSequence() + : $project->getSequence(); + } $dbForProject ->setSharedTables(true) - ->setTenant($sharedTablesV1 ? (int)$project->getSequence() : null) + ->setTenant($tenant) ->setNamespace($dsn->getParam('namespace')); } else { $dbForProject diff --git a/src/Appwrite/Platform/Workers/StatsUsage.php b/src/Appwrite/Platform/Workers/StatsUsage.php index 07051d1f15..836185132f 100644 --- a/src/Appwrite/Platform/Workers/StatsUsage.php +++ b/src/Appwrite/Platform/Workers/StatsUsage.php @@ -479,7 +479,11 @@ class StatsUsage extends Action } } $documentClone = clone $stat; - $documentClone->setAttribute('$tenant', (int) $project->getSequence()); + $dbForLogs = ($this->getLogsDB)(); + $tenant = $dbForLogs->getIdAttributeType() === Database::VAR_INTEGER + ? (int) $project->getSequence() + : $project->getSequence(); + $documentClone->setAttribute('$tenant', $tenant); $this->statDocuments[] = $documentClone; }