Skip to content

Commit d8833a0

Browse files
authored
Add mix release example to README
As discussed in midas#8
1 parent 915eec4 commit d8833a0

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed

README.md

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,3 +188,57 @@ Use the module in the production app's remote console.
188188
```elixir
189189
MyApp.Deployment.Seeder.seed(tags: ~w(things stuff)a)
190190
```
191+
### Seeding in production when using mix release
192+
When using `mix release`, mix itself will not be available in your release. The module below provides an example of a module that does not use mix.
193+
```elixir
194+
defmodule MyApp.Deployment.Seeder do
195+
import Ecto.Migrator, only: [migrations_path: 2, with_repo: 2]
196+
197+
@app :my_app
198+
199+
def seed(opts \\ [], seeder \\ &PhilColumns.Seeder.run/4) do
200+
load_app()
201+
# set env with current_env/0 overwriting provided arg
202+
opts = Keyword.put(opts, :env, current_env())
203+
opts = Keyword.put(opts, :tags, [])
204+
205+
opts =
206+
if opts[:to] || opts[:step] || opts[:all],
207+
do: opts,
208+
else: Keyword.put(opts, :all, true)
209+
210+
opts =
211+
if opts[:log],
212+
do: opts,
213+
else: Keyword.put(opts, :log, :info)
214+
215+
opts =
216+
if opts[:quiet],
217+
do: Keyword.put(opts, :log, false),
218+
else: opts
219+
220+
for repo <- repos() do
221+
{:ok, _, _} = with_repo(repo, &seeder.(&1, migrations_path(&1, "seeds"), :up, opts))
222+
end
223+
end
224+
225+
defp current_env do
226+
## Add this to config/config.exs:
227+
##
228+
## config :my_app, env: config_env()
229+
Application.fetch_env!(@app, :env)
230+
end
231+
232+
defp repos do
233+
Application.fetch_env!(@app, :ecto_repos)
234+
end
235+
236+
defp load_app do
237+
Application.load(@app)
238+
end
239+
end
240+
```
241+
It can be run from the remote console or on the command line like this:
242+
```
243+
bin/my_app eval "MyApp.Deployment.Seeder.seed(tags: ~w(things stuff)a)"
244+
```

0 commit comments

Comments
 (0)