SCJR

SCJR

Custom Mix Tasks

Hoping to get some insight from those who work on Elixir code in production or in their side projects - what do you write custom Mix tasks for?

Elixir is my first language and I have no working experience as an SE, so I was curious about others who have implemented this feature in a practical way?

Most Liked

awerment

awerment

We have written a custom code generator mix task. Working with external APIs, we wrote the same set of API provider-specific modules every time a new integration would be added.

The generator is simple/naive in its implementation, but it saves hours of repetitive copying, pasting and adjusting. It spits out the modules alongside some unit tests and prints out a to-do list of tasks that need to be performed manually.

The task itself was maybe half a day‘s work and has paid for that time investment many times over by now. (Refactoring the code base to a point where it‘s extensible in a way that is so straightforward that a codegen is even an option was naturally a bit more involved, but that is a different story :slight_smile: )

03juan

03juan

That kind of story makes for an invaluable article for many still learning and improving in their Elixir journey… :wink:

03juan

03juan

Custom tasks are good to formalize data migrations that should run as part of a multi-step migration strategy when changing DB and Ecto schema structures in large database projects. It’s a good way to plan for, check, execute, and test data changes at every step. Then they can be moved to a history branch for posterity when the migrations succeed.

Last Post!

awerment

awerment

That sounds interesting, would you mind elaborating a bit on what the steps a custom task might help with would be? I assume you mean “bundling” multiple migrate, update, verify operations or is it something else?

We also have a home-grown, so to speak, schema-based multi-tenancy lib that comes with custom mix tasks for handling schema creation/migration for configured tenants (plus the public schema). The actual logic is in separate modules, so that it is callable via mix, e.g. mix app.migrate as well as the release, e.g. /app/release_binary eval 'ReleaseTasks.migrate()', so that it works locally in dev and when deploying.

The lib itself is pretty brute-force I’d say, essentially wrapping all the relevant Ecto.Repo functions, turning the :prefix option into a required parameter, but it works really well for our needs with a minimal amount of magic. Nowadays we could probably use one of the available multi-tenancy solutions, but I think it predates those and we haven’t had the need to switch yet.

As for the potential for an article… Hopefully we’ll get to that, some time. Would need to make up a messy, complex enough use case to walk through. :sweat_smile: In a nutshell: use the language constructs available - protocols, behaviours (with default, selectively overridable implementations), macros where you need them. In some cases we resorted to just generating function/test clauses in a for comprehension, iterating over some list from the app’s configuration. :man_shrugging:

As a general theme, I’m very happy with the flexibility working with Elixir/Mix affords. Building entirely custom solutions as needed almost never feels hacky, because you’re working with the tools/frameworks, not against them, if that makes sense.

Where Next?

Popular in Questions Top

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
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New

Other popular topics Top

jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
AngeloChecked
What learn first? Rust or Elixir Hi Elixir community! I’m here because i want learn a new language. I’m a junior developer and mainly i ...
New