通过从酿酒厂释放来运行ecto迁移

问题描述 投票:0回答:2

我有一个elixir应用程序与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种子

我收到此错误:

Elixir.Se.ReleaseTasks.seed未定义或具有非零的arity

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

migration elixir ecto distillery
2个回答
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

0
投票

Se.ReleaseTasks模块应该放在mix下可以编译它的lib文件夹下。对于伞形项目,您可以遵循以下代码结构:

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

这是一个example

© www.soinside.com 2019 - 2024. All rights reserved.