Skip to content

Commit

Permalink
Merge pull request guilespi#14 from nberger/update-sqlingvo
Browse files Browse the repository at this point in the history
Upgrade sqlingvo to 0.7.10
  • Loading branch information
guilespi committed Apr 11, 2015
2 parents 69b8e75 + e931536 commit 1a397ce
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 35 deletions.
2 changes: 1 addition & 1 deletion project.clj
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@
[org.clojure/java.jdbc "0.3.5"]
[postgresql/postgresql "8.4-702.jdbc4"]
[clj-time "0.8.0"]
[org.clojars.guilespi/sqlingvo "0.6.7"]])
[sqlingvo "0.7.10"]])
7 changes: 5 additions & 2 deletions src/time_series_storage/postgres/query.clj
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
(ns time-series-storage.postgres.query
(:refer-clojure :exclude [distinct group-by])
(:require [clojure.java.jdbc :as j])
(:require [clojure.java.jdbc :as j]
[sqlingvo.db :as sqdb])
(:use sqlingvo.core
time-series-storage.postgres.common))

(def sqdb (sqdb/postgresql))

(defn- range-where
"Retrieves a time-ranged condition for a specific fact in
a specific dimension path"
Expand Down Expand Up @@ -39,6 +42,6 @@
finish)]
(j/query db
(sql
(select [*]
(select sqdb [*]
(from table-name)
(where condition))))))
28 changes: 15 additions & 13 deletions src/time_series_storage/postgres/schema.clj
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
(ns time-series-storage.postgres.schema
(:refer-clojure :exclude [distinct group-by])
(:require [clojure.java.jdbc :as j]
[sqlingvo.db :as sqdb]
[clojure.string :as string])
(:use sqlingvo.core
time-series-storage.postgres.common))

(def sqdb (sqdb/postgresql))

