{"action":"created","issue":{"url":"https://api.github.com/repos/phoenixframework/phoenix_ecto/issues/8","labels_url":"https://api.github.com/repos/phoenixframework/phoenix_ecto/issues/8/labels{/name}","comments_url":"https://api.github.com/repos/phoenixframework/phoenix_ecto/issues/8/comments","events_url":"https://api.github.com/repos/phoenixframework/phoenix_ecto/issues/8/events","html_url":"https://github.com/phoenixframework/phoenix_ecto/issues/8","id":66737465,"number":8,"title":"Add mix task for seeding the database","user":{"login":"scrogson","id":53274,"avatar_url":"https://avatars.githubusercontent.com/u/53274?v=3","gravatar_id":"","url":"https://api.github.com/users/scrogson","html_url":"https://github.com/scrogson","followers_url":"https://api.github.com/users/scrogson/followers","following_url":"https://api.github.com/users/scrogson/following{/other_user}","gists_url":"https://api.github.com/users/scrogson/gists{/gist_id}","starred_url":"https://api.github.com/users/scrogson/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/scrogson/subscriptions","organizations_url":"https://api.github.com/users/scrogson/orgs","repos_url":"https://api.github.com/users/scrogson/repos","events_url":"https://api.github.com/users/scrogson/events{/privacy}","received_events_url":"https://api.github.com/users/scrogson/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":1,"created_at":"2015-04-07T00:18:49Z","updated_at":"2015-04-07T01:40:52Z","closed_at":null,"body":"I brought this idea to @ericmj and @chrismccord today, but it seems that we might need some more opinions on the matter.\r\n\r\nThe first idea was to add `mix ecto.seed` to Ecto. It would read `seed.exs` from `priv/repo/seeds`. This would allow users to seed their database following a nice convention.\r\n\r\nEric pointed out that this can be done really easily with `mix run priv/repo/seeds.exs`. But now I realize that the application probably needs to be started (or at least the `Repo` connected first). So it might be nice to provide a way to do this without boilerplate.\r\n\r\nEric also mentioned that maybe this should not be something concerned with Ecto but a separate application. I thought about `phoenix_ecto` for this.\r\n\r\nTalking with Chris, he mentioned that we could just handle this by generating a mix task for the application `my_app/lib/mix/tasks/seed.ex` and have empty `seed/1` functions base on the environment that the user would have to fill out.\r\n\r\nI still think that it would be nice to separate the task itself from the seed files. The task would start the repo and use `priv/repo/seeds/seed.exs` to insert records and whatnot. I think it's important to provide a clean interface for this.\r\n\r\n```elixir\r\n# priv/repo/seeds/seed.exs\r\nuse PhoenixEcto.Seed\r\n\r\nimport_seeds \"users_#{Mix.env}.exs\"\r\nimport_seeds \"posts_#{Mix.env}.exs\"\r\nimport_seeds \"categories_#{Mix.env}.exs\"\r\n```\r\n\r\n```elixir\r\n# priv/repo/seeds/users_dev.exs\r\nuse PhoenixEcto.Seed\r\n\r\n%User{name: \"Sonny Scroggin\", admin: true} |> Repo.insert\r\n...\r\n```\r\n\r\n```\r\nmix my_app.seed\r\n```\r\n\r\nThoughts?"},"comment":{"url":"https://api.github.com/repos/phoenixframework/phoenix_ecto/issues/comments/90307963","html_url":"https://github.com/phoenixframework/phoenix_ecto/issues/8#issuecomment-90307963","issue_url":"https://api.github.com/repos/phoenixframework/phoenix_ecto/issues/8","id":90307963,"user":{"login":"chrismccord","id":576796,"avatar_url":"https://avatars.githubusercontent.com/u/576796?v=3","gravatar_id":"","url":"https://api.github.com/users/chrismccord","html_url":"https://github.com/chrismccord","followers_url":"https://api.github.com/users/chrismccord/followers","following_url":"https://api.github.com/users/chrismccord/following{/other_user}","gists_url":"https://api.github.com/users/chrismccord/gists{/gist_id}","starred_url":"https://api.github.com/users/chrismccord/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/chrismccord/subscriptions","organizations_url":"https://api.github.com/users/chrismccord/orgs","repos_url":"https://api.github.com/users/chrismccord/repos","events_url":"https://api.github.com/users/chrismccord/events{/privacy}","received_events_url":"https://api.github.com/users/chrismccord/received_events","type":"User","site_admin":false},"created_at":"2015-04-07T01:40:52Z","updated_at":"2015-04-07T01:40:52Z","body":"I am against the `import_seed` approach because it introduces more functions/macros where we could use a regular task. If we add an idiomatic seed setup, my vote is to generate a file like below. I also like that it acts as another avenue to introduce mix tasks to users early and make it part of their workflow. \r\n\r\n```elixir\r\ndefmodule Mix.Tasks.MyApp.Seed do\r\n  use Mix.Task\r\n\r\n  def run(_) do\r\n    Mix.Task.run \"app.start\", []\r\n    seed(Mix.env)\r\n  end\r\n\r\n  def seed(:dev) do\r\n    # dev seeds\r\n  end\r\n\r\n  # def seed(:prod) do\r\n  #   prod seeds, use with care\r\n  # end\r\nend\r\n```\r\n\r\nI am :+1: on coming up with a standard seeding approach"}}