alvinncx
Ever since I’ve started on Elixir, I’ve fell in love
with writing tests for my applications.
For the most part, writing tests for a regular Phoenix + Ecto + ExMachina stack has been a breeze since it is well documented.
Recently, I’ve been working on an application that is multi-tenant in nature and it’s been a pain to write tests with existing tools.
The current design is to have a different database schema for every tenant. I’m looking for advise for these questions:
- Should I set up and teardown the test database everytime?
- Should I use ExMachina here? It doesn’t seem to support prefixes yet.
Would be open to hear other advise as well (test methodology, approaches, etc). Thanks!
Trending in Questions
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
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
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
Documentation
While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
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
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
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
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
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
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
Hi there! We created Gust: A task orchestrator inspired by Airflow.
For those who have never heard about Aiflow, it’s a Python-based wor...
New
Hi everyone!
The first release candidate for the Expert language server project is now available!
We’ve published a press release detai...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #ai
- #elixirconf-us
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #elixirconf-eu
- #metaprogramming
- #hex










Most Liked- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
alvinncx
So I somewhat got this sorted out. Posting my experiences so maybe it will help someone out.
This post was helpful for me to structure the application for multi-tenancy.
A dive into database multi-tenancy in Elixir with Ecto. I also used the example github repo in the post as reference to help me.
There are a few pointers in case anyone out there gets stuck with the same issues:
First, I stopped using factories (ExMachina). I moved all my fixtures into a single module and used
Repo.insert!where ever I needed to interact. Since tests are ran in sandboxed environment I didn’t find a lot of advantages to continue using ExMachina, especially since it doesn’t support prefixes.At first I thought it would be a bad idea to teardown the database and migrate for each test suite… It turns out the tenant migrations are very fast so there isn’t a big hit to test productivity. What I did is add
ecto.dropto the start of the test script inmix.exs. Effectively I rebuild the database for every test run.Because of the issue in 2), I can’t really use
create extensionseffectively. This caused me to move row defaults into the application instead. Not a major issue, but something to take note of.Take note that schema migrations don’t currently work in a transaction using Ecto 3 Migrator. The problem is documented here
Ecto 3 Support · Issue #59 · ateliware/triplex · GitHub. This particular issue had me scratching my head for a long time, until I dug into github.
JohnnyCurran
We use multi tenancy and ExMachina. It’s fairly straightforward and you don’t have to ditch your factories.
You’re going to want to define an
EctoStrategymodule:dimitarvp
This might be a generic and not very helpful reply (sorry), but for posterity I feel obliged to mention Ecto.Adapters.SQL.Sandbox — Ecto SQL v3.14.0 – have you read through it? Have you tried its different modes of operation?
Last Post!
ricksonoliveira
The only way I found to solve the issue was to remove the umbrella structure, and the tests worked like a breeze. For us it was not a problem since we detached the API from the frontend and no longer would make use of the umbrella structure.