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
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
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
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










Showing Posts 1 to 9- 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.
ricksonoliveira
Thanks for the tips @alvinncx , I’m also working on the same type of application, and plus it’s an umbrella app, so I’m facing the issue which is: When running all tests as one
mix testit runs without problem, I believe it’s because it only opens one connection for everything. But when I run tests for s specific app, for one it runs succssefully, in this case the app that is responsible for the database. And when I run the tests for the web application it says thatcould not checkout the connection owned by #PID<xxxx>, it’s stressfull since I wasn’t able to find a solution which supports testing multi tenancy with umbrella structures. I wish I could get some reply from someone that knows about the problem. I don’t understand how can I manage the connections in this case so I’ll be able to run them as one and separately like when I’m developing for example. Any help? @josevalimdimitarvp
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?
ricksonoliveira
Thanks a lot for your reply @dimitarvp !
Yes, I have tried managing the checkout connections, and I feel like that’s the way to solve the problem, but I don’t know how, I have tried multiple different ways to checkout the connections with manual, auto and shared mode, but none have worked, as I said, they only work when all tests run together, but not for a specific test or a specfic app, in this case my web app.
dimitarvp
Hm. Reading through your OP again, it seems that you might have to get your hands dirty with Ecto dynamic repos and then combine that with the Ecto sandbox.
ricksonoliveira
Yes, I’m thinking to get rid of the umbrella structure, this will make the tests run a lot easier with multi tenancy.
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:ricksonoliveira
Seems like a nice solution, do you have any examples on how to use it with tests?
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.