Add NOT NULL constraint to users.role_id and optimize default_role_id
All checks were successful
continuous-integration/drone/push Build is passing

- Add database-level NOT NULL constraint for users.role_id
- Update SystemActor tests to verify NOT NULL constraint enforcement
- Add process dictionary caching for default_role_id/0 to reduce DB queries
This commit is contained in:
Moritz 2026-01-25 17:04:37 +01:00
parent 86c8b23c77
commit 2d446f63ea
Signed by: moritz
GPG key ID: 1020A035E5DD0824
6 changed files with 501 additions and 37 deletions

View file

@ -442,6 +442,15 @@ defmodule Mv.Accounts.User do
It fetches the "Mitglied" role from the database without authorization checks
(safe during user creation bootstrap phase).
The result is cached in the process dictionary to avoid repeated database queries
during high-volume user creation. The cache is invalidated on application restart.
## Performance Note
This function makes one database query per process (cached in process dictionary).
For very high-volume scenarios, consider using a fixed UUID from Application config
instead of querying the database.
## Returns
- UUID of the "Mitglied" role if it exists
@ -454,9 +463,20 @@ defmodule Mv.Accounts.User do
"""
@spec default_role_id() :: Ecto.UUID.t() | nil
def default_role_id do
case Mv.Authorization.Role.get_mitglied_role() do
{:ok, %Mv.Authorization.Role{id: role_id}} -> role_id
_ -> nil
# Cache in process dictionary to avoid repeated queries
case Process.get({__MODULE__, :default_role_id}) do
nil ->
role_id =
case Mv.Authorization.Role.get_mitglied_role() do
{:ok, %Mv.Authorization.Role{id: id}} -> id
_ -> nil
end
Process.put({__MODULE__, :default_role_id}, role_id)
role_id
cached_role_id ->
cached_role_id
end
end
end