首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Docker Distillery版本运行迁移

使用Docker Distillery版本运行迁移
EN

Stack Overflow用户
提问于 2017-10-29 17:47:33
回答 1查看 443关注 0票数 0

我正在尝试部署我的Phoenix 1.3应用程序。我已经设置了mix docker发布版本。现在我正在弄清楚如何运行迁移。

我决定采用定制发布命令的方法。我创建了这样的MigrationTask:

代码语言:javascript
复制
  defmodule SfiBackend.ReleaseTasks do
    @start_apps [
      :crypto,
      :ssl,
      :postgrex,
      :ecto,
      :logger
    ]

    def myapp do
      Application.ensure_started(:sfi_backend)
      Enum.each(@start_apps, fn app -> Application.ensure_started(app) end)
      Application.get_application(__MODULE__) |> IO.inspect(label: "My application:")
    end

    def repos, do: Application.get_env(myapp(), :ecto_repos, []) |> IO.inspect(label: "Repose:")

    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() |> IO.inspect(), &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

从技术上讲,它应该运行,因为我从Distillery Repo复制了它,但事实并非如此。

我收到这样的错误:

代码语言:javascript
复制
My application:: :sfi_backend
Repose:: [SfiBackend.Repo]
[SfiBackend.Repo]
Running migrations for sfi_backend
init terminating in do_boot ()
{"init terminating in do_boot",{#{'__exception__'=>true,'__struct__'=>'Elixir.ArgumentError',message=><<"argument error">>},[{ets,lookup_element,['Elixir.Ecto.Registry',nil,3],[]},{'Elixir.Ecto.Registry',lookup,1,[{file,"lib/ecto/registry.ex"},{line,18}]},{'Elixir.Ecto.Adapters.SQL',sql_call,6,[{file,"lib/ecto/adapters/sql.ex"},{line,251}]},{'Elixir.Ecto.Adapters.SQL','query!',5,[{file,"lib/ecto/adapters/sql.ex"},{line,198}]},{'Elixir.Ecto.Adapters.Postgres','-execute_ddl/3-fun-0-',4,[{file,"lib/ecto/adapters/postgres.ex"},{line,85}]},{'Elixir.Enum','-reduce/3-lists^foldl/2-0-',3,[{file,"lib/enum.ex"},{line,1755}]},{'Elixir.Ecto.Adapters.Postgres',execute_ddl,3,[{file,"lib/ecto/adapters/postgres.ex"},{line,85}]},{'Elixir.Ecto.Migrator','-migrated_versions/2-fun-0-',2,[{file,"lib/ecto/migrator.ex"},{line,44}]}]}}

Crash dump is being written to: erl_crash.dump...done
EN

回答 1

Stack Overflow用户

发布于 2017-11-01 12:11:30

从堆栈跟踪中,它显示它在按名称查找后无法找到您的repo:

下面是ecto2.2中的lookup函数:

代码语言:javascript
复制
def lookup(repo_name) do
  pid = GenServer.whereis(repo_name)
  :ets.lookup_element(__MODULE__, pid, 3)
end

如果pidnil,这将导致参数错误。看起来您是在seed中启动Repos,而不是在migrate

代码语言:javascript
复制
  # Start the Repo(s) for myapp
  IO.puts "Starting repos.."
  Enum.each(repos(), &(&1.start_link(pool_size: 1)))

将该代码分解到另一个函数中并从migrate调用它,以确保启动Repos。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46998796

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档