From d5addad542a0d367dcc2219093fddb38fe6e148e Mon Sep 17 00:00:00 2001 From: Marcin Kulik Date: Wed, 15 Feb 2017 12:48:35 +0000 Subject: [PATCH] Implement *Database protocols in our db component ns --- resources/asciinema/system.edn | 2 +- src/asciinema/boundary/asciicast_database.clj | 33 +----------- src/asciinema/boundary/user_database.clj | 4 ++ src/asciinema/component/db.clj | 50 +++++++++++++++++++ .../db_test.clj} | 21 ++++---- 5 files changed, 67 insertions(+), 43 deletions(-) create mode 100644 src/asciinema/boundary/user_database.clj create mode 100644 src/asciinema/component/db.clj rename test/asciinema/{boundary/asciicast_database_test.clj => component/db_test.clj} (70%) diff --git a/resources/asciinema/system.edn b/resources/asciinema/system.edn index 99b80fd..2016ee7 100644 --- a/resources/asciinema/system.edn +++ b/resources/asciinema/system.edn @@ -1,7 +1,7 @@ {:components {:app #var duct.component.handler/handler-component :http #var asciinema.component.aleph/aleph-server - :db #var duct.component.hikaricp/hikaricp + :db #var asciinema.component.db/hikaricp :ragtime #var duct.component.ragtime/ragtime :file-store #var asciinema.component.s3-file-store/s3-file-store :file-server #var asciinema.component.s3-file-server/s3-file-server} diff --git a/src/asciinema/boundary/asciicast_database.clj b/src/asciinema/boundary/asciicast_database.clj index 8fb904d..f06e5b5 100644 --- a/src/asciinema/boundary/asciicast_database.clj +++ b/src/asciinema/boundary/asciicast_database.clj @@ -1,36 +1,5 @@ -(ns asciinema.boundary.asciicast-database - (:require [clojure.java.jdbc :as jdbc] - [clj-time.coerce :as timec] - [duct.component.hikaricp :as hikaricp])) +(ns asciinema.boundary.asciicast-database) (defprotocol AsciicastDatabase (get-asciicast-by-id [this id]) (get-asciicast-by-token [this token])) - -(extend-protocol clojure.java.jdbc/ISQLValue - org.joda.time.DateTime - (sql-value [val] - (timec/to-sql-time val))) - -(extend-protocol clojure.java.jdbc/IResultSetReadColumn - java.sql.Timestamp - (result-set-read-column [x _ _] - (timec/from-sql-time x))) - -(def q-get-asciicast-by-id "SELECT * FROM asciicasts WHERE id=?") -(def q-get-asciicast-by-secret-token "SELECT * FROM asciicasts WHERE secret_token=?") -(def q-get-public-asciicast-by-id "SELECT * FROM asciicasts WHERE id=? AND private=FALSE") - -(extend-protocol AsciicastDatabase - duct.component.hikaricp.HikariCP - - (get-asciicast-by-id [{db :spec} id] - (first (jdbc/query db [q-get-asciicast-by-id id]))) - - (get-asciicast-by-token [{db :spec} token] - (when-let [query (cond - (re-matches #"\d+" token) - [q-get-public-asciicast-by-id (Long/parseLong token)] - (= (count token) 25) - [q-get-asciicast-by-secret-token token])] - (first (jdbc/query db query))))) diff --git a/src/asciinema/boundary/user_database.clj b/src/asciinema/boundary/user_database.clj new file mode 100644 index 0000000..abae4aa --- /dev/null +++ b/src/asciinema/boundary/user_database.clj @@ -0,0 +1,4 @@ +(ns asciinema.boundary.user-database) + +(defprotocol UserDatabase + (get-user-by-id [this id])) diff --git a/src/asciinema/component/db.clj b/src/asciinema/component/db.clj new file mode 100644 index 0000000..85bf0e5 --- /dev/null +++ b/src/asciinema/component/db.clj @@ -0,0 +1,50 @@ +(ns asciinema.component.db + (:require [asciinema.boundary.asciicast-database :refer :all] + [asciinema.boundary.user-database :refer :all] + [clojure.java.jdbc :as jdbc] + [clj-time.coerce :as timec] + [duct.component.hikaricp :as hikaricp])) + +(extend-protocol jdbc/ISQLValue + org.joda.time.DateTime + (sql-value [val] + (timec/to-sql-time val))) + +(extend-protocol jdbc/IResultSetReadColumn + java.sql.Timestamp + (result-set-read-column [x _ _] + (timec/from-sql-time x))) + +;; AsciicastDatabase + +(def q-get-asciicast-by-id "SELECT * FROM asciicasts WHERE id=?") +(def q-get-asciicast-by-secret-token "SELECT * FROM asciicasts WHERE secret_token=?") +(def q-get-public-asciicast-by-id "SELECT * FROM asciicasts WHERE id=? AND private=FALSE") + +(extend-protocol AsciicastDatabase + duct.component.hikaricp.HikariCP + + (get-asciicast-by-id [{db :spec} id] + (first (jdbc/query db [q-get-asciicast-by-id id]))) + + (get-asciicast-by-token [{db :spec} token] + (when-let [query (cond + (re-matches #"\d+" token) + [q-get-public-asciicast-by-id (Long/parseLong token)] + (= (count token) 25) + [q-get-asciicast-by-secret-token token])] + (first (jdbc/query db query))))) + +;; UserDatabase + +(def q-get-user-by-id "SELECT * FROM users WHERE id=?") + +(extend-protocol UserDatabase + duct.component.hikaricp.HikariCP + + (get-user-by-id [{db :spec} id] + (first (jdbc/query db [q-get-user-by-id id])))) + +;; constructor + +(def hikaricp hikaricp/hikaricp) diff --git a/test/asciinema/boundary/asciicast_database_test.clj b/test/asciinema/component/db_test.clj similarity index 70% rename from test/asciinema/boundary/asciicast_database_test.clj rename to test/asciinema/component/db_test.clj index 5860bad..4a5fba7 100644 --- a/test/asciinema/boundary/asciicast_database_test.clj +++ b/test/asciinema/component/db_test.clj @@ -1,12 +1,13 @@ -(ns asciinema.boundary.asciicast-database-test +(ns asciinema.component.db-test (:require [clojure.test :refer :all] [clojure.java.jdbc :as jdbc] [clj-time.local :as timel] [com.stuartsierra.component :as component] - [asciinema.boundary.asciicast-database :as db])) + [asciinema.component.db :as db] + [asciinema.boundary.asciicast-database :as adb])) (defmacro with-db-component [component-var & body] - `(let [component# (-> (duct.component.hikaricp/hikaricp {:uri "jdbc:postgresql://localhost:15432/asciinema_test?user=vagrant"}) + `(let [component# (-> (db/hikaricp {:uri "jdbc:postgresql://localhost:15432/asciinema_test?user=vagrant"}) component/start)] (try (jdbc/with-db-transaction [db# (:spec component#)] @@ -32,22 +33,22 @@ (testing "for existing asciicast" (with-db-component db (let [asciicast (insert-asciicast (:spec db))] - (is (map? (db/get-asciicast-by-id db (:id asciicast))))))) + (is (map? (adb/get-asciicast-by-id db (:id asciicast))))))) (testing "for non-existing asciicast" (with-db-component db - (is (nil? (db/get-asciicast-by-id db 1)))))) + (is (nil? (adb/get-asciicast-by-id db 1)))))) (deftest get-asciicast-by-token-test (testing "for existing public asciicast" (with-db-component db (let [asciicast (insert-asciicast (:spec db) {:private false})] - (is (map? (db/get-asciicast-by-token db (:secret_token asciicast)))) - (is (map? (db/get-asciicast-by-token db (-> asciicast :id str))))))) + (is (map? (adb/get-asciicast-by-token db (:secret_token asciicast)))) + (is (map? (adb/get-asciicast-by-token db (-> asciicast :id str))))))) (testing "for existing private asciicast" (with-db-component db (let [asciicast (insert-asciicast (:spec db) {:private true})] - (is (map? (db/get-asciicast-by-token db (:secret_token asciicast)))) - (is (nil? (db/get-asciicast-by-token db (-> asciicast :id str))))))) + (is (map? (adb/get-asciicast-by-token db (:secret_token asciicast)))) + (is (nil? (adb/get-asciicast-by-token db (-> asciicast :id str))))))) (testing "for non-existing asciicast" (with-db-component db - (is (nil? (db/get-asciicast-by-token db "1")))))) + (is (nil? (adb/get-asciicast-by-token db "1"))))))