   []
   (try
     (let [query "SELECT version FROM schema_migrations ORDER BY version DESC LIMIT 1"
-          results (query-to-vec query)]
+          results (sql/transaction
+                    (query-to-vec query))]
       (:version (first results)))
     (catch java.sql.SQLException e
       0)))

along with the time at which the migration was performed."
  [version]
  {:pre [(integer? version)]}
  (sql/insert-record :schema_migrations {:version version :time (Date.)}))

(defn pending-migrations
  "Returns a collection of pending migrations, ordered from oldest to latest."

  "Migrates database to the latest schema version. Does nothing if database is
already at the latest schema version."
  []
  (if-let [pending (seq (pending-migrations))]
    (sql/transaction
      (doseq [[version migration] pending]
      (log/info (format "Migrating to version %d" version))
      (migration)
      (record-migration! version)))
    (log/info "There are no pending migrations")))
