Botanist
Botanist is a seeding library which uses Ecto. Its intended purpose is for seeding a database in a safe and atomic manner.
Installation
Add botanist to your mix.exs file:
defp deps do
[
{:ecto, "~> 2.2.10"},
{:postgrex, "~> 0.11"},
{:botanist, "~> 0.1.0"}, # <--
]
endSet up
Configure Botanist by passing in your repo via your config.exs:
config :botanist,
ecto_repo: MyApp.RepoYou can also configure the following fields:
seeds_path: "priv/repo/seeds" # Location of seed files, defaults to priv/repo/seedsUsage
Botanist is very similar to Ecto in terms of usage. To generate a seed, run:
mix ecto.gen.seed my_seed
You'll find this seed in either the seeds_path field you listed in your config or in the
priv/repo/seeds directory by default.
Tell the seed what to do (see the example) and run your seed with
mix ecto.seedVoila! Your database has now been seeded.
Example seed file
import Botanist
alias MyApp.Repo
alias MyApp.User
def planter do
seed do
Repo.insert(%User{email: "email@gmail.com", name: "John Smith"})
end
end