Skip to content

Commit

Permalink
Update migration for audit collection
Browse files Browse the repository at this point in the history
1. Remove the userInternalId attribute
2. Replace userId in audit documents with userInternalId and put
userId in data
  • Loading branch information
stnguyen90 committed Jul 20, 2023
1 parent 9908a90 commit cd78706
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions src/Appwrite/Migration/Version/V18.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,16 @@ private function migrateCollections(): void
Console::warning("'options' from {$id}: {$th->getMessage()}");
}
break;
case 'audit':
try {
/**
* Delete 'userInternalId' attribute
*/
$this->projectDB->deleteAttribute($id, 'userInternalId');
} catch (\Throwable $th) {
Console::warning("'userInternalId' from {$id}: {$th->getMessage()}");
}
break;
default:
break;
}
Expand Down Expand Up @@ -195,6 +205,34 @@ protected function fixDocument(Document $document): Document
Console::warning($th->getMessage());
}
break;
case 'audit':
/**
* Set the userId to the userInternalId and add userId to data
*/
try {
$userId = $document->getAttribute('userId');
$data = $document->getAttribute('data', []);
$mode = $data['mode'] ?? 'default';
$user = match ($mode) {
'admin' => $this->consoleDB->getDocument('users', $userId),
default => $this->projectDB->getDocument('users', $userId),
};

if ($user->isEmpty()) {
// The audit userId could already be an internal Id.
// Otherwise, the user could have been deleted.
// Nonetheless, there's nothing else we can do here.
break;
}
$internalId = $user->getInternalId();
$document->setAttribute('userId', $internalId);
$data = $document->getAttribute('data', []);
$data['userId'] = $user->getId();
$document->setAttribute('data', $data);
} catch (\Throwable $th) {
Console::warning($th->getMessage());
}
break;
}

return $document;
Expand Down

0 comments on commit cd78706

Please sign in to comment.