       [logging :as log]))
 
   (:use
-    [clojure.set :only (select)]
+    (clojure
+      test
+      [set :only (select)])
     
     (clojure.contrib
       [except :only (throw-if throwf)])))
 
+;;;
+;;; Pure fns
+;;;
+
+(defn version-rec-to-double
+  "Creates a double from database_version table row."
+  [{:keys [major minor]}]
+  (Double/valueOf (format "%s.%s" major minor)))
+
+
+(defn double-to-version-rec
+  "Creates a record intended to insert/update row in database_version table."
+  [version]
+  (let [[major minor] (su/re-split #"\." (str version))]
+    {
+      :major major,
+      :minor (if minor minor "0")
+    }))
+
+
+(defn required-diff
+  "Returns a diff required to update db or nil if no corresponding diff exists.
+   db-conf-diffs - a set of diffs; see conf.clj and tests to learn more about diffs definition.
+   current-version - current db version as double."
+  [db-conf-diffs current-version]
+  (when-first [diff (select #(= current-version (:from %)) db-conf-diffs)]
+    diff))
+
+
+(defn required-diffs
+  "Returns a seq of diffs beginning from a current version or nil.
+   db-conf-diffs - a set of diffs; see conf.clj and tests to learn more about diffs definition.
+   current-version - current db version as double."  
+  [db-conf-diffs current-version]
+  (loop [from current-version, diffs []]
+    (if-let [diff (required-diff db-conf-diffs from)]
+      (let [new-current-version (:to diff)
+            collected-diffs (conj diffs diff)]
+        (recur new-current-version collected-diffs))
+      (seq diffs))))
+
+
+;;;
+;;; Fns with side-effect
+;;;
 
 (defn tables
   "Returns all db tables."

  ([]
    (sql/with-query-results rs ["SHOW TABLES"]
      (mapcat vals (doall rs)))))


(defn double-to-version-rec [version]
  (let [[major minor] (su/re-split #"\." (str version))]
    {:major major,
     :minor (if-not minor 0 minor)
    }))


(defn version-rec-to-double [{:keys [major minor]}]
  (Double/valueOf (format "%s.%s" major (if minor minor 0))))


(defn get-version

    (sql/update-or-insert-values
      "database_version" ["major IS NOT NULL"]
      (double-to-version-rec version))))


(defn prepare
