1

我有一个包含 3 个伞形项目的长生不老药应用程序。我正在通过酿酒厂创建它的二进制文件(发布)。

运行此命令会在_build/prod/rel/se/releases/0.1.0中创建 .tar.gz 文件:

MIX_ENV=prod 混合发布--env=qa

我能够提取并运行该应用程序。为了运行 ecto 迁移,我为发布任务添加了这个模块 [通过遵循https://hexdocs.pm/distillery/running-migrations.html ]:

defmodule Se.ReleaseTasks do

  @start_apps [
    :postgrex,
    :ecto
  ]

  def myapp, do: Application.get_application(__MODULE__)

  def repos, do: Application.get_env(myapp(), :ecto_repos, [])

  def seed() do
    me = myapp()

    IO.puts "Loading #{me}.."
    # Load the code for myapp, but don't start it
    :ok = Application.load(me)

    IO.puts "Starting dependencies.."
    # Start apps necessary for executing migrations
    Enum.each(@start_apps, &Application.ensure_all_started/1)

    # Start the Repo(s) for myapp
    IO.puts "Starting repos.."
    Enum.each(repos(), &(&1.start_link(pool_size: 1)))

    # Run migrations
    migrate()

    # Run seed script
    Enum.each(repos(), &run_seeds_for/1)

    # Signal shutdown
    IO.puts "Success!"
    :init.stop()
  end

  def migrate, do: Enum.each(repos(), &run_migrations_for/1)

  def priv_dir(app), do: "#{:code.priv_dir(app)}"

  defp run_migrations_for(repo) do
    app = Keyword.get(repo.config, :otp_app)
    IO.puts "Running migrations for #{app}"
    Ecto.Migrator.run(repo, migrations_path(repo), :up, all: true)
  end

  def run_seeds_for(repo) do
    # Run the seed script if it exists
    seed_script = seeds_path(repo)
    if File.exists?(seed_script) do
      IO.puts "Running seed script.."
      Code.eval_file(seed_script)
    end
  end

  def migrations_path(repo), do: priv_path_for(repo, "migrations")

  def seeds_path(repo), do: priv_path_for(repo, "seeds.exs")

  def priv_path_for(repo, filename) do
    app = Keyword.get(repo.config, :otp_app)
    repo_underscore = repo |> Module.split |> List.last |> Macro.underscore
    Path.join([priv_dir(app), repo_underscore, filename])
  end
end

应用程序使用位于我们需要迁移的伞形项目之一中的代码运行和编译。编译并启动服务器后,当我尝试通过以下方式运行它时:

bin/se_cloud 命令 Elixir.Se.ReleaseTasks 种子

我收到此错误:

Elixir.Se.ReleaseTasks.seed 未定义或具有非零参数

有没有其他人遇到过这个问题?或者我在这里配置错误?

4

2 回答 2

2

不要直接在终端中运行命令,而是将其放在以下脚本文件中rel/commands/migrate.sh

#!/bin/sh

$RELEASE_ROOT_DIR/bin/se command Elixir.Se.ReleaseTasks seed

然后在发布配置中注册您的自定义命令:

release :se do
  ...
  set commands: [
    "migrate": "rel/commands/migrate.sh"
  ]
end

您现在应该可以使用以下命令运行它:

bin/se migrate
于 2018-04-04T13:57:54.190 回答
0

Se.ReleaseTasks module should put under the lib folder that mix can compile it. For umbrella project, you can follow like below code structure:

project
 - apps
   - api-app
     - lib
       - Release.ex
   - ecto-app
 - rel

Here is an example.

于 2019-03-19T03:19:25.787 回答