Triplex with transactions always returns error on migrate

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.migrate to run within a transaction. Considering create_schema will 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:

    Triplex.create_schema(slug, Repo, fn tenant, repo ->
      {:ok, _} = Triplex.migrate(tenant, repo)
  
      Repo.transaction(fn ->
        # do other inserts etc. here... such as...
        with {:ok, user} <- insert_some_default_user_on_schema_creation(),
             {:ok, _something_else} <- do_something_else() do
          {:ok, user}
        else
          {:error, error} ->
            Repo.rollback(error)
        end
      end)
    end)
2 Likes