ejstembler

ejstembler

I want to seed my Users table using a CSV file. I have it reading the file okay, but I don’t know how to typecast what CVS decodes (map?) into my Ecto Model.

File.stream!(Path.expand("priv/repo/users.tsv"))
  |> Stream.drop(1) # Skip header row
  |> CSV.decode(separator: ?\t, headers: [:id, :username, :first_name, :middle_name, :last_name, :email, :position, :last_login_at, :last_request_at, :last_request_url, :active, :inserted_at, :updated_at])
  |> Enum.each(&Repo.insert!/1)

Also, my CSV file was exported from SQL Server, and the null columns have NULL as text. Is there a way to replace with nil in a pipeline. Only certain columns could have NULL, not all of them.

Lastly, I’ve already seen the blog post from Wendy; it seems there’s been breaking changes since it was written.

Showing Posts 1 to 1

ejstembler

ejstembler OP

Well, I got this to work after a bit:

alias MyProject.Repo
alias MyProject.User

defmodule MyProject.Seeds do

  def row_to_user(row) when is_map(row) do
    %User{
      id: normalize_id(row.id),
      username: normalize_username(row.username),
      first_name: normalize_null(row.first_name),
      middle_name: normalize_null(row.middle_name),
      last_name: normalize_null(row.last_name),
      email: normalize_null(row.email),
      position: normalize_null(row.position),
      last_logon_at: normalize_date(row.last_logon_at),
      last_request_at: normalize_date(row.last_request_at),
      last_request_url: normalize_null(row.last_request_url),
      active: normalize_active(row.active),
      inserted_at: normalize_date(row.inserted_at),
      updated_at: normalize_date(row.updated_at)
    }
  end

  def normalize_id(value) when is_binary(value) do
    {result, _} = Integer.parse(value)
    result
  end

  def normalize_username(value) when is_binary(value) do
    String.downcase(value)
  end

  def normalize_active(value) when is_binary(value) do
    case value do
      "0" -> false
      "1" -> true
    end
  end

  def normalize_date(value) when is_binary(value) do
    case value do
      "NULL" -> nil
      "" -> nil
      _ ->
        parts = String.split(value, " ")
        Ecto.DateTime.cast!("#{Enum.at(parts, 0)}T#{Enum.at(parts, 1)}")
    end
  end

  def normalize_null(value) when is_binary(value) do
    case value do
      "NULL" -> nil
      "" -> nil
      _ -> value
    end
  end

end

# Users
File.stream!(Path.expand("priv/repo/users.tsv"))
  |> Stream.drop(1) # Skip header row
  |> CSV.decode(separator: ?\t, headers: [:id, :username, :first_name, :middle_name, :last_name, :email, :position, :last_logon_at, :last_request_at, :last_request_url, :active, :inserted_at, :updated_at])
  |> Enum.map(fn (row) -> MyProject.Seeds.row_to_user(row) end)
  |> Enum.each(&Repo.insert!/1)
— All posts loaded —

Where Next? Top

Trending in Questions Top

itzmidinesh
Hi all, I’m working on a Phoenix project where I need to make external HTTP requests and I’m trying to choose between Req and HTTPoison....
New
yarrichar
I’m very new to Elixir, so sorry if this is a silly question: I am wondering what happens to the mailbox of a process when the machine i...
New
pragdave
How do I tell Hologram not to escape HTML when substituting into templates? {Textile.render(@product.long_description)} escapes the HTM...
New
Fl4m3Ph03n1x
Background When trying to execute mix release on a Windows 11 machine for a Phoenix project I get the following error: * assembling mark...
New
rlipscombe
I’d like to create a file with 600 permissions (rw-------), but there’s a race condition if I create it and then change the permissions. ...
New
saleyn
Is there a way to import record definitions in the iex shell for pretty printing (similar to the rr command in the erl shell)? This woul...
New
mandar1045
I load tested a plain Phoenix chat app backed by Postgres with 100 million messages in it, everything on default settings, to see what br...
New

Other Trending Topics Top

CodeSync
Update from the Erlang Ecosystem Foundation - Dan Janowski | ElixirConf US 2026 Comments welcome! View the <span class="h...
New
dominicletz
Hey there, I haven’t seen a list of useful Elixir specific skills for the popular AI tools yet so I’m starting one here with my first. ...
New
raa-ziq
Hi everyone :waving_hand: I wanted to share something I’ve been building — NowBlind, a random chat app designed to help people discover ...
New
mudasobwa
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
OndrejValenta
(I just needed to vent somewhere and LinkedIn is full of hope, or hype, I’m not sure which exactly) AI dream has many faces, but general...
New
kip
Localize is the next generation localisation library for Elixir. Think of it as ex_cldr version 3.0. The first version will be released ...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews