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

hariharasudhan94
I would like to know what is the best IDE for elixir development?
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
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
marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
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
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New

Other popular topics Top

Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
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
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 54996 245
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New

We're in Beta

About us Mission Statement