在伞应用程序中运行混合ecto种子文件



在伞应用程序的最高级别上,许多ECTO混合任务工作。可以在伞的顶层运行seeds.exs文件吗?我想将mix ecto.reset别名添加到伞上。我可以运行dropcreatemigrate任务。但是我不知道如何运行每个应用程序的种子文件。

我想了解其他人如何解决这个问题。

我知道这个线程是旧的,但是我今天早些时候需要这样做,并提出一个小解决方案,可能会帮助其他人有相同的问题。

@mike buhot答案很好,但我不想创建一个任务或模块,我还想使用seeds.exs.exs.exs文件,该文件中存在于顶级Priv文件夹中。只需将以下片段添加到您的顶级种子中。exs即可,它将运行您在伞应用程序上可能拥有的所有种子。

umbrella_directory = "./apps/"
seeds_path = "/priv/repo/seeds.exs"
umbrella_directory
|> File.ls!()
|> Enum.filter(&File.dir?(Path.join(umbrella_directory, &1)))
|> Enum.each(fn(directory) ->
  app_seeds = Path.join([umbrella_directory, directory, seeds_path])
  case File.exists?(app_seeds) do
    true -> Mix.Tasks.Run.run([app_seeds])
    _ -> :ok
  end
end)

然后运行 $ mix run priv/repo/seeds.exs将有能力。

自定义递归混音任务可能有效

defmodule Mix.Tasks.Ecto.Seed do
  use Mix.Task
  @recursive true
  def run(_args) do
    Mix.Tasks.Run.run(["priv/repo/seeds.exs"])
  end
end

最新更新