Skip to content

Commit

Permalink
validate UID
Browse files Browse the repository at this point in the history
  • Loading branch information
fogelito committed Oct 9, 2024
1 parent b37050f commit b230c8b
Showing 1 changed file with 36 additions and 1 deletion.
37 changes: 36 additions & 1 deletion src/Migration/Destinations/Appwrite.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
use Utopia\Database\Query;
use Utopia\Database\Validator\Index as IndexValidator;
use Utopia\Database\Validator\Structure;
use Utopia\Database\Validator\UID;
use Utopia\Migration\Destination;
use Utopia\Migration\Exception;
use Utopia\Migration\Resource;
Expand Down Expand Up @@ -302,14 +303,25 @@ public function importDatabaseResource(Resource $resource, bool $isLast): Resour
/**
* @throws AuthorizationException
* @throws StructureException
* @throws DatabaseException
* @throws DatabaseException|Exception
*/
protected function createDatabase(Database $resource): bool
{
$resourceId = $resource->getId() == 'unique()'
? ID::unique()
: $resource->getId();

$validator = new UID();

if ($validator->isValid($resourceId)) {
throw new Exception(
resourceName: $resource->getName(),
resourceGroup: $resource->getGroup(),
resourceId: $resource->getId(),
message: $validator->getDescription(),
);
}

$resource->setId($resourceId);

$database = $this->database->createDocument('databases', new UtopiaDocument([
Expand Down Expand Up @@ -354,6 +366,17 @@ protected function createCollection(Collection $resource): bool
? ID::unique()
: $resource->getId();

$validator = new UID();

if ($validator->isValid($resourceId)) {
throw new Exception(
resourceName: $resource->getName(),
resourceGroup: $resource->getGroup(),
resourceId: $resource->getId(),
message: $validator->getDescription(),
);
}

$resource->setId($resourceId);

$database = $this->database->getDocument(
Expand Down Expand Up @@ -865,9 +888,21 @@ protected function createIndex(Index $resource): bool
* @throws AuthorizationException
* @throws DatabaseException
* @throws StructureException
* @throws Exception
*/
protected function createDocument(Document $resource, bool $isLast): bool
{
$validator = new UID();

if ($validator->isValid($resource->getId())) {
throw new Exception(
resourceName: $resource->getName(),
resourceGroup: $resource->getGroup(),
resourceId: $resource->getId(),
message: $validator->getDescription(),
);
}

// Check if document has already been created
$exists = \array_key_exists(
$resource->getId(),
Expand Down

0 comments on commit b230c8b

Please sign in to comment.