Skip to content

Instantly share code, notes, and snippets.

@mathieujobin
Last active February 27, 2017 10:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mathieujobin/5697b4e9c509407fb4c8e48e1963cbe7 to your computer and use it in GitHub Desktop.
Save mathieujobin/5697b4e9c509407fb4c8e48e1963cbe7 to your computer and use it in GitHub Desktop.

I used this to move some data from a ruby project to an eliir project

i used a rails console and a iex -S mix session

in ruby

json = Model.all.map {|p| {f1: p.f1, ...}}.to_json
File.open('user.json', 'wb+') {|f| f.write(json)}

in elixir

json_file = "/path/to/file/user.json"
binary = json_file |> File.read!
data = binary |> Poison.decode!
alias Web.User

  defmodule Bingo do
    def insert_multiple(repo, model, map, [string_key_map | tail]) do
      data = for {key, val} <- string_key_map, into: %{}, do: {String.to_atom(key), val}
      model.changeset(map, data) |> repo.insert
      IO.puts "Inserted #{data}"
      insert_multiple(repo, model, map, tail)
    end
    def insert_multiple(_,_,_, []) do
      IO.puts "This is the end. Thank you"
    end
  end
  Bingo.insert_multiple(Web.Repo, User, %User{}, data)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment