I have the following seeding setup:
- A
seed/2
function that usesPhilColumns.Seeder.run/4
- Seed files are placed in
priv/repo/seeds
- I also have a
seeds.exs
script that runsGlific.Seeds.SeedsDev.seed()
and populates a lot of initial data
code is something like this
@spec seed(any, any) :: any
def seed(opts \\ Keyword.new(), seeder \\ &PhilColumns.Seeder.run/4) do
repos = load_repos()
# set env with current_env/0 overwriting provided arg
# Tags keyword is required for the PhilColumns library
opts =
Keyword.put(opts, :env, current_env())
|> Keyword.put(:tags, [])
opts =
if opts[:to] || opts[:step] || opts[:all],
do: opts,
else: Keyword.put(opts, :all, true)
opts =
if opts[:log],
do: opts,
else: Keyword.put(opts, :log, :info)
opts =
if opts[:quiet],
do: Keyword.put(opts, :log, false),
else: opts
opts =
if opts[:tenant],
do: opts,
else: Keyword.put(opts, :tenant, "main")
# We need to run the with the loaded repo. This is a public API provided
for repo <- repos do
{:ok, _, _} = Ecto.Migrator.with_repo(repo, &run_seeders(&1, seeder, opts))
end
end
defp current_env, do: Mix.env()
# Get active repo context
@spec load_repos() :: any()
defp load_repos do
Application.load(@app)
Application.fetch_env!(@app, :ecto_repos)
end
@spec run_seeders(any(), any(), Keyword.t()) :: any()
defp run_seeders(repo, seeder, opts) do
IO.inspect(repo, label: "repo")
IO.inspect(seeder, label: "seeder")
IO.inspect(opts, label: "opts")
IO.inspect(Path.join(:code.priv_dir(@app), "repo/seeds"), label: "path")
seeder.(repo, Path.join(:code.priv_dir(@app), "repo/seeds"), :up, opts)
end
and it should go here
# Script for populating the database. You can run it as:
#
# mix run priv/repo/seeds.exs
#
# Inside the script, you can read and write to any of your
# repositories directly:
#
# Glific.Repo.insert!(%Glific.SomeSchema{})
#
# We recommend using the bang functions (`insert!`, `update!`
# and so on) as they will fail if something goes wrong.
alias Glific.Seeds.SeedsDev
SeedsDev.seed()
but its not going the reason i got
PhilColumns does not automatically execute seeds.exs
.
It only executes .exs
files in the priv/repo/seeds/
folder that:
- Are named like a timestamp:
20250323123000_seed_users.exs
- Contain a module that uses
PhilColumns.Migration
- Define an
up/2
function
I just want to understand — could this be the reason? It was working before but suddenly stopped