script
How to increment database table column
I know in elixir data types are immutable. I have a column name failed_login_tries. which is integer and i want to increment it every time user put incorrect password. this is the code
defp check_chromatic_password(plaintext, %User{} = user) do
if Bcrypt.checkpw(plaintext, user.local_password_hash) do
{:ok, user}
else
{:error, :invalid_password}
end
end
failed_login_tries is inside the %User struct
Marked As Solved
minhajuddin
You can change the user struct by using user = %{ user | failed_login_tries: user.failed_login_tries + 1} and to persist this in the database use Repo.update by passing it an updated ecto changeset. Immutability refers to the fact that the previous struct remains the same. e.g.
defmodule User do
defstruct [:email]
end
u1 = %User{email: "mujju"}
u2 = u1
u2 = %{u2 | email: "danny"}
IO.inspect(u1)
# => %User{email: "mujju"}
IO.inspect(u2)
# => %User{email: "danny"}
Also Liked
LostKobrakai
There’s an even simpler option to write that query:
from(u in User, update: [inc: [failed_login_tries: 1]], where: u.id == user.id) )
aseigo
Just be aware that there is an inherent race condition with that approach: if the user fails auth in quick succession twice both may have the user record with the current count prior to either auth attempt, and so both will increment the old value by one and update the db with that, causing the N attempts to be counted as just 1!
The correct way to do this is in the database itself using a fragment as in:
from(u in User,
update: [set: [failed_login_tries: fragment("failed_login_tries + 1")]],
where: u.id == user.id) )
|> MyRepo.update_all([])
This way the db sorts out the concurrent updates and you will always end up with the correct count.
aseigo
I always forget about inc (and friends) .. thanks for the reminder!









