title: How to update field value dynamically in Ecto migration?
body: <p>I have a Users table like:</p>
  
  <pre><code>     email     | username
  ---------------+----------
   123@321.com   |
   123@123.com   |
   haha@haha.com |
  </code></pre>
  
  <p>and I want to update <code>username</code> field by <code>email</code> field, just slice the <code>email</code> before <code>@</code>.</p>
  
  <pre><code>     email     | username
  ---------------+----------
   123@321.com   | 123
   123@123.com   | 123
   haha@haha.com | haha
  </code></pre>
  
  <p>I have try to use the following migration:</p>
  
  <pre><code>defmodule MyApp.Repo.Migrations.AddDefaultUsernameForUsers do
    use Ecto.Migration
    import Ecto.Query
  
    def up do
        from(u in MyApp.User, update: [set: [username: String.split(u.email, "@") |&gt; List.first ]])
          |&gt; MyApp.Repo.update_all([])
    end
  
    def down do
        MyApp.Repo.update_all(MyApp.User, set: [username: nil])
    end
  end
  </code></pre>
  
  <p>But when runing the migration, I got the following error:</p>
  
  <pre><code>$ mix ecto.migrate
  ** (Ecto.Query.CompileError) `List.first(String.split(u.email(), "@"))` is not a valid query expression
  </code></pre>
  
  <p>How can I solve this?</p>