iarekk
Why does this code work as 2 separate migrations, but not as a single one?
Hi all, new to Elixir, going through the “Programming Phoenix LiveView” book right now. Fairly new to working with Ecto as well ![]()
An exercise in chapter instructs the reader to add a username field to the User struct, and implement the Ecto migrations to support that.
Additionally, I would like to make the username:
- mandatory
- unique
- populate
emailas default value for existing DB records.
I’ve achieved it successfully with the following migrations:
First migration: create column:
def change do
alter(table(:users)) do
add :username, :citext
end
end
Second migration, fill column and make it unique/mandatory:
def change do
from(u in Pento.Accounts.User, update: [set: [username: u.email]])
|> Pento.Repo.update_all([])
alter(table(:users)) do
modify :username, :citext, null: false
end
create(unique_index(:users, :username))
end
However, I can’t seem to be able to make these updates as a single migration. Why?
My first draft looked like this:
defmodule Pento.Repo.Migrations.CreateUsernameColumn do
use Ecto.Migration
import Ecto.Query, only: [from: 2]
def change do
alter(table(:users)) do
add :username, :citext
end
from(u in Pento.Accounts.User, update: [set: [username: u.email]])
|> Pento.Repo.update_all([])
alter(table(:users)) do
modify :username, :citext, null: false
end
create(unique_index(:users, :username))
end
end
… but it throws an error in the from(u in... statement as Users doesn’t have the column called username. But I just created that column 2 rows above. What am I missing?
P.S. If it helps, complete repo here: Ch2/add username field by iarekk · Pull Request #7 · iarekk/programming_phoenix_liveview · GitHub
Most Liked
LostKobrakai
SQL works fine, but for completeness one can write ecto queries without depending on schemas as well. from u in "users", update: [set: [username: u.email]] should do just fine.
sodapopcan
It very well might be transaction related (everything in change is indeed run inside one). If you ever need to commit a transaction in a migration you can use the flush() function. However, @iarekk, you really shouldn’t put schema names in migrations. If the schemas ever change in a way that older migrations don’t expect, they will break. It’s actually best to keep data migrations complete out of migrations and use a different solution but if you do, they should be written as raw SQL: execute("update users u set username = u.email") and best written as separate migrations anyway so they can be deleted once they’ve been run in production.







