   end
 
 
-  defp do_insert(flag_name, changeset, options \\ []) do
+  defp do_insert(repo, flag_name, changeset, options \\ []) do
     changeset
-    |> ecto_repo().insert(options)
+    |> repo.insert(options)
     |> handle_write(flag_name)
   end
 
 
-  defp do_update(flag_name, changeset, options \\ []) do
+  defp do_update(repo, flag_name, changeset, options \\ []) do
     changeset
-    |> ecto_repo().update(options)
+    |> repo.update(options)
     |> handle_write(flag_name)
   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})"
    )


      where: r.gate_type == "percentage"
    )

    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(repo, flag_name, changeset)
        nil ->
          changeset = Record.build(flag_name, gate)
          do_insert(repo, flag_name, changeset)
      end
    end)

