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

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
Blokh
Hey guys, I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly Do you guys have any suggestions what is the best prac...
New
roeland
Kia ora, We have been using elixir-google-api to connect to Google Drive. However, with the updates to Tesla due to CVEs this is now bro...
New
kszambelanczyk
Hello! Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app. I creat...
New
Onor.io
I have what I’ve heard referred to as a “lookup table” in my database. This is a way of assigning codes to common values. One common lo...
New
jaybe78
Hello, I’m developing a online persistent chat system (what’s app) like using elixir/dynamodb/aws for a mobile app(flutter). The diffic...
New
Trolleger
What approach to take when sending live updates to “random” users Hi! I have a question, I have a little chat app, and when I create a DM...
New

Other Trending Topics Top

garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
wintermeyer
There are three potential reasons for members of this forum to have a look at https://vutuv.de You are tired or annoyed of LinkedIn. Yo...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews