TL;DR When playing with Ecto, where (in the code) should I load data to pre-populate a small table after it is created?
I'm learning Elixir, Phoenix, and Ecto. I have a table in my schema to define a list of configurable options. I'd like to pre-populate this table with a minimum set of options when the table is created.
It seems to me that the _create_table.exs script is the place to do that since that's where the table is created. I was looking at an execute{} block but that seems klunky.
Is there a better place in the code to accomplish pre-populating a new table?
In Phoenix, the common way to populate the DB is to use priv/repo/seeds.exs
it generates with mix run
task as:
mix run priv/repo/seeds.exs
But this approach does actually work pretty fine with any Ecto project. Simply create the file, put seeding code there, and update your mix.exs
aliases
section as
defp aliases do
[
"ecto.setup": [
"ecto.create",
"ecto.migrate",
"run priv/repo/seeds.exs"
],
...
]
Now mix ecto.setup
would create, migrate, and fill in the database.