Skip to content

Instantly share code, notes, and snippets.

@bratsche
Created January 18, 2016 20:13
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 bratsche/ea3085141b35579f1fd5 to your computer and use it in GitHub Desktop.
Save bratsche/ea3085141b35579f1fd5 to your computer and use it in GitHub Desktop.
Example custom mix task to seed the database using an existing seed file
# lib/mix/tasks/musikr.seed.ex
defmodule Mix.Tasks.Musikr.Seed do
use Mix.Task
@shortdoc "Seeds the database"
@moduledoc """
Seed the database using the file 'priv/repo/seeds.exs'
"""
def run(_args) do
Code.eval_file("priv/repo/seeds.exs")
end
end
# priv/repo/seeds.exs
instruments = ~w(piano violin viola cello bass horn tuba trombone clarinet flute oboe)
for i <- instruments do
Musikr.Repo.insert!(%Musikr.Instrument{name: i})
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment