1
0
Fork 0
mirror of synced 2024-08-04 04:52:03 +12:00

Merge pull request #5833 from appwrite/fix-deletes-worker-internal-ids

Ensure deletes worker deletes resources using internal id
This commit is contained in:
Eldad A. Fux 2023-07-20 09:34:51 +03:00 committed by GitHub
commit 5b2dab2b10
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 25 additions and 26 deletions

View file

@ -255,18 +255,6 @@ App::delete('/v1/teams/:teamId')
throw new Exception(Exception::TEAM_NOT_FOUND); throw new Exception(Exception::TEAM_NOT_FOUND);
} }
$memberships = $dbForProject->find('memberships', [
Query::equal('teamId', [$teamId]),
Query::limit(2000), // TODO fix members limit
]);
// TODO delete all members individually from the user object
foreach ($memberships as $membership) {
if (!$dbForProject->deleteDocument('memberships', $membership->getId())) {
throw new Exception(Exception::GENERAL_SERVER_ERROR, 'Failed to remove membership for team from DB');
}
}
if (!$dbForProject->deleteDocument('teams', $teamId)) { if (!$dbForProject->deleteDocument('teams', $teamId)) {
throw new Exception(Exception::GENERAL_SERVER_ERROR, 'Failed to remove team from DB'); throw new Exception(Exception::GENERAL_SERVER_ERROR, 'Failed to remove team from DB');
} }

View file

@ -8,7 +8,6 @@ use Utopia\Database\Database;
use Utopia\Database\Document; use Utopia\Database\Document;
use Utopia\Database\Query; use Utopia\Database\Query;
use Appwrite\Resque\Worker; use Appwrite\Resque\Worker;
use Executor\Executor;
use Utopia\Storage\Device\Local; use Utopia\Storage\Device\Local;
use Utopia\Abuse\Abuse; use Utopia\Abuse\Abuse;
use Utopia\Abuse\Adapters\TimeLimit; use Utopia\Abuse\Adapters\TimeLimit;
@ -256,6 +255,7 @@ class DeletesV1 extends Worker
protected function deleteCollection(Document $document, Document $project): void protected function deleteCollection(Document $document, Document $project): void
{ {
$collectionId = $document->getId(); $collectionId = $document->getId();
$collectionInternalId = $document->getInternalId();
$databaseId = $document->getAttribute('databaseId'); $databaseId = $document->getAttribute('databaseId');
$databaseInternalId = $document->getAttribute('databaseInternalId'); $databaseInternalId = $document->getAttribute('databaseInternalId');
@ -264,13 +264,13 @@ class DeletesV1 extends Worker
$dbForProject->deleteCollection('database_' . $databaseInternalId . '_collection_' . $document->getInternalId()); $dbForProject->deleteCollection('database_' . $databaseInternalId . '_collection_' . $document->getInternalId());
$this->deleteByGroup('attributes', [ $this->deleteByGroup('attributes', [
Query::equal('databaseId', [$databaseId]), Query::equal('databaseInternalId', [$databaseInternalId]),
Query::equal('collectionId', [$collectionId]) Query::equal('collectionInternalId', [$collectionInternalId])
], $dbForProject); ], $dbForProject);
$this->deleteByGroup('indexes', [ $this->deleteByGroup('indexes', [
Query::equal('databaseId', [$databaseId]), Query::equal('databaseInternalId', [$databaseInternalId]),
Query::equal('collectionId', [$collectionId]) Query::equal('collectionInternalId', [$collectionInternalId])
], $dbForProject); ], $dbForProject);
$this->deleteAuditLogsByResource('database/' . $databaseId . '/collection/' . $collectionId, $project); $this->deleteAuditLogsByResource('database/' . $databaseId . '/collection/' . $collectionId, $project);
@ -297,12 +297,21 @@ class DeletesV1 extends Worker
*/ */
protected function deleteMemberships(Document $document, Document $project): void protected function deleteMemberships(Document $document, Document $project): void
{ {
$teamId = $document->getAttribute('teamId', ''); $dbForProject = $this->getProjectDB($project);
$teamInternalId = $document->getInternalId();
// Delete Memberships // Delete Memberships
$this->deleteByGroup('memberships', [ $this->deleteByGroup(
Query::equal('teamId', [$teamId]) 'memberships',
], $this->getProjectDB($project)); [
Query::equal('teamInternalId', [$teamInternalId])
],
$dbForProject,
function (Document $membership) use ($dbForProject) {
$userId = $membership->getAttribute('userId');
$dbForProject->deleteCachedDocument('users', $userId);
}
);
} }
/** /**
@ -330,19 +339,20 @@ class DeletesV1 extends Worker
protected function deleteUser(Document $document, Document $project): void protected function deleteUser(Document $document, Document $project): void
{ {
$userId = $document->getId(); $userId = $document->getId();
$userInternalId = $document->getInternalId();
$dbForProject = $this->getProjectDB($project); $dbForProject = $this->getProjectDB($project);
// Delete all sessions of this user from the sessions table and update the sessions field of the user record // Delete all sessions of this user from the sessions table and update the sessions field of the user record
$this->deleteByGroup('sessions', [ $this->deleteByGroup('sessions', [
Query::equal('userId', [$userId]) Query::equal('userInternalId', [$userInternalId])
], $dbForProject); ], $dbForProject);
$dbForProject->deleteCachedDocument('users', $userId); $dbForProject->deleteCachedDocument('users', $userId);
// Delete Memberships and decrement team membership counts // Delete Memberships and decrement team membership counts
$this->deleteByGroup('memberships', [ $this->deleteByGroup('memberships', [
Query::equal('userId', [$userId]) Query::equal('userInternalId', [$userInternalId])
], $dbForProject, function (Document $document) use ($dbForProject) { ], $dbForProject, function (Document $document) use ($dbForProject) {
if ($document->getAttribute('confirm')) { // Count only confirmed members if ($document->getAttribute('confirm')) { // Count only confirmed members
$teamId = $document->getAttribute('teamId'); $teamId = $document->getAttribute('teamId');
@ -352,7 +362,7 @@ class DeletesV1 extends Worker
'teams', 'teams',
$teamId, $teamId,
// Ensure that total >= 0 // Ensure that total >= 0
$team->setAttribute('total', \max($team->getAttribute('total', 0) - 1, 0)) $team->setAttribute('total', \max($team->getAttribute('total', 0) - 1, 0))
); );
} }
} }
@ -360,7 +370,7 @@ class DeletesV1 extends Worker
// Delete tokens // Delete tokens
$this->deleteByGroup('tokens', [ $this->deleteByGroup('tokens', [
Query::equal('userId', [$userId]) Query::equal('userInternalId', [$userInternalId])
], $dbForProject); ], $dbForProject);
} }
@ -475,13 +485,14 @@ class DeletesV1 extends Worker
$projectId = $project->getId(); $projectId = $project->getId();
$dbForProject = $this->getProjectDB($project); $dbForProject = $this->getProjectDB($project);
$functionId = $document->getId(); $functionId = $document->getId();
$functionInternalId = $document->getInternalId();
/** /**
* Delete Variables * Delete Variables
*/ */
Console::info("Deleting variables for function " . $functionId); Console::info("Deleting variables for function " . $functionId);
$this->deleteByGroup('variables', [ $this->deleteByGroup('variables', [
Query::equal('functionId', [$functionId]) Query::equal('functionInternalId', [$functionInternalId])
], $dbForProject); ], $dbForProject);
/** /**