ravecat

ravecat

Implement complex logic for multi-tenancy solution: create team → create schema → run migration on new schema. Found a thread and a mention in Triplex documentation and moved the migrations from the transaction. Method look like

  def create_team(attrs \\ %{}) do
    changeset =
      %Team{}
      |> change(%{uid: generate_token(type: :schema)})
      |> Team.changeset(attrs)

    Multi.new()
    |> Multi.insert(:team, changeset)
    |> Multi.run(:schema, fn repo, %{team: team} ->
      Triplex.create_schema(team.uid, repo)
    end)
    |> Repo.transaction()
    |> case do
      {:ok, %{team: team}} ->
        {:ok, _} = Triplex.migrate(team.uid)

        {:ok, team}

      {:error, _, reason, _} ->
        {:error, reason}
    end
  end

It work flawless inside iex

iex(97)> Runa.Teams.create_team(%{title: “test”})
[debug] QUERY OK db=0.4ms idle=1327.3ms
begin
↳ Runa.Teams.create_team/1, at: lib/domains/teams/teams.ex:82
[debug] QUERY OK source=“teams” db=0.5ms
INSERT INTO “teams” (“title”,“uid”,“inserted_at”,“updated_at”) VALUES ($1,$2,$3,$4) RETURNING “id” [“test”, “nm8qnu26poykd2y02e0dup5wnf168qz2”, ~U[2024-05-20 17:55:36Z], ~U[2024-05-20 17:55:36Z]]
↳ Runa.Teams.create_team/1, at: lib/domains/teams/teams.ex:82
[debug] QUERY OK db=0.1ms
CREATE SCHEMA “nm8qnu26poykd2y02e0dup5wnf168qz2”
[debug] QUERY OK db=14.5ms
commit
↳ Runa.Teams.create_team/1, at: lib/domains/teams/teams.ex:82
[info] == Running 20240420161100 Runa.Repo.Migrations.CreateContributors.up/0 forward
[info] execute “CREATE TABLE nm8qnu26poykd2y02e0dup5wnf168qz2.contributors (\n id serial PRIMARY KEY,\n user_id integer REFERENCES public.users(id) ON DELETE CASCADE NOT NULL,\n team_id integer REFERENCES public.teams(id) ON DELETE CASCADE NOT NULL,\n role_id integer REFERENCES public.roles(id) ON DELETE CASCADE NOT NULL,\n inserted_at timestamp NOT NULL,\n updated_at timestamp NOT NULL\n);\n”
[info] execute “CREATE UNIQUE INDEX contributors_user_id_team_id_role_id_index\nON nm8qnu26poykd2y02e0dup5wnf168qz2.contributors (user_id, team_id, role_id);\n”
[info] == Migrated 20240420161100 in 0.0s
[info] == Running 20240520093137 Runa.Repo.Migrations.CreateProjectsTable.up/0 forward
[info] create table nm8qnu26poykd2y02e0dup5wnf168qz2.projects
[info] == Migrated 20240520093137 in 0.0s
{:ok,
%Runa.Teams.Team{
meta: #Ecto.Schema.Metadata<:loaded, “teams”>,
id: 53,
title: “test”,
uid: “nm8qnu26poykd2y02e0dup5wnf168qz2”,
team_roles: #Ecto.Association.NotLoaded,
users: #Ecto.Association.NotLoaded,
inserted_at: ~U[2024-05-20 17:55:36Z],
updated_at: ~U[2024-05-20 17:55:36Z]
}}

But inside test in returns error, I use create_project inside the fixture and create a entity, schema and migrations before each test

as I see it migrations cannot be performed in a test environment, but Triplex.exists?(team.uid) return true, Triplex.migrate("public") return {:ok, _}

  1. test projects returns all projects (Runa.ProjectsTest)
    test/domains/projects_test.exs:16
    ** (MatchError) no match of right hand side value: {:error, “could not checkout the connection owned by pid<0.610.0>. When using the sandbox, connections are shared, so this may imply another process is using a connection. Reason: connection not available and request was dropped from queue after 822ms. You can configure how long requests wait in the queue using :queue_target and :queue_interval. See DBConnection.start_link/2 for more information”}
    stacktrace:
    (runa 0.1.0) lib/domains/teams/teams.ex:71: Runa.Teams.create_team/1
    (runa 0.1.0) test/support/fixtures/teams_fixtures.ex:25: Runa.TeamsFixtures.create_aux_team/1
    Runa.ProjectsTest.__ex_unit_describe_0/1

Is this a Triplex problem or a test environment problem?

Showing Posts 1 to 2

ashrafhasson

ashrafhasson

Hi @ravecat I’ve stumbled upon this too but not the testing part. I didn’t think this would work in iex (or in a transaction) but then it seems it’s only Triplex.create that cannot run inside of a transaction since Ecto 3 :thinking:

However, running the Triplex.create_schema this way inside a multi transaction means it may leave unclean teams’ schemas should the Triplex.migrate fail later on.
If you use the Triplex.create_schema func argument to wrap the transaction instead, you might be able to avoid that side effect.

Regarding the failure in your test, I wonder whether your fixture is spawning a process that’s not dropping the connection before it’s checked out again by the test process? It would be useful to share your fixture and test code here. Also a total speculation is whether you have multiple of these tests in different modules with async turned on ..

ashrafhasson

ashrafhasson

and apparently Sandboxed SQL in tests share the connection with the parent process while transaction spawned functions will not be part of the parent process and will hold their own connection (if they can?)

The transaction is per process. A separate process started inside a transaction won’t be part of the same transaction and will use a separate connection altogether.

.. the parent process will typically hold the connection until the transaction completes. This may lead to a deadlock if the child process attempts to use the same connection.

So maybe your test is getting stuck in that deadlock?

— All posts loaded —

Where Next? Top

Trending in Questions Top

Blokh
Hey guys, I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly Do you guys have any suggestions what is the best prac...
New
kszambelanczyk
Hello! Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app. I creat...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
matt-savvy
Anyone here using Honeybadger? My Honeybadger account is being overwhelmed with noise from some bots. Seeing a lot of Bandit.HTTPError...
New
velrest
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
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews