title: How to use Repo.transaction with raw sql queries with Ecto
body: <p>I am converting a Node.js app into Elixir and I am needing to use raw SQL queries within Ecto as I have some fairly complex SQL queries. I have about 10 that sit within a transaction.</p>
  
  <p>I am fairly new to Elixir as well and am trying to use Repo.transaction instead of trying to roll my own transactions.</p>
  
  <p>So I have about 10 of these:</p>
  
  <pre><code>raw_query = "INSERT INTO user_servers(user_id, server_id, server_type_code, position, active_flag, create_date, created_by)
               SELECT $1, $2, 'SERVER', (COALESCE(MAX(position), 0) + 1), 'Y', CURRENT_TIMESTAMP, $3
               FROM user_servers WHERE server_type_code = 'SERVER' and user_id = $4;"
  
  Ecto.Adapters.SQL.query(MyRepo, raw_query, [value1, value2, value3, value4])
  </code></pre>
  
  <p>If I do </p>
  
  <pre><code>Repo.transaction fn -&gt;
    Ecto.Adapters.SQL.query(MyRepo, raw_query, [value1, value2, value3, value4])
    Ecto.Adapters.SQL.query(MyRepo, raw_query, [value1, value2, value3, value4])
  end
  </code></pre>
  
  <p>And one of them fails, will it auto-rollback any queries on a failure?  Also is there a way to name those queries?</p>
  
  <p>For example, maybe I want my first query to be named user_servers so when the transaction comes back I can retrieve it with {:ok, :user_servers: results} like I can with Ecto.Multi?</p>