       where: r.gate_type == "percentage"
     )
 
-    transaction_fn = case db_type() do
-      :postgres -> &_transaction_with_lock_postgres/1
-      :mysql -> &_transaction_with_lock_mysql/1
-    end
-
-    out = transaction_fn.(fn() ->
-      case ecto_repo().one(find_one_q) do
+    repo = ecto_repo()
+
+    transaction_fn = case db_type(repo) do
+      :postgres -> &_transaction_with_lock_postgres/2
+      :mysql -> &_transaction_with_lock_mysql/2
+    end
+
+    out = transaction_fn.(repo, fn() ->
+      case repo.one(find_one_q) do
         record = %Record{} ->
           changeset = Record.update_target(record, gate)
-          do_update(flag_name, changeset)
+          do_update(repo, flag_name, changeset)
         nil ->
           changeset = Record.build(flag_name, gate)
-          do_insert(flag_name, changeset)
+          do_insert(repo, flag_name, changeset)
       end
     end)
 

  end


  defp db_type do
    case ecto_repo().__adapter__() do
      Ecto.Adapters.Postgres -> :postgres
      Ecto.Adapters.MySQL -> :mysql # legacy, Mariaex
      Ecto.Adapters.MyXQL -> :mysql # new in ecto_sql 3.1

  end


  defp mysql_unlock!(repo) do
    result = Ecto.Adapters.SQL.query!(
      repo,
      "SELECT RELEASE_LOCK('fun_with_flags_percentage_gate_upsert');"
    )

