TL;DR
In Create.php, there is a TODO to move the v2 metadata schema creation to the Databases background worker. Currently, the metadata collection (database_{sequence}) is created synchronously inside the HTTP request (line 204), blocking the API response until the DDL operation completes. This is inconsistent with how Collections, Attributes, and Indexes are handled (all async via workers).
Detailed Context
The TODO (line 37-38):
// TODO: use database worker for for creating the v2 schema if not present
// it is considered that the v2 metadata schema is already created during server start in the http.php
Source permalink
The synchronous call (line 204):
$this->createMetadataCollection($dbForProject, $database);
Source permalink
What happens today:
- User hits
POST /v1/databases.
- A document is saved to the
databases collection.
createMetadataCollection() runs synchronously — it executes a DDL query (createCollection) on the project database, blocking the HTTP response.
- It also assumes the target database cluster already has the base Utopia "v2 schema" initialized from the
http.php boot script.
Why this is a problem:
- API Latency: DDL I/O blocks the response. Every other schema operation (attributes, indexes, collections) is already async via workers — database creation is the odd one out.
- Assumption Gap: If a dynamically provisioned or externally added database cluster is targeted post-boot, it won't have the v2 base schema, causing failures.
Proposed Solution
-
Create.php: Remove $this->createMetadataCollection(). Enqueue a DatabaseMessage with a new type DATABASE_TYPE_CREATE_DATABASE via $publisherForDatabase (similar to how Delete.php dispatches DATABASE_TYPE_DELETE_DATABASE).
-
Workers/Databases.php: Add a new handler for DATABASE_TYPE_CREATE_DATABASE. Move the createMetadataCollection logic here. Add v2 schema verification on the target DSN before creating the metadata collection.
-
app/init/constants.php: Add DATABASE_TYPE_CREATE_DATABASE constant.
Related Files
I have explored the codebase and would love to work on this refactor. Happy to submit a PR if this approach aligns with the core team's vision!
TL;DR
In
Create.php, there is a TODO to move the v2 metadata schema creation to theDatabasesbackground worker. Currently, the metadata collection (database_{sequence}) is created synchronously inside the HTTP request (line 204), blocking the API response until the DDL operation completes. This is inconsistent with how Collections, Attributes, and Indexes are handled (all async via workers).Detailed Context
The TODO (line 37-38):
The synchronous call (line 204):
What happens today:
POST /v1/databases.databasescollection.createMetadataCollection()runs synchronously — it executes a DDL query (createCollection) on the project database, blocking the HTTP response.http.phpboot script.Why this is a problem:
Proposed Solution
Create.php: Remove$this->createMetadataCollection(). Enqueue aDatabaseMessagewith a new typeDATABASE_TYPE_CREATE_DATABASEvia$publisherForDatabase(similar to howDelete.phpdispatchesDATABASE_TYPE_DELETE_DATABASE).Workers/Databases.php: Add a new handler forDATABASE_TYPE_CREATE_DATABASE. Move thecreateMetadataCollectionlogic here. Add v2 schema verification on the target DSN before creating the metadata collection.app/init/constants.php: AddDATABASE_TYPE_CREATE_DATABASEconstant.Related Files
Create.php(HTTP Action)Workers/Databases.php(Background Worker)Delete.php(Reference for queue dispatch pattern)I have explored the codebase and would love to work on this refactor. Happy to submit a PR if this approach aligns with the core team's vision!