diff --git a/app/controllers/api/database.php b/app/controllers/api/database.php index f9bbaeee60..13b419eb7a 100644 --- a/app/controllers/api/database.php +++ b/app/controllers/api/database.php @@ -6,6 +6,7 @@ use Utopia\Validator\Boolean; use Utopia\Validator\Numeric; use Utopia\Validator\Range; use Utopia\Validator\WhiteList; +use Utopia\Validator\Wildcard; use Utopia\Validator\Text; use Utopia\Validator\ArrayList; use Utopia\Validator\JSON; @@ -246,12 +247,13 @@ App::post('/v1/database/collections/:collectionId/attributes') ->param('type', null, new Text(256), 'Attribute type.') ->param('size', null, new Numeric(), 'Attribute size for text attributes, in number of characters. For integers, floats, or bools, use 0.') ->param('required', null, new Boolean(), 'Is attribute required?') + ->param('default', null, new Wildcard(), 'Default value for attribute when not provided. Cannot be set when attribute is required.', true) ->param('array', false, new Boolean(), 'Is attribute an array?', true) ->inject('response') ->inject('dbForExternal') ->inject('database') ->inject('audits') - ->action(function ($collectionId, $id, $type, $size, $required, $array, $response, $dbForExternal, $database, $audits) { + ->action(function ($collectionId, $id, $type, $size, $required, $default, $array, $response, $dbForExternal, $database, $audits) { /** @var Appwrite\Utopia\Response $response */ /** @var Utopia\Database\Database $dbForExternal*/ /** @var Appwrite\Event\Event $database */ @@ -267,7 +269,7 @@ App::post('/v1/database/collections/:collectionId/attributes') $signed = true; $filters = []; - $success = $dbForExternal->addAttributeInQueue($collectionId, $id, $type, $size, $required, $signed, $array, $filters); + $success = $dbForExternal->addAttributeInQueue($collectionId, $id, $type, $size, $required, $default, $signed, $array, $filters); // Database->addAttributeInQueue() does not return a document // So we need to create one for the response @@ -279,6 +281,7 @@ App::post('/v1/database/collections/:collectionId/attributes') 'type' => $type, 'size' => $size, 'required' => $required, + 'default' => $default, 'signed' => $signed, 'array' => $array, 'filters' => $filters diff --git a/app/controllers/api/projects.php b/app/controllers/api/projects.php index bdcb89bb3d..2eeac4881a 100644 --- a/app/controllers/api/projects.php +++ b/app/controllers/api/projects.php @@ -112,6 +112,7 @@ App::post('/v1/projects') $attribute['type'], $attribute['size'], $attribute['required'], + $attribute['default'], $attribute['signed'], $attribute['array'], $attribute['filters'], diff --git a/app/workers/database.php b/app/workers/database.php index 8b93bcfd4e..900335f829 100644 --- a/app/workers/database.php +++ b/app/workers/database.php @@ -72,11 +72,12 @@ class DatabaseV1 extends Worker $type = $attribute->getAttribute('type', ''); $size = $attribute->getAttribute('size', 0); $required = $attribute->getAttribute('required', false); + $default = $attribute->getAttribute('default', null); $signed = $attribute->getAttribute('signed', true); $array = $attribute->getAttribute('array', false); $filters = $attribute->getAttribute('filters', []); - $success = $dbForExternal->createAttribute($collectionId, $id, $type, $size, $required, $signed, $array, $filters); + $success = $dbForExternal->createAttribute($collectionId, $id, $type, $size, $required, $default, $signed, $array, $filters); if ($success) { $removed = $dbForExternal->removeAttributeInQueue($collectionId, $id); } diff --git a/tests/e2e/Services/Database/DatabaseBase.php b/tests/e2e/Services/Database/DatabaseBase.php index 7dad8b14cf..89567aae30 100644 --- a/tests/e2e/Services/Database/DatabaseBase.php +++ b/tests/e2e/Services/Database/DatabaseBase.php @@ -63,6 +63,7 @@ trait DatabaseBase 'type' => 'string', 'size' => 256, 'required' => false, + 'default' => null, 'array' => true, ]);