Drill behaviour (drill v1.2.3)
Drill is an elixir seeder library inspired by Seed Fu and Phinx.
Usage
- Create your seeder modules. The directory where the seeder modules should be located is described on mix drill documentation.
In my_app/priv/repo/seeds/user.exs:
defmodule MyApp.Seeds.User do
use Drill, key: :users, source: MyApp.Accounts.User
def factory do
%{
first_name: Person.first_name(),
last_name: Person.last_name()
}
end
def run(_context) do
[
seed(email: "email1@example.com"),
seed(email: "email2@example.com"),
seed(email: "email3@example.com")
]
end
endIn my_app/priv/repo/seeds/post.exs:
defmodule MyApp.Seeds.Post do
use Drill, key: :posts, source: MyApp.Blogs.Post
alias Faker.Lorem
def deps do
[MyApp.Seeds.User]
end
def factory do
%{content: Lorem.paragraph()}
end
def run(%Drill.Context{seeds: %{users: [user1, user2, user3 | _]}}) do
[
seed(user_id: user1.id),
seed(user_id: user2.id),
seed(user_id: user3.id)
]
end
end- Run
mix drill -r MyApp.Repoin the terminal with your project root as the current working directory
Installation
Add drill to your list of dependencies in mix.exs:
def deps do
[
{:drill, "~> 1.2"}
]
endConfigurations
Timeout
Default timeout is 600 seconds or 10 minutes. You may configure the task timeout in your config.exs file e.g.: config :drill, :timeout, 10_000
use Drill options
source(required) - source is the schema modulekey(required) - once the seeder module runs, the inserted result will be saved to%Drill.Context{}.seeds[key]. Drill.Context struct is passed to one of Drill's callback which isrun/1to be discussed in theCallbackssection below.returning(optional) - selects which fields to return. Defaults to true. See Ecto.Repo.insert_all/3
Callbacks
constraints/0(optional) - returns a list of column names to verify for conflicts. If a conflict occurs all fields will just be updated. This prevents insertion of new records based on the constraints when drill is run again.on_conflict/0(optional) - returns the conflict strategy. The default is:replace_all. Only works whenconstraints/0returns a non-empty list. See Ecto.Repo.insert_all/4 for more details.deps/0(optional) - returns a list of seeder modules that should be run prior to the current seederfactory/0(optional) - set default values for the fields. This is used when you callseed/1from the seeder module.run/1(required) - returns a list of seeds (a call toDrill.seed/1function or anything you want to include in the context seed). Autogenerated fields such as:inserted_ator:updated_atmay not be defined. The first argument is theDrill.Contextstruct, which you can use to get the inserted records from previously run seeder modules (see Usage section above).
Command line options
--repo- specifies the repository to use--seeds-path- overrides the default seeds path- Command line options for
mix app.startdocumented here
Summary
Callbacks
Link to this callback
constraints()
Link to this callback
deps()
@callback deps() :: [atom()]
Link to this callback
factory()
@callback factory() :: map()
Link to this callback
on_conflict()
Link to this callback
run(t)
@callback run(Drill.Context.t()) :: [any()]