方法一: 直接执行

CHAT_SERVER=/srv/chat_server/releases/0.1.0/bin/chat_server

$CHAT_SERVER rpc               \
Elixir.Application app_dir     \
"[chat_server, <<\"/priv/repo/migrations\">>]." 

# Using /srv/chat_server/releases/0.1.0/releases/0.1.0/chat_server.sh
# <<"/srv/chat_server/releases/0.1.0/lib/chat_server-0.1.0/priv/repo/migrations">>
$ $CHAT_SERVER rpc Elixir.Ecto.Migrator run                                               \ 
    "['Elixir.ChatServer.Repo',                                                           \
    <<\"/srv/chat_server/releases/0.1.0/lib/chat_server-0.0.1/priv/repo/migrations\">>,   \
    up, [{all, true}]]."

# Using /srv/chat_server/releases/0.1.0/releases/0.1.0/chat_server.sh
# [20160427034905,20160429032218,20160429091703,20160429093449,20160504091600,20160509040913]
migrations
    ├── 20160427034905_create_table_user.exs
    ├── 20160429032218_create_table_authorization.exs
    ├── 20160429091703_create_table_chat_group.exs
    ├── 20160429093449_create_table_chat_group_user.exs
    ├── 20160504091600_create_table_user_contact.exs
    └── 20160509040913_create_table_chat_dialog.exs 

方法二: 需要编写任务

defmodule :release_tasks do
  @moduledoc """
  http://blog.plataformatec.com.br/2016/04/running-migration-in-an-exrm-release/
  """
  def migrate do
    {:ok, _} = Application.ensure_all_started(:chat_server)
    path = Application.app_dir(:chat_server, "priv/repo/migrations")
    Ecto.Migrator.run(ChatServer.Repo, path, :up, all: true)
    :init.stop()
  end

  def rollback do
    {:ok, _} = Application.ensure_all_started(:chat_server)
    path = Application.app_dir(:chat_server, "priv/repo/migrations")
    Ecto.Migrator.run(ChatServer.Repo, path, :down, all: true)
    :init.stop()    
  end
end

执行移植

rel/chat_server/bin/chat_server command release_tasks migrate

developerworks
1.7k 声望266 粉丝

引用和评论

0 条评论