diff --git a/app/config/collections/platform.php b/app/config/collections/platform.php index a90170946b..2aff20aee5 100644 --- a/app/config/collections/platform.php +++ b/app/config/collections/platform.php @@ -415,6 +415,13 @@ $platformCollections = [ 'lengths' => [], 'orders' => [], ], + [ + '$id' => ID::custom('_key_accessedAt'), + 'type' => Database::INDEX_KEY, + 'attributes' => ['accessedAt'], + 'lengths' => [], + 'orders' => [], + ], [ '$id' => ID::custom('_key_teamInternalId'), 'type' => Database::INDEX_KEY, diff --git a/src/Appwrite/Migration/Migration.php b/src/Appwrite/Migration/Migration.php index 77c62bce96..96f16ca57f 100644 --- a/src/Appwrite/Migration/Migration.php +++ b/src/Appwrite/Migration/Migration.php @@ -98,6 +98,7 @@ abstract class Migration '1.9.3' => 'V24', '1.9.4' => 'V24', '1.9.5' => 'V24', + '2.0.0' => 'V25', ]; /** diff --git a/src/Appwrite/Migration/Version/V25.php b/src/Appwrite/Migration/Version/V25.php new file mode 100644 index 0000000000..800b9b451f --- /dev/null +++ b/src/Appwrite/Migration/Version/V25.php @@ -0,0 +1,69 @@ +migrateCollections(); + } + + /** + * Migrate Collections. + * + * @return void + * @throws Exception|Throwable + */ + private function migrateCollections(): void + { + $projectInternalId = $this->project->getSequence(); + + if (empty($projectInternalId)) { + throw new Exception('Project ID is null'); + } + + $collectionType = match ($projectInternalId) { + 'console' => 'console', + default => 'projects', + }; + + $collections = $this->collections[$collectionType]; + + foreach ($collections as $collection) { + $id = $collection['$id']; + + if (empty($id)) { + continue; + } + + Console::log("Migrating collection \"{$id}\""); + + $this->dbForProject->purgeCachedCollection($id); + $this->dbForProject->purgeCachedDocument(Database::METADATA, $id); + + switch ($id) { + case 'projects': + if ($collectionType === 'console') { + try { + $this->createIndexFromCollection($this->dbForProject, $id, '_key_accessedAt'); + } catch (Throwable $th) { + Console::warning("Failed to create index \"_key_accessedAt\" from {$id}: {$th->getMessage()}"); + } + } + $this->dbForProject->purgeCachedCollection($id); + break; + } + } + } +} diff --git a/src/Appwrite/Utopia/Database/Validator/Queries/Projects.php b/src/Appwrite/Utopia/Database/Validator/Queries/Projects.php index 50c9d850f3..95720a5823 100644 --- a/src/Appwrite/Utopia/Database/Validator/Queries/Projects.php +++ b/src/Appwrite/Utopia/Database/Validator/Queries/Projects.php @@ -8,7 +8,8 @@ class Projects extends Base 'name', 'teamId', 'labels', - 'search' + 'search', + 'accessedAt', ]; /** diff --git a/src/Appwrite/Utopia/Database/Validator/Queries/Users.php b/src/Appwrite/Utopia/Database/Validator/Queries/Users.php index 4fdae0e93c..e38cd6c397 100644 --- a/src/Appwrite/Utopia/Database/Validator/Queries/Users.php +++ b/src/Appwrite/Utopia/Database/Validator/Queries/Users.php @@ -15,6 +15,7 @@ class Users extends Base 'phoneVerification', 'labels', 'impersonator', + 'accessedAt', ]; /** diff --git a/tests/e2e/Services/Projects/ProjectsConsoleClientTest.php b/tests/e2e/Services/Projects/ProjectsConsoleClientTest.php index 696fce63cc..f8de004a6a 100644 --- a/tests/e2e/Services/Projects/ProjectsConsoleClientTest.php +++ b/tests/e2e/Services/Projects/ProjectsConsoleClientTest.php @@ -1531,6 +1531,28 @@ final class ProjectsConsoleClientTest extends Scope ], $this->getHeaders())); $this->assertEquals(404, $response['headers']['status-code']); + + $response = $this->client->call(Client::METHOD_GET, '/projects', array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + ], $this->getHeaders()), [ + 'queries' => [ + Query::orderDesc('accessedAt')->toString() + ] + ]); + + $this->assertEquals(200, $response['headers']['status-code']); + + $response = $this->client->call(Client::METHOD_GET, '/projects', array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + ], $this->getHeaders()), [ + 'queries' => [ + Query::orderAsc('accessedAt')->toString() + ] + ]); + + $this->assertEquals(200, $response['headers']['status-code']); } public function testGetProjectUsage(): void diff --git a/tests/e2e/Services/Users/UsersBase.php b/tests/e2e/Services/Users/UsersBase.php index 8adaf9ff47..316f63f4e0 100644 --- a/tests/e2e/Services/Users/UsersBase.php +++ b/tests/e2e/Services/Users/UsersBase.php @@ -1156,6 +1156,30 @@ trait UsersBase $this->assertEquals($user['body']['code'], 404); $this->assertEquals($user['body']['message'], 'User with the requested ID could not be found.'); $this->assertEquals($user['body']['type'], 'user_not_found'); + + $response = $this->client->call(Client::METHOD_GET, '/users', array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + ], $this->getHeaders()), [ + 'queries' => [ + Query::orderDesc('accessedAt')->toString() + ] + ]); + + $this->assertEquals(200, $response['headers']['status-code']); + $this->assertNotEmpty($response['body']['users']); + + $response = $this->client->call(Client::METHOD_GET, '/users', array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + ], $this->getHeaders()), [ + 'queries' => [ + Query::orderAsc('accessedAt')->toString() + ] + ]); + + $this->assertEquals(200, $response['headers']['status-code']); + $this->assertNotEmpty($response['body']['users']); } public function testListUserMemberships(): void