kylelw23

kylelw23

Populating database from CSV file

I have a Phoenix app that already set up a schema, a controller and a router…
I would like to populate data into my created schema through seeds.exs
I have a csv file and I created a file CSVUtil.ex which is like this:

defmodule CampaignsApi.Store.Campaign do
  @moduledoc """
    Utility module to ingest `campaigns.csv`
  """

  alias NimbleCSV.RFC4180, as: CSV
  alias CampaignsApi.{Store.Campaign, Repo}

  def column_data(file) do
    column_names = get_column_names(file)

    file
    |> File.stream!()
    |> CSV.parse_stream(skip_headers: true)
    |> Enum.map(fn row ->
      row
      |> Enum.with_index()
      |> Map.new(fn {val, num} -> {column_names[num], val} end)
      |> create_or_skip()
    end)
  end

  def get_column_names(file) do
    file
    |> File.stream!()
    |> CSV.parse_stream(skip_headers: false)
    |> Enum.fetch!(0)
    |> Enum.with_index()
    |> Map.new(fn {val, num} -> {num, val} end)
  end

  def create_or_skip(row) do
    case Repo.get_by(Campaign,
           id: row["id"]
         ) do
      nil ->
        Repo.insert(
          %Campaign{}
          |> Campaign.changeset(%{
            id: Integer.new(row["id"]),
            name: row["name"],
            start_date: row["start_date"],
            end_date: row["end_date"],
            budget: Integer.new(row["budget"]),
            hashtags: row["hashtags"],
            team_id: Integer.new(row["team_id"]),
            description: row["description"]
          })
        )

      campaign ->
        {:ok, campaign}
    end
  end
end

I ran mix test and my schema is working fine, and after I ran a seed file that run functions from CSVUtil.
I got this error:

** (Module.Types.Error) found error while checking types for CampaignsApi.Store.update_campaign/2

def update_campaign(%CampaignsApi.Store.Campaign{} = campaign, attrs) do
  CampaignsApi.Repo.update(
    CampaignsApi.Store.Campaign.changeset(
      campaign,
      attrs
    )
  )
end

Please report this bug: https://github.com/elixir-lang/elixir/issues

** (UndefinedFunctionError) function CampaignsApi.Store.Campaign.__struct__/0 is undefined or private
        CampaignsApi.Store.Campaign.__struct__()
        (elixir 1.13.1) lib/module/types/of.ex:131: Module.Types.Of.struct/3
        (elixir 1.13.1) lib/module/types/pattern.ex:750: Module.Types.Pattern.of_shared/4
        (elixir 1.13.1) lib/module/types/pattern.ex:47: Module.Types.Pattern.of_pattern/3
        (elixir 1.13.1) lib/module/types/helpers.ex:93: Module.Types.Helpers.do_map_reduce_ok/3
        (elixir 1.13.1) lib/module/types/pattern.ex:12: Module.Types.Pattern.of_head/4
        (elixir 1.13.1) lib/module/types.ex:62: Module.Types.warnings_from_clause/6
        (elixir 1.13.1) lib/module/types.ex:22: anonymous fn/8 in Module.Types.warnings/5

I am also new to this technology and I dont know what to do to get that CSVUtil works probably…

Marked As Solved

soup

soup

Im not sure, but you’re defining your import module as CampaignsApi.Store.Campaign, then aliasing CampaignsApi.Store.Campaign (I assume this is supposed to be your struct).

As shown, CampaignsApi.Store.Campaign isn’t a struct, nor a schema? So ecto is probably giving up when it can’t find the “type” because it’s given a Module, not a Struct. CampaignsApi.Store.Campaign.__struct__/0 is undefined or private

Can you make your importer CampaignsApi.Import or something?

Also Liked

trisolaran

trisolaran

Hi @kylelw23 and welcome!

Apart from getting your code to run, as you already did with @soup’s help, I think there are a couple of things you could improve:

  1. is somebody consuming the return value of column_data ? If yes, then I suggest you keep it a stream in order to avoid storing all the inserted structs in memory when calling Enum.map(). Thus, consider replacing Enum.map() with Stream.map(). Using the latter will allow the result of the mapping step to be lazily evaluated by the consumer, which is more efficient if you’re dealing with a large number of rows. If, on the other hand, no one is consuming the return value of column_data and you are only interested in the side effect of inserting the rows into the DB, then do replace Enum.map() with Enum.each() to make this clear.

  2. In create_or_skip you are hitting the DB twice: once to check if a row with the given ID already exists, and then to insert the row if it doesn’t. Assuming you have a primary key constraint on the id column in your DB schema you could get rid of the first call by calling Repo.insert() with the on_conflict: :nothing option. The only downside is that the campaign struct returned by the insert, in case of a conflict, won’t be the one in the DB but the one you were trying to insert. This may or may not be a problem depending on who (if ever) is consuming this result (the answer to my question at point 1.)

I hope this helps!

dimitarvp

dimitarvp

When using Stream you always have to use a function that collects the results at the end of the pipe because Stream functions only return other functions. Only when you put e.g. Enum.to_list or Stream.run in the end will the Stream functions in the pipe get executed.

Example:

"/path/to/file.csv"
|> File.stream!()
|> NimbleCSV.RFC41080.parse_stream()
|> Stream.map( ....... )
|> Enum.to_list()

Without the last function the code above only returns a function. Appending Enum.to_list forces that function to get executed. It’s how Stream works in Elixir and many other languages.


But be advised: using Stream incurs some performance penalty. Only use it if you have big collections of elements and you don’t want to have intermediate collections that get processed and then thrown away. Also it’s a good idea to only reach for Stream when you have several steps of processing. Your code above definitely does NOT need Stream as it is.

dimitarvp

dimitarvp

Sure, I’ve seen it above. I was seeking to remove confusion as to why only using Stream will seem to not work.

Last Post!

trisolaran

trisolaran

Ok then you should ditch Enum.map and use Enum.each instead, because you are only running column_data for the side effects, you don’t care about what it returns.

It makes the code clearer and I think also more efficient: Enum.map will store the result of the mapping in memory, and this could be a lot if you have a lot of rows.

Where Next?

Popular in Questions Top

baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New

Other popular topics Top

electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 40082 209
New
saif
Hello everyone, Long time lurker first time poster here. I’ve recently begun working on Elixir full-time again! :raised_hands: It’s been...
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New

We're in Beta

About us Mission Statement