ragamuf

ragamuf

Elixir Blog Post: Speed up Multitenant Migrations with Database Schema Snapshots

Who doesn’t want to go faster? Migrations in Elixir with Ecto are already quick. Still, we can go faster, especially when we need to onboard customers repeatedly in multi-tenant architectures.

Most Liked

cjbottaro

cjbottaro

Neat!

We do something very similar. We have a “template” Pg schema that we dump/load on demand when a new tenant is created. The name of the schema is "__schema_$template", which is extremely likely to be to be unique when we run String.replace (or sed, can’t remember) on the dump output.

I’m a little confused how your #{prefix} works. Is that literally the name of your Pg schema?

One thing that I love about Ecto is how easy it is to do multitenancy. Between get_dynamic_repo, and default_options, it’s pretty easy to handle multietenancy (besides migrations, but that’s a story for another day).

Contrast that to Rails where they implemented first class support for multitenancy, but it’s very prescriptive about how it works… and further it makes a connection pool per tenant:man_facepalming:

ragamuf

ragamuf

Glad to hear that you have a similar process working well for you. Reusing the connection pool is a big deal. The prefix is just a placeholder like yours. I could have named it anything. The snapshot function returns a function that takes a specific prefix as an argument. This creates the unique schema for each tenant.

Did you run into any SSL invoking psql from System.cmd?

cjbottaro

cjbottaro

Nope, no SSL issues at all. We just use the Amazon managed certs for RDS and everything just works.

Where Next?

Popular in Blog Posts Top

stryrckt
Support for AlpineJS in LiveView was added in 0.13.3 and it works fabulously. I just wrote a blog article about it and plan another one s...
New
sheharyarn
In this article, I talk about the Elixir library Delta we at Slab just open-sourced, its various features including support for Operation...
New
brainlid
You are storing some Phoenix LiveView state in the browser. You want to retrieve that saved state as early as possible to improve the use...
New
ragamuf
Does the world need another How to create a blog article? Maybe not. But then again, creating something out of nothing is what we love....
New
bemesa21
Add drag-and-drop functionality to your Phoenix LiveView app with SortableJS and LiveView Hooks! This posts shows you how to create an in...
New
marcin
Hi! :waving_hand: I wanted to refresh my knowledge on how to mix phx.gen.auth with local password users db, as well as OAuth providers s...
New
brainlid
Phoenix LiveView v0.20.4 offers a hidden gem with its handy little JS.toggle_class/1 feature. This addition simplifies toggling classes f...
New

Other popular topics Top

electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 54006 488
New
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
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
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

We're in Beta

About us Mission Statement