 ;##############################################################################
 
 (defn next-trial-for-pull [tx executor]
-  (->> (-> trials-excutors-query-ordered
+  (->> (-> (trials-excutors-query-ordered)
            (sql-merge-where [:= :exs.id (:id executor)])
            (sql-merge-where [:= cider-ci.self/VERSION (:version executor)])
            (sql-limit 1) sql-format)

                                      " * dispatch_storm_tasks.dispatch_storm_delay_seconds ))"
                                      " > now() "))))])

(def ^:private trials-excutors-base-query
  (-> (sql-from :trials)
      (sql-merge-where (sql-raw "trials.state = 'pending'"))
      (sql-merge-join :tasks [:= :tasks.id :trials.task_id])
      (sql-merge-join [:executors_with_load :exs]
                      (sql-raw "(tasks.traits <@ exs.traits)"))
      (sql-select :trials.id :trials.task_id [:exs.id :executor_id])
      ; TODO write this out more explicitly
      (sql-merge-where [:< :exs.relative_load 1])
      (sql-merge-where (sql-raw (str "tasks.load < "
                                     "exs.max_load * exs.temporary_overload_factor "

      (sql-merge-where (sql-raw (str "(exs.last_ping_at >  "
                                     "(now() - interval '1 Minutes'))")))
      (sql-merge-where without-conflicting-dispatch-storm-delay)
      (sql-merge-where without-or-with-available-global-resource-condition)
      (sql-merge-where (with-defective-trial-delay))
      ))

(defn ^:private trials-excutors-base-with-repo []
  (-> (trials-excutors-base-query)
      (sql-merge-join :jobs [:= :tasks.job_id :jobs.id])
      (sql-merge-join :commits [:= :jobs.tree_id :commits.tree_id])
      (sql-merge-join [:branches_commits :bcts]
