chore(migration): ensure_system_actor_user_exists

Use admin_role_id, consistent UUID and timestamps.
This commit is contained in:
Moritz 2026-01-27 17:37:35 +01:00
parent 62b9a82045
commit 0df90c369b

View file

@ -41,13 +41,15 @@ defmodule Mv.Repo.Migrations.EnsureSystemActorUserExists do
end
end
defp ensure_system_actor_user_exists(_admin_role_id) do
defp ensure_system_actor_user_exists(admin_role_id) do
case repo().one(from(u in "users", where: u.email == ^@system_user_email, select: u.id)) do
nil ->
# Use uuid_generate_v7() for consistency with roles insert in this migration
role_id_str = uuid_to_string(admin_role_id)
execute("""
INSERT INTO users (id, email, hashed_password, oidc_id, member_id, role_id)
SELECT gen_random_uuid(), '#{@system_user_email}', NULL, NULL, NULL, r.id
FROM roles r WHERE r.name = 'Admin' LIMIT 1
VALUES (uuid_generate_v7(), '#{@system_user_email}', NULL, NULL, NULL, '#{role_id_str}'::uuid)
""")
IO.puts("✅ Created system actor user (#{@system_user_email})")
@ -56,4 +58,11 @@ defmodule Mv.Repo.Migrations.EnsureSystemActorUserExists do
:ok
end
end
defp uuid_to_string(id) when is_binary(id) do
{:ok, str} = Ecto.UUID.load(id)
str
end
defp uuid_to_string(id), do: to_string(id)
end