Add NOT NULL constraint to users.role_id and optimize default_role_id
All checks were successful
continuous-integration/drone/push Build is passing
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:
parent
86c8b23c77
commit
2d446f63ea
6 changed files with 501 additions and 37 deletions
|
|
@ -0,0 +1,36 @@
|
|||
defmodule Mv.Repo.Migrations.AddNotNullConstraintToUsersRoleId do
|
||||
@moduledoc """
|
||||
Adds NOT NULL constraint to users.role_id column.
|
||||
|
||||
This ensures that role_id can never be NULL at the database level,
|
||||
providing an additional safety layer beyond Ash's allow_nil? false.
|
||||
|
||||
Before running this migration, ensure all existing users have a role_id
|
||||
(the previous migration AssignMitgliedRoleToExistingUsers handles this).
|
||||
"""
|
||||
use Ecto.Migration
|
||||
|
||||
def up do
|
||||
# First ensure all users have a role_id (safety check)
|
||||
# This should already be done by the previous migration, but we check anyway
|
||||
execute("""
|
||||
UPDATE users
|
||||
SET role_id = (
|
||||
SELECT id FROM roles WHERE name = 'Mitglied' LIMIT 1
|
||||
)
|
||||
WHERE role_id IS NULL
|
||||
""")
|
||||
|
||||
# Now add NOT NULL constraint
|
||||
alter table(:users) do
|
||||
modify :role_id, :uuid, null: false
|
||||
end
|
||||
end
|
||||
|
||||
def down do
|
||||
# Remove NOT NULL constraint (allow NULL again)
|
||||
alter table(:users) do
|
||||
modify :role_id, :uuid, null: true
|
||||
end
|
||||
end
|
||||
end
|
||||
Loading…
Add table
Add a link
Reference in a new issue