Which functions are called 'under the hood' when using Ecto.Repo in elixir

I am trying to get a better understanding of Ecto adapters in elixir. I have begun trying to build my own adapter using Ecto.Adapters.Postgres as a base. This seemed like a good choice to start with as it is the default adapter used with Phoenix.

I can use my adapter in my own projects now by updating the the following line in my project’s repo file…

defmodule UsingTestAdapter.Repo do
  use Ecto.Repo,
    otp_app: :using_test_adapter,
    adapter: TestAdapter  # <------ this line

At the moment it has the same functionality as the postgres adapter. I have been trying to edit some of the functions found in the Ecto.Adapters.Postgres.Connection and I have realised that they do not work quite how I expected.

The insert function for example does not actually use the params passed into Repo.insert.

To make this a little more clear imagine we have the following table, Comments

| id | comment |
| -- | ------- |

Now Repo.insert(%Comments{comment: "hi"}) is called.

I want to modify the adapter, so that it ignores the “hi” value that is passed in and instead inserts a comment of “I am the adapter, and I control this database. Hahaha (evil laugh)”…

| id | comment                                                            |
| -- | ------------------------------------------------------------------ |
| 1  | I am the adapter and I control this database. Hahaha (evil laugh)" |

However, the insert function does not appear to actually take the data to be stored as an argument.

My initial thought of what happened with ecto adapters was that when a user calls one of the repo functions it called the corresponding function in the Ecto.Adapters.Postgres.Connection module. This does appear to happen but other steps seem to be happening before this.

If anyone has a better understanding of the chain of functions that are called when Repo.insert (and any other Repo function) is called, please explain below.


Aside: Building a new MySQL adapter for Ecto (2018-11-14) might be useful.


Check the postgres connection insert method.

I think it is a good place to start, just put IEx.pry and start playing :slight_smile:

Would be great to explore your solution for a such task.