How do I create new prefixes in Ecto Migrations?

I’m doing some reading around prefixes and came across the very useful Multi tenancy with query prefixes — Ecto v3.6.2 (hexdocs.pm) but I’m a bit confused about how to create a prefix in the first place.

The linked article does so using a psql command but ideally I’d like this to be completed via Ecto as that is where the rest of the database set up is handled. Looking through the documentation for Ecto.Schema and Ecto.Migration I can’t find anything about creating a “prefix” schema.

Any pointers to the correct direction would be appreciated.

Repo.query!("CREATE SCHEMA #{name}")

But beware, that you need to be sure, that name is SQL-safe (IIRC you cannot use prepared statement param there).

Then you also need to run all migrations against that schema, as by default it will be empty.

1 Like

Super, that did the trick