DidactMacros

DidactMacros

Multi-tenancy: Phx.gen.auth, Ecto.Migration.execute/1 specifying the schema

I’m currently handling auth on multi-tenancy, is this approach to migration warranted? TLWR; I’m schema targeting everything including the execute function that resolves case sensitivity issues.

defmodule AuthTest.Repo.Migrations.CreateUsersAuthTables do
  use Ecto.Migration

  def change do
    Enum.each(~w{client_1 client_2 client_3}, fn 
      prefix -> 
        execute "CREATE EXTENSION IF NOT EXISTS citext WITH SCHEMA #{prefix}", ""

        create table(:users, prefix: prefix) do
          add :email, :citext, null: false
          add :hashed_password, :string, null: false
          add :confirmed_at, :utc_datetime

          timestamps(type: :utc_datetime)
        end

        create unique_index(:users, [:email], prefix: prefix)

        create table(:users_tokens, prefix: prefix) do
          add :user_id, references(:users, on_delete: :delete_all), null: false
          add :token, :binary, null: false
          add :context, :string, null: false
          add :sent_to, :string

          timestamps(type: :utc_datetime, updated_at: false)
        end

        create index(:users_tokens, [:user_id], prefix: prefix)
        create unique_index(:users_tokens, [:context, :token], prefix: prefix)
      end
    )
  end
end

Oops, apologies, I’ve amended the code above. Forgot to add the rest of the prefix key value pairs.

Marked As Solved

DidactMacros

DidactMacros

FYI I do believe this to be a correct form:

  def change do
    Enum.each(~w{schema_1 schema_2 schema_3}, fn
      prefix ->
        execute "CREATE EXTENSION IF NOT EXISTS citext WITH SCHEMA #{prefix}", ""

        create table(:users, primary_key: false, prefix: prefix) do
          add :id, :binary_id, primary_key: true
          add :email, :citext, null: false
          add :hashed_password, :string, null: false
          add :confirmed_at, :utc_datetime

          timestamps(type: :utc_datetime)
        end

        create unique_index(:users, [:email], prefix: prefix)

        create table(:users_tokens, primary_key: false, prefix: prefix) do
          add :id, :binary_id, primary_key: true
          add :user_id, references(:users, type: :binary_id, on_delete: :delete_all), null: false
          add :token, :binary, null: false
          add :context, :string, null: false
          add :sent_to, :string

          timestamps(type: :utc_datetime, updated_at: false)
        end

        create index(:users_tokens, [:user_id], prefix: prefix)
        create unique_index(:users_tokens, [:context, :token], prefix: prefix)
      end
    )
  end

I’m using it and it’s working.

Where Next?

Popular in Questions Top

vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New

Other popular topics Top

danschultzer
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch. This project took far...
548 29377 241
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 53690 245
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
marick
I had some trouble figuring out how to make many-to-many associations work. Once I got it working, I wrote a blog post. Because I’m a nov...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New

We're in Beta

About us Mission Statement