diff --git a/app/config/collections/projects.php b/app/config/collections/projects.php index 157422f29e..12f1534377 100644 --- a/app/config/collections/projects.php +++ b/app/config/collections/projects.php @@ -69,6 +69,17 @@ return [ 'signed' => true, 'array' => false, 'filters' => [], + ], + [ + '$id' => ID::custom('status'), + 'type' => Database::VAR_STRING, + 'format' => '', + 'size' => 16, + 'signed' => true, + 'required' => false, + 'default' => null, + 'array' => false, + 'filters' => [], ] ], 'indexes' => [ diff --git a/composer.json b/composer.json index 66498d9f5d..0439d13034 100644 --- a/composer.json +++ b/composer.json @@ -83,7 +83,7 @@ "utopia-php/lock": "0.2.*", "utopia-php/logger": "0.8.*", "utopia-php/messaging": "^1.2", - "utopia-php/migration": "1.14.*", + "utopia-php/migration": "1.16.*", "utopia-php/platform": "^1.0@RC", "utopia-php/pools": "1.*", "utopia-php/span": "3.0.*", diff --git a/composer.lock b/composer.lock index 7e4b64cfab..d337d7e4ea 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "9fb1fb88559be6ad0be7d51248fd86dd", + "content-hash": "50b6e587a70f02a5896790012248f50f", "packages": [ { "name": "adhocore/jwt", @@ -4409,16 +4409,16 @@ }, { "name": "utopia-php/migration", - "version": "1.14.1", + "version": "1.16.0", "source": { "type": "git", "url": "https://github.com/utopia-php/migration.git", - "reference": "926556113c770b3646cabeb042153969e3e0cadf" + "reference": "3bb4428e4fb7a29f120f73f87d0a2519a2a414a4" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/utopia-php/migration/zipball/926556113c770b3646cabeb042153969e3e0cadf", - "reference": "926556113c770b3646cabeb042153969e3e0cadf", + "url": "https://api.github.com/repos/utopia-php/migration/zipball/3bb4428e4fb7a29f120f73f87d0a2519a2a414a4", + "reference": "3bb4428e4fb7a29f120f73f87d0a2519a2a414a4", "shasum": "" }, "require": { @@ -4458,9 +4458,9 @@ ], "support": { "issues": "https://github.com/utopia-php/migration/issues", - "source": "https://github.com/utopia-php/migration/tree/1.14.1" + "source": "https://github.com/utopia-php/migration/tree/1.16.0" }, - "time": "2026-06-30T13:18:56+00:00" + "time": "2026-07-09T05:46:44+00:00" }, { "name": "utopia-php/mongo", diff --git a/src/Appwrite/Migration/Version/V25.php b/src/Appwrite/Migration/Version/V25.php index 800b9b451f..3a997c865d 100644 --- a/src/Appwrite/Migration/Version/V25.php +++ b/src/Appwrite/Migration/Version/V25.php @@ -7,6 +7,8 @@ use Exception; use Throwable; use Utopia\Console; use Utopia\Database\Database; +use Utopia\Database\Document; +use Utopia\Database\Query; class V25 extends Migration { @@ -63,6 +65,32 @@ class V25 extends Migration } $this->dbForProject->purgeCachedCollection($id); break; + + case 'databases': + if ($collectionType === 'projects') { + try { + $this->createAttributeFromCollection($this->dbForProject, $id, 'status'); + } catch (Throwable $th) { + Console::warning("Failed to create attribute \"status\" in collection {$id}: {$th->getMessage()}"); + } + $this->dbForProject->purgeCachedCollection($id); + + // Backfill existing databases so the stored value matches the intended default. + // Materialize the matched documents before updating any of them: updating + // "status" removes rows from the isNull() set, and the offset-based iterator + // would otherwise skip un-processed rows as the filtered set shrinks mid-scan. + $databases = \iterator_to_array($this->documentsIterator($id, [Query::isNull('status')])); + foreach ($databases as $database) { + try { + $this->dbForProject->updateDocument($id, $database->getId(), new Document([ + 'status' => 'ready', + ])); + } catch (Throwable $th) { + Console::warning("Failed to backfill \"status\" for database {$database->getId()}: {$th->getMessage()}"); + } + } + } + break; } } } diff --git a/src/Appwrite/Platform/Modules/Databases/Http/Databases/Create.php b/src/Appwrite/Platform/Modules/Databases/Http/Databases/Create.php index 09b1c274ec..51bef56c47 100644 --- a/src/Appwrite/Platform/Modules/Databases/Http/Databases/Create.php +++ b/src/Appwrite/Platform/Modules/Databases/Http/Databases/Create.php @@ -190,7 +190,8 @@ class Create extends Action 'enabled' => $enabled, 'search' => implode(' ', [$databaseId, $name]), 'type' => $this->getDatabaseType(), - 'database' => $this->getDatabaseDSN($project) + 'database' => $this->getDatabaseDSN($project), + 'status' => 'ready', ])); } catch (DuplicateException) { throw new Exception(Exception::DATABASE_ALREADY_EXISTS, params: [$databaseId]); diff --git a/src/Appwrite/Utopia/Response/Model/Database.php b/src/Appwrite/Utopia/Response/Model/Database.php index df9ad2e1f5..2cbf4ace63 100644 --- a/src/Appwrite/Utopia/Response/Model/Database.php +++ b/src/Appwrite/Utopia/Response/Model/Database.php @@ -4,6 +4,7 @@ namespace Appwrite\Utopia\Response\Model; use Appwrite\Utopia\Response; use Appwrite\Utopia\Response\Model; +use Utopia\Database\Document; class Database extends Model { @@ -46,9 +47,32 @@ class Database extends Model 'default' => 'legacy', 'example' => 'legacy', 'enum' => ['legacy', 'tablesdb', 'documentsdb', 'vectorsdb'], + ]) + ->addRule('status', [ + 'type' => self::TYPE_ENUM, + 'description' => 'Database status. Possible values: `provisioning`, `ready` or `failed`', + 'default' => 'ready', + 'example' => 'ready', + 'enum' => ['provisioning', 'ready', 'failed'], + 'enumSDKName' => 'DatabaseStatus', ]); } + /** + * A null status is treated as ready. + * + * @param Document $document + * @return Document + */ + public function filter(Document $document): Document + { + if (empty($document->getAttribute('status'))) { + $document->setAttribute('status', 'ready'); + } + + return $document; + } + /** * Get Name * diff --git a/tests/e2e/Services/Databases/DatabasesBase.php b/tests/e2e/Services/Databases/DatabasesBase.php index e3efe3bbd9..4beed77479 100644 --- a/tests/e2e/Services/Databases/DatabasesBase.php +++ b/tests/e2e/Services/Databases/DatabasesBase.php @@ -776,6 +776,45 @@ trait DatabasesBase $this->assertEquals($this->getDatabaseType(), $database['body']['type']); } + public function testDatabaseStatus(): void + { + if ($this->getSide() === 'client') { + // Databases are created and read with a server API key. + $this->expectNotToPerformAssertions(); + return; + } + + $headers = [ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + 'x-appwrite-key' => $this->getProject()['apiKey'], + ]; + + /** + * Test for SUCCESS + * A newly created database defaults to the "ready" status. + */ + $database = $this->client->call(Client::METHOD_POST, $this->getApiBasePath(), $headers, [ + 'databaseId' => ID::unique(), + 'name' => 'Status Database', + ]); + + $this->assertEquals(201, $database['headers']['status-code']); + $this->assertArrayHasKey('status', $database['body']); + $this->assertEquals('ready', $database['body']['status']); + + $databaseId = $database['body']['$id']; + + /** + * The status is persisted and returned on read. + */ + $response = $this->client->call(Client::METHOD_GET, $this->getApiBasePath() . '/' . $databaseId, $headers); + + $this->assertEquals(200, $response['headers']['status-code']); + $this->assertArrayHasKey('status', $response['body']); + $this->assertEquals('ready', $response['body']['status']); + } + public function testCreateCollection(): void { $data = $this->setupDatabase();