通过从酿酒厂

时间:2018-04-04 13:32:57

标签: migration elixir ecto distillery

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

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

  

MIX_ENV = prod mix release --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 seed

我收到此错误:

  

Elixir.Se.ReleaseTasks.seed未定义或非零   元数

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

2 个答案:

答案 0 :(得分:1)

不是直接在终端中运行命令,而是将其放在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

答案 1 :(得分:0)

Se.ReleaseTasks模块应放在混合可编译它的lib文件夹下。 对于伞项目,您可以按照以下代码结构进行操作:

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

这里是example