ymtszw

ymtszw

Equivalent to distillery's boot hooks in mix release? (Elixir 1.9)

Distillery has Boot Hooks, which can be utilized to e.g. run ecto migration scripts before application startup during deployment.

I am basically following “Running Migrations” guide in distillery, wiring the task onto pre-start hook.

Now that Elixir 1.9 and mix release come out, are there similar mechanisms?

Marked As Solved

josevalim

josevalim

Creator of Elixir

None (necessarily). You can use eval to run any code you want, including what Distillery would call a custom command (not to confuse with Distillery custom hooks). We have an example on the new Phoenix releases guides: phoenix/guides/deployment/releases.md at main · phoenixframework/phoenix · GitHub (not yet deployed).

Ecto v3.0 actually locks the migration table when running migrations, so this is safe unless you disable the migration lock.

Also Liked

josevalim

josevalim

Creator of Elixir

We don’t have hooks on purpose. :slight_smile:

You can run them when your application starts, as proposed by @LostKobrakai. Alternatively, you can add your own script to the bin directory, that you use to run migrations and then start the release. You can do this by adding a step to your mix.exs:

def project do
  [
    ...,
    releases: [
      my_app: [
        steps: [:assemble, &copy_bin_files/1]
      ]
    ]
  ]
end

where:

defp copy_bin_files(release) do
  File.cp_r("rel/bin", Path.join(release.path, "bin"))
  release
end

And you put the extra scripts in rel/bin.

LostKobrakai

LostKobrakai

Couldn’t you run them as part of application startup but before starting handling user input (like e.g. starting the phoenix endpoint)

ymtszw

ymtszw

I will examine both approaches. Thank you @LostKobrakai and @josevalim.

I am slightly inclined to “bring scripts with release” method since I would like to decouple scripts from application implementation.

BTW,

I am curious about the rationale here. Simplification, perhaps?

LostKobrakai

LostKobrakai

Imo the “steps” abstraction is more powerful and composable. One could probably build a small purpose build library, which handles all the dirty work of such boot hooks and supply its assembly to the steps listed in mix.exs.

hubertlepicki

hubertlepicki

I think migrations run wrapped in a transaction (one transaction per migration if I recall) but yes, that’s a concern since I can imagine situation where with multiple application instances starting up things can go sideways.

Where Next?

Popular in Questions Top

JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
wernerlaude
In AR this is so simple @articles = current_user.articles How to do in Ecto? def index(conn, _params) do current_user = conn.assig...
New
idi527
I’ve been re-reading swift book again and noticed that multiline strings there don’t have a trailing line break, unlike in elixir iex(2)...
New

Other popular topics Top

chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 30048 115
New
peerreynders
Manning 2016 Halloween weekend sale via Deal of the Day Friday, October 28 - Half off all MEAPs - code WM102816LT Saturday, October 29 ...
326 29600 154
New
sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list....
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

We're in Beta

About us Mission Statement