Narven
Hi,
For the last 2 days, I’ve be trying to implement Triplex to add multi-tenancy to my application, and I use transactions a lot, usually with Ecto.Multi.
From the documentation it looks like with triplex I need to use:
Triplex.create_schema(slug, Project.Repo, fn(tenant, repo) ->
Repo.transaction(fn ->
mig = Triplex.migrate(tenant, repo)
IO.inspect(mig, label: "MIGRATE")
// Add any ecto operation here...
tenant
end)
end)
My problem is that Triplex.migrate(tenant, repo) is always, always returning:
{:error,
"ERROR 42P01 (undefined_table) relation \"prj_c24.schema_migrations\" does not exist\n\n query: INSERT INTO \"prj_c24\".\"schema_migrations\" (\"version\",\"inserted_at\") VALUES ($1,$2)"}
For me this is an extreme confusing error. It tells nothing about what is happening, (at least for me).
My only migration is this:
defmodule Project.Repo.Migrations.CreateUsersTables do
use Ecto.Migration
def change do
# execute "CREATE EXTENSION IF NOT EXISTS citext", ""
create table(:users, primary_key: false) do
add(:id, :uuid, primary_key: true, null: false)
# add(:email, :citext, null: false)
add(:hashed_password, :string, null: false)
add(:first_name, :string, null: false)
add(:last_name, :string, null: false)
add(:is_enabled, :boolean, null: false, default: true)
add(:dob, :date)
add(:photo, :string, default: "/images/empty.png")
add(:confirmed_at, :naive_datetime)
timestamps()
end
# create unique_index(:users, [:email])
end
end
Any help on that error? thanks
PS: The prefix is being created in the database. But migrations are not running.
❯ elixir --version
Erlang/OTP 24 [erts-12.1.5] [source] [64-bit] [smp:16:16] [ds:16:16:10] [async-threads:1] [jit] [dtrace]
Elixir 1.13.0 (compiled with Erlang/OTP 24)
{:triplex, "~> 1.3.0-rc.1"}
Trending in Questions
Hello!
Suppose you are building workflow (order / task / payment) processing system with the following requirements:
Each workflow con...
New
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app?
Looking for hints regarding:
Addi...
New
Kia ora,
We have been using elixir-google-api to connect to Google Drive. However, with the updates to Tesla due to CVEs this is now bro...
New
Hi all, I wanted to ask how the community is dealing with post-release steps.
Today we have Ecto migrations, which make sure that the db...
New
Hello,
I have an Elixir backend that implements a custom protocol over TCP. I want to load test the backend and assess the performance o...
New
Other Trending Topics
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
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve.
They are GUI (Emerge) and State management (S...
New
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
There are three potential reasons for members of this forum to have a look at https://vutuv.de
You are tired or annoyed of LinkedIn.
Yo...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #blog-post
- #phoenix_html
- #iex
- #graphql
- #ai
- #genstage
- #elixirconf-us
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #security
- #hex










First 10 of 11 Posts
al2o3cr
The error is complaining because the
schema_migrationstable isn’t present in the tenant schema.That doesn’t make a ton of sense -
Triplex.migratecallsEcto.Migrator.runwhich ensures that table existsIs there anything useful in the Elixir or DB logs?
Narven
Yeah.. I was just looking at that. it looks like for some reason that “schema_migrations” is not being created on the tenants side.
Could it be because i have both “migrations” and “tenant_migrations” inside of “repo” folder ?
I was trying to see if there was a way to prevent the rollback. so that I could see the last state.
Narven
From the logs:
al2o3cr
Getting to the
== Running 2021etcetcetcbit suggests that the code believes it has created theschema_migrationstable.One thought: this pattern looks like the one mentioned in the Triplex docs, but I wonder if that actually works. Maybe the
schema_migrationstable is being inserted in a separate transaction somehow; that would produce the observed behavior:Repo.transactionin your posted codeTriplex.migratecalls intoEcto.Migrator, which starts a transaction in another process (the migration lock holder?)Ecto.Migratortries to read fromschema_migrationsand failsNarven
One thing I dont understad is that if I go and run
iex -S mix phx.serverand on iex i run:
It works flawless.
If I run
Triplex.create("foo")directly on the code… Does do nothing.Because for some weird reason according to the docs we have to run
create_schema. But on IEX just runningcreateworks perfectly.Narven
If I do something simple like:
it works. I think that the issue is only when they are running inside a migration.
al2o3cr
Yeah, that’s what makes me suspicious about the
Repo.transactionin the middle -Triplex.createis defined in a way that’s equivalent to:Re: your last message about running inside a migration - if this code is inside a migration’s
def up, then there’s already a transaction wrapped around the whole thing. You may need to disable that with@disable_ddl_transaction true.Narven
It looks like adding
@disable_ddl_transaction trueto the migration and doing like this:returns:
Still shows the error, which technally is an error, but in this case I dont think it should show has an error.
I think this is more like… I cannot find schema_migrations… so I’m going to create it. Or maybe not.
But for now it looks like is working… I will add tomorrow all the extra removed migrations and add the rest of the transactions to see how it goes.
Thanks @al2o3cr
Narven
After more testing… this lib does not work at all.
This working if there is only
onemigration, after that for some reason it throws the errorand no other migration get’s run:
i’ve currently have 2 migrations and this is what happens:
PS: added some extra debug lines to the actual library
§riebeekn
I think the inner transaction is the issue. As mentionned in the Triplex docs the way migrations run was changed in Ecto 3. Since that change I haven’t ever been able to get
Triplex.migrateto run within a transaction. Consideringcreate_schemawill do a schema drop on any{:error, some_err_message}result… not using a transaction seems an alright way of going about things.So I would try something along the lines of: