SCJR

SCJR

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?

Showing Posts 1 to 5

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.

SCJR

SCJR OP

I didn’t even consider the idea of leveraging it in with respect to third party integration.

Seems it’s easy to get bogged down earlier on with connecting the dots internally, I forget that the value of almost any non-trivial program is from interaction with outside processes/APIs and need to grow my mindset to consider that potential from the beginning.

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.

— All posts loaded —

Where Next? Top

Trending in Questions Top

katta
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
kpanic
Hi everyone, I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding. I sta...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
asweet-confluent
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
apz
I’m new to elixir and just tried to install the elixirLS extension for VScode(ium) and it is throwing some errors that I would like help ...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
budgie
A little off-topic, but I feel like people here have a good head on their shoulders. I used to be quite good at making software. Was luc...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews