   end
 
 
-  defp _transaction_with_lock_postgres(upsert_fn) do
-    ecto_repo().transaction fn() ->
-      postgres_table_lock!()
+  defp _transaction_with_lock_postgres(repo, upsert_fn) do
+    repo.transaction fn() ->
+      postgres_table_lock!(repo)
       upsert_fn.()
     end
   end
 
-  defp _transaction_with_lock_mysql(upsert_fn) do
-    ecto_repo().transaction fn() ->
-      if mysql_lock!() do
+  defp _transaction_with_lock_mysql(repo, upsert_fn) do
+    repo.transaction fn() ->
+      if mysql_lock!(repo) do
         try do
           upsert_fn.()
         rescue
           e ->
-            ecto_repo().rollback("Exception: #{inspect(e)}")
+            repo.rollback("Exception: #{inspect(e)}")
         else
           {:error, reason} ->
-            ecto_repo().rollback("Error while upserting the gate: #{inspect(reason)}")
+            repo.rollback("Error while upserting the gate: #{inspect(reason)}")
           {:ok, value} ->
             {:ok, value}
         after
           # This is not guaranteed to run if the VM crashes, but at least the
           # lock gets released when the MySQL client session is terminated.
-          mysql_unlock!()
+          mysql_unlock!(repo)
         end
       else
         Logger.error("Couldn't acquire lock with 'SELECT GET_LOCK()' after #{@mysql_lock_timeout_s} seconds")
-        ecto_repo().rollback("couldn't acquire lock")
+        repo.rollback("couldn't acquire lock")
       end
     end
   end

  end


  defp postgres_table_lock! do
    Ecto.Adapters.SQL.query!(
      ecto_repo(),
      "LOCK TABLE #{ecto_table_name()} IN SHARE ROW EXCLUSIVE MODE;"
    )
  end


  defp mysql_lock! do
    result = Ecto.Adapters.SQL.query!(
      ecto_repo(),
      "SELECT GET_LOCK('fun_with_flags_percentage_gate_upsert', #{@mysql_lock_timeout_s})"
    )


  @impl true
  def put(flag_name, gate = %Gate{}) do
    changeset = Record.build(flag_name, gate)
    repo = ecto_repo()
    options = upsert_options(repo, gate)

    case do_insert(repo, flag_name, changeset, options) do
      {:ok, flag} ->
        {:ok, flag}
      other ->