(defn get-fact
"Retrieves a fact definition from database, nil if fact does not exists"
[db fact]
(first
(j/query db
(sql
(select [*]
(select sqdb [*]
(from :facts)
(where `(= :id ~(name fact))))))))

Expand All @@ -21,7 +23,7 @@
[db]
(j/query db
(sql
(select [*]
(select sqdb [*]
(from :facts)))))

(defn all-dimensions
Expand All @@ -30,7 +32,7 @@
(map #(update-in % [:grouped_by] read-string)
(j/query db
(sql
(select [*]
(select sqdb [*]
(from :dimensions))))))


Expand All @@ -39,7 +41,7 @@
a map of definitions keyed by dimension"
[db s]
(let [dims (seq (j/query db
(sql (select [*]
(sql (select sqdb [*]
(from :dimensions)
(where `(in :id ~(map #(name %) s)))))))
defs (reduce #(assoc %1 (keyword (:id %2)) (update-in %2 [:grouped_by] read-string))
Expand All @@ -54,7 +56,7 @@
[db id]
(when-let [dim (first
(j/query db
(sql (select [*]
(sql (select sqdb [*]
(from :dimensions)
(where `(= :id ~(name id)))))))]
(update-in dim [:grouped_by] read-string)))
Expand All @@ -63,7 +65,7 @@
[db]
(j/execute! db
(sql
(create-table :facts
(create-table sqdb :facts
(if-not-exists true)
(column :id :varchar :length 40 :primary-key? true)
(column :name :varchar :length 40)
Expand All @@ -79,14 +81,14 @@
[db]
(j/execute! db
(sql
(drop-table [:facts]
(drop-table sqdb [:facts]
(if-exists true)))))

(defn create-dimensions-table!
[db]
(j/execute! db
(sql
(create-table :dimensions
(create-table sqdb :dimensions
(if-not-exists true)
(column :id :varchar :length 40 :primary-key? true)
(column :name :varchar :length 40)
Expand All @@ -98,15 +100,15 @@
[db]
(j/execute! db
(sql
(drop-table [:dimensions]
(drop-table sqdb [:dimensions]
(if-exists true)))))

(defn create-fact!
"Inserts the new fact to the database"
[db id type slice {:keys [name filler units
start end step]}]
(j/execute! db
(sql (insert :facts []
(sql (insert sqdb :facts []
(values {:id (clojure.core/name id)
:name name
:type (clojure.core/name type)
Expand All @@ -121,7 +123,7 @@
"Returns the query needed to create a dimension table
for the specified parameters."
[id {:keys [slice name group_only grouped_by]}]
(insert :dimensions []
(insert sqdb :dimensions []
(values {:id (clojure.core/name id)
:name name
:slice slice
Expand Down Expand Up @@ -155,7 +157,7 @@
* Columns according to total dimensions
* Columns according to fact type"
[fact dims]
(create-table (make-table-name fact dims)
(create-table sqdb (make-table-name fact dims)
(if-not-exists true)
;;primary key is composite on all dimensions + timestamp
(apply primary-key (concat [:timestamp] dims))
Expand All @@ -175,7 +177,7 @@
(->> grouped-by
(map #(conj % dim-id))
(map #(make-table-name fact %))
(map #(drop-table [%]
(map #(drop-table sqdb [%]
(if-exists true)))))

(defn- drop-fact-time-series-stmts
Expand Down
36 changes: 20 additions & 16 deletions src/time_series_storage/postgres/update.clj
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
(ns time-series-storage.postgres.update
(:refer-clojure :exclude [distinct group-by])
(:require [clojure.java.jdbc :as j]
[sqlingvo.db :as sqdb]
[time-series-storage.postgres.schema :as schema])
(:use sqlingvo.core
time-series-storage.postgres.common))

(def sqdb (sqdb/postgresql))

(defn event-key
"Returns the particular key for updating a fact in a specific dimension.
Expand Down Expand Up @@ -47,13 +49,14 @@
(make-table-name fact))
value (get event (:id fact))]
(when-let [key (event-key fact dimension group event date-time)]
(with [:upsert (update table-name `((:= ~'counter
~(symbol (str "counter+" value))))
(where (expand-condition key))
(returning *))]
(insert table-name (conj (keys key) :counter)
(select (conj (vals key) value))
(where `(not-exists ~(select [*] (from :upsert)))))))))))
(with sqdb [:upsert (update sqdb table-name
`((:= ~'counter
~(symbol (str "counter+" value))))
(where (expand-condition key))
(returning *))]
(insert sqdb table-name (conj (keys key) :counter)
(select sqdb (conj (vals key) value))
(where `(not-exists ~(select sqdb [*] (from :upsert)))))))))))

(defmethod make-dimension-fact :average
;;Makes a statement for upserting averages on a specific fact and
Expand All @@ -66,15 +69,16 @@
(make-table-name fact))
value (get event (:id fact))]
(when-let [key (event-key fact dimension group event date-time)]
(with [:upsert (update table-name (conj '()
'(= counter counter+1)
(concat '(= total)
[(symbol (str "total+" value))]))
(where (expand-condition key))
(returning *))]
(insert table-name (concat (keys key) [:counter :total])
(select (conj (vec (vals key)) 1 value))
(where `(not-exists ~(select [*] (from :upsert)))))))))))
(with sqdb [:upsert (update sqdb table-name
(conj '()
'(= counter counter+1)
(concat '(= total)
[(symbol (str "total+" value))]))
(where (expand-condition key))
(returning *))]
(insert sqdb table-name (concat (keys key) [:counter :total])
(select sqdb (conj (vec (vals key)) 1 value))
(where `(not-exists ~(select sqdb [*] (from :upsert)))))))))))

(defn new-fact
"When a new fact occurs update all the corresponding dimensions specified in the fact
Expand Down
9 changes: 6 additions & 3 deletions test/time_series_storage/postgres_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@
[time-series-storage.postgres.schema :as schema]
[clojure.java.jdbc :as j]
[sqlingvo.core :as sql]
[sqlingvo.db :as sqdb]
[clj-time.coerce :as tcoerce])
(:import [time_series_storage.postgres Postgres]))

(def sqdb (sqdb/postgresql))

(def db-spec (or (System/getenv "DATABASE_URL")
"postgresql://postgres:postgres@localhost:5432/timeseries_test"))

Expand Down Expand Up @@ -146,7 +149,7 @@
(defn find-table-names
[db]
(let [query (sql/sql
(sql/select [:table_name]
(sql/select sqdb [:table_name]
(sql/from :information_schema.tables)
(sql/where '(= :table_schema "public"))))]
(->> query
Expand All @@ -167,7 +170,7 @@

(deftest drop-schema-keeps-other-tables
(j/execute! db-spec
(sql/sql (sql/create-table :random_table_name
(sql/sql (sql/create-table sqdb :random_table_name
(sql/if-not-exists true))))
(t/define-fact! service :signups :counter 10 {})
(t/define-fact! service :conversions :counter 10 {})
Expand All @@ -181,4 +184,4 @@
(is (= ["random_table_name"] (find-table-names db-spec)))

(j/execute! db-spec
(sql/sql (sql/drop-table [:random_table_name]))))
(sql/sql (sql/drop-table sqdb [:random_table_name]))))

0 comments on commit 1a397ce

Please sign in to comment.