Strangely I fixed this by using a schema in my migration. (Just a local schema- not making the mistake of using my model’s schema…)
Updated migration that works:
defmodule Ido.Repo.Migrations.CreateUsersAuthTables do
import Ecto.Changeset
use Ecto.Migration
defmodule User do
use Ido.Schema
schema "users" do
field(:hashed_password, :string)
end
def changeset(element, attrs) do
element
|> cast(attrs, [
:hashed_password
])
end
end
def change do
execute("CREATE EXTENSION IF NOT EXISTS citext", "")
alter table(:users) do
# temporarily not making password required, as we need to fill in defaults for existing users
add(:hashed_password, :string)
end
# generate random password for existing users
flush()
add_placeholder_passwords()
alter table(:users) do
modify(:hashed_password, :string, null: false)
end
end
defp add_placeholder_passwords do
# generate random password for existing users
for user <- Ido.Repo.all(User) do
# use a unique uuid for password, users will have to reset password to login
password = Ecto.UUID.generate()
hashed_password = Bcrypt.hash_pwd_salt(password)
user
|> User.changeset(%{hashed_password: hashed_password})
|> Ido.Repo.update()
end
end
end