1
0
Fork 0
mirror of synced 2024-07-06 23:21:05 +12:00

Update migration for audit collection

1. Remove the userInternalId attribute
2. Replace userId in audit documents with userInternalId and put
userId in data
This commit is contained in:
Steven Nguyen 2023-07-12 10:57:55 -07:00
parent ca6e9a7f1e
commit 26334b79bf
No known key found for this signature in database

View file

@ -133,6 +133,16 @@ class V18 extends Migration
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;
}
@ -195,6 +205,34 @@ class V18 extends Migration
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;