From f06af411badfb757aa05cc8a18626519c65aa585 Mon Sep 17 00:00:00 2001 From: ArnabChatterjee20k Date: Tue, 5 Aug 2025 16:48:05 +0530 Subject: [PATCH] Add 'type' attribute to database creation and update tests --- app/config/collections/projects.php | 9 ++++++ .../Databases/Http/Databases/Create.php | 4 ++- .../Utopia/Response/Model/Database.php | 6 ++++ .../Databases/Grids/DatabasesBase.php | 28 +++++++++++++++++++ .../Databases/Legacy/DatabasesBase.php | 28 +++++++++++++++++++ 5 files changed, 74 insertions(+), 1 deletion(-) diff --git a/app/config/collections/projects.php b/app/config/collections/projects.php index ac14421382..3c840094ec 100644 --- a/app/config/collections/projects.php +++ b/app/config/collections/projects.php @@ -51,6 +51,15 @@ return [ 'default' => null, 'array' => false, ], + [ + '$id' => ID::custom('type'), + 'type' => Database::VAR_STRING, + 'size' => 128, + 'required' => true, + 'signed' => true, + 'array' => false, + 'filters' => [], + ], ], 'indexes' => [ [ diff --git a/src/Appwrite/Platform/Modules/Databases/Http/Databases/Create.php b/src/Appwrite/Platform/Modules/Databases/Http/Databases/Create.php index 9b52b482bb..487b1fb420 100644 --- a/src/Appwrite/Platform/Modules/Databases/Http/Databases/Create.php +++ b/src/Appwrite/Platform/Modules/Databases/Http/Databases/Create.php @@ -80,13 +80,14 @@ class Create extends Action ->param('databaseId', '', new CustomId(), 'Unique Id. Choose a custom ID or generate a random ID with `ID.unique()`. Valid chars are a-z, A-Z, 0-9, period, hyphen, and underscore. Can\'t start with a special char. Max length is 36 chars.') ->param('name', '', new Text(128), 'Database name. Max length: 128 chars.') ->param('enabled', true, new Boolean(), 'Is the database enabled? When set to \'disabled\', users cannot access the database but Server SDKs with an API key can still read and write to the database. No data is lost when this is toggled.', true) + ->param('type', 'mysql', new Text(128), 'Database type.', true) ->inject('response') ->inject('dbForProject') ->inject('queueForEvents') ->callback($this->action(...)); } - public function action(string $databaseId, string $name, bool $enabled, UtopiaResponse $response, Database $dbForProject, Event $queueForEvents): void + public function action(string $databaseId, string $name, bool $enabled, string $type, UtopiaResponse $response, Database $dbForProject, Event $queueForEvents): void { $databaseId = $databaseId == 'unique()' ? ID::unique() : $databaseId; @@ -96,6 +97,7 @@ class Create extends Action 'name' => $name, 'enabled' => $enabled, 'search' => implode(' ', [$databaseId, $name]), + 'type' => $type ])); } catch (DuplicateException) { throw new Exception(Exception::DATABASE_ALREADY_EXISTS); diff --git a/src/Appwrite/Utopia/Response/Model/Database.php b/src/Appwrite/Utopia/Response/Model/Database.php index 90b4ac8cb4..61e435cd9f 100644 --- a/src/Appwrite/Utopia/Response/Model/Database.php +++ b/src/Appwrite/Utopia/Response/Model/Database.php @@ -40,6 +40,12 @@ class Database extends Model 'default' => true, 'example' => false, ]) + ->addRule('type', [ + 'type' => self::TYPE_STRING, + 'description' => 'Database type.', + 'default' => 'mysql', + 'example' => 'mysql', + ]) ; } diff --git a/tests/e2e/Services/Databases/Grids/DatabasesBase.php b/tests/e2e/Services/Databases/Grids/DatabasesBase.php index 77c5a958ac..a6a0e704fb 100644 --- a/tests/e2e/Services/Databases/Grids/DatabasesBase.php +++ b/tests/e2e/Services/Databases/Grids/DatabasesBase.php @@ -32,6 +32,34 @@ trait DatabasesBase $this->assertNotEmpty($database['body']['$id']); $this->assertEquals(201, $database['headers']['status-code']); $this->assertEquals('Test Database', $database['body']['name']); + $this->assertEquals('mysql', $database['body']['type']); + + // testing to create a database with type + $database2 = $this->client->call(Client::METHOD_POST, '/databases', [ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + 'x-appwrite-key' => $this->getProject()['apiKey'] + ], [ + 'databaseId' => ID::unique(), + 'name' => 'Test Database with type', + 'type' => 'mongodb' + ]); + + $this->assertNotEmpty($database2['body']['$id']); + $this->assertEquals(201, $database2['headers']['status-code']); + $this->assertEquals('Test Database with type', $database2['body']['name']); + $this->assertEquals('mongodb', $database2['body']['type']); + + // cleanup(for database2) + $databaseId = $database2['body']['$id']; + + $response = $this->client->call(Client::METHOD_DELETE, '/databases/' . $databaseId, [ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + 'x-appwrite-key' => $this->getProject()['apiKey'] + ]); + + $this->assertEquals(204, $response['headers']['status-code']); return ['databaseId' => $database['body']['$id']]; } diff --git a/tests/e2e/Services/Databases/Legacy/DatabasesBase.php b/tests/e2e/Services/Databases/Legacy/DatabasesBase.php index 2e263f9699..838948d57e 100644 --- a/tests/e2e/Services/Databases/Legacy/DatabasesBase.php +++ b/tests/e2e/Services/Databases/Legacy/DatabasesBase.php @@ -32,6 +32,34 @@ trait DatabasesBase $this->assertNotEmpty($database['body']['$id']); $this->assertEquals(201, $database['headers']['status-code']); $this->assertEquals('Test Database', $database['body']['name']); + $this->assertEquals('mysql', $database['body']['type']); + + // testing to create a database with type + $database2 = $this->client->call(Client::METHOD_POST, '/databases', [ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + 'x-appwrite-key' => $this->getProject()['apiKey'] + ], [ + 'databaseId' => ID::unique(), + 'name' => 'Test Database with type', + 'type' => 'mongodb' + ]); + + $this->assertNotEmpty($database2['body']['$id']); + $this->assertEquals(201, $database2['headers']['status-code']); + $this->assertEquals('Test Database with type', $database2['body']['name']); + $this->assertEquals('mongodb', $database2['body']['type']); + + // cleanup(for database2) + $databaseId = $database2['body']['$id']; + + $response = $this->client->call(Client::METHOD_DELETE, '/databases/' . $databaseId, [ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + 'x-appwrite-key' => $this->getProject()['apiKey'] + ]); + + $this->assertEquals(204, $response['headers']['status-code']); return ['databaseId' => $database['body']['$id']]; }