Search code examples
csvelixir

Seeding the Database from a CSV file in Phoenix/Elixir


When I try to run: mix run priv/repo/seeds.exs, I have a problem: (FunctionClauseError) no function clause matching in anonymous fn/1 in :elixir_compiler_1.__FILE__/1 The following arguments were given to anonymous fn/1 in :elixir_compiler_1.__FILE__/1:

This is my seeds.exs file:

alias FlightsList.Repo
alias FlightsList.Management.Flights

File.stream!("C:/Users/vukap/phx_projects/flights_list/priv/repo/flights.csv")
|> Stream.drop(1)
|> CSV.decode(headers: [:Id, :Origin, :Destination, :DepartureDate, :DepartureTime, :ArrivalDate, :ArrivalTime, :Number])
|> Enum.each(fn {:ok, map} ->
  Flights.changeset(
    %Flights{},
    %{Id: String.to_integer(map[:Id]), Origin: map[:Origin], Destination: map[:Destination], DepartureDate: String.to_integer(map[:DepartureDate]), DepartureTime: String.to_integer(map[:DepartureTime]), ArrivalDate: String.to_integer(map[:ArrivalDate]), ArrivalTime: String.to_integer(map[:ArrivalTime]), Number: map[:Number]})
  |> Repo.insert!()
end)

How can I fix it?


Solution

  • I guess you are missing a separator in CSV.decode function, here is an example of how I do it, you can call stream_csv in seed file.

      def store_it(row) do
        {:ok, result} = row
    
        %Segments{
          id: result.id,
          name: result.name
        } |> Repo.insert!
      end
    
      def stream_csv do
        Path.expand("~/Project/segmments.csv")
        |> File.stream!
        |> CSV.decode(separator: ?;, headers: [:id, :name])
        |> Enum.each(&store_it/1)
      end