Skip to content

Commit

Permalink
Merge pull request #52 from utopia-php/validate-uid
Browse files Browse the repository at this point in the history
Validate UID
  • Loading branch information
abnegate committed Oct 9, 2024
1 parent 1f67226 commit 667997c
Show file tree
Hide file tree
Showing 2 changed files with 46 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 @@ -866,9 +889,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
10 changes: 10 additions & 0 deletions src/Migration/Sources/Appwrite.php
Original file line number Diff line number Diff line change
Expand Up @@ -541,6 +541,8 @@ protected function exportGroupDatabases(int $batchSize, array $resources): void
previous: $e
)
);

return;
}

try {
Expand All @@ -557,6 +559,8 @@ protected function exportGroupDatabases(int $batchSize, array $resources): void
previous: $e
)
);

return;
}

try {
Expand All @@ -573,6 +577,8 @@ protected function exportGroupDatabases(int $batchSize, array $resources): void
previous: $e
)
);

return;
}

try {
Expand All @@ -589,6 +595,8 @@ protected function exportGroupDatabases(int $batchSize, array $resources): void
previous: $e
)
);

return;
}

try {
Expand All @@ -605,6 +613,8 @@ protected function exportGroupDatabases(int $batchSize, array $resources): void
previous: $e
)
);

return;
}
}

Expand Down

0 comments on commit 667997c

Please sign in to comment.