Skip to content

Commit 89ca628

Browse files
committedJan 11, 2014
initial steps to use postgres for all stages
1 parent c008fab commit 89ca628

File tree

9 files changed

+66
-48
lines changed

9 files changed

+66
-48
lines changed
 

‎Procfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
web: lein with-profile production trampoline ring server
1+
web: lein with-profile production trampoline ring server-headless

‎devserver

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#!/bin/bash
2+
lein with-profile dev trampoline ring server-headless

‎project.clj

+33-10
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,6 @@
2222
;; Database
2323
[korma "0.3.0-RC6"]
2424

25-
;; JSON
26-
[cheshire "5.2.0"]
27-
2825
;; REST architecture
2926
[liberator "0.10.0"]
3027

@@ -51,21 +48,47 @@
5148
:init patronage.handler/init
5249
:destroy patronage.handler/destroy}
5350

54-
:profiles {:production
55-
{:dependencies [[org.postgresql/postgresql "9.3-1100-jdbc41"]]
51+
:profiles {:dev
52+
{:dependencies [[org.clojure/tools.trace "0.7.6"]
53+
[ring-mock "0.1.5"]
54+
[ring/ring-devel "1.2.1"]
55+
[com.h2database/h2 "1.3.174"]]
5656
:ring {:open-browser? false
57-
:stacktraces? false
58-
:auto-reload? false}}
59-
60-
:dev
57+
:nrepl {:start? true}}
58+
:env {:db-spec "h2://localhost/site.db"}}
59+
;; {:classname "org.h2.Driver"
60+
;; :subprotocol "h2"
61+
;; :subname "site.db"
62+
;; :user "sa"
63+
;; :password ""
64+
;; :naming {:keys clojure.string/lower-case
65+
;; :fields clojure.string/upper-case}}}}
66+
67+
:dev-ssl
6168
{:dependencies [[org.clojure/tools.trace "0.7.6"]
6269
[ring-mock "0.1.5"]
6370
[ring/ring-devel "1.2.1"]
6471
[com.h2database/h2 "1.3.174"]]
6572
:ring {:adapter {:ssl-port 3443
6673
:keystore "codesykeystore"
6774
:key-password "codesy"}
68-
:nrepl {:start? true}}}}
75+
:nrepl {:start? true}}}
76+
77+
:dev-pg
78+
{:dependencies [[org.clojure/tools.trace "0.7.6"]
79+
[ring-mock "0.1.5"]
80+
[ring/ring-devel "1.2.1"]
81+
[org.postgresql/postgresql "9.3-1100-jdbc41"]]
82+
:ring {:open-browser? false
83+
:nrepl {:start? true}}}
84+
85+
:production
86+
{:dependencies [[org.postgresql/postgresql "9.3-1100-jdbc41"]]
87+
:ring {:open-browser? false
88+
:stacktraces? false
89+
:auto-reload? false}}}
90+
91+
6992

7093
:plugins [[lein-ring "0.8.8"]
7194
[lein-environ "0.4.0"]]

‎runserver

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#!/bin/bash
2+
lein with-profile production trampoline ring server-headless

‎src/patronage/auth.clj

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
(def client-config
1313
{:client-id (env :github-oauth-client-id)
1414
:client-secret (env :github-oauth-client-secret)
15-
:callback {:domain "https://localhost:3443" :path "/auth/github"}})
15+
:callback {:domain "https://localhost:3443" :path "/auth/github"}}) ;; TODO factor this out into an envvar
1616

1717
(def uri-config
1818
{:authentication-uri

‎src/patronage/handler.clj

+2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
[compojure.handler :as handler]
66
[compojure.route :as route]
77
[patronage.auth :refer :all]
8+
[patronage.models.schema :refer [create-tables]]
89
[patronage.routes.api :refer [api-routes]]
910
[ring.util.response :as response]
1011
[taoensso.timbre :as timbre]))
@@ -28,6 +29,7 @@
2829
{:path "patronage.log"
2930
:max-size (* 512 1024)
3031
:backlog 10})
32+
(create-tables)
3133
(timbre/info "patronage started successfully"))
3234

3335
(defn destroy

‎src/patronage/models/schema.clj

+20-31
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,35 @@
11
(ns patronage.models.schema
2-
(:require [clojure.java.jdbc :as sql]
3-
[patronage.util :as util]))
2+
(:require [clojure.java.jdbc :as sql]
3+
[environ.core :refer [env]]
4+
[korma.db :refer :all ]
5+
[patronage.util :as util]))
46

5-
(def db-store "site.db")
6-
7-
(def db-spec {:classname "org.h2.Driver"
8-
:subprotocol "h2"
9-
:subname (str (util/resource-path) db-store)
10-
:user "sa"
11-
:password ""
12-
:naming {:keys clojure.string/lower-case
13-
:fields clojure.string/upper-case}})
14-
15-
(defn initialized?
16-
"checks to see if the database schema is present"
17-
[]
18-
(.exists (new java.io.File (str (util/resource-path) db-store ".h2.db"))))
7+
(def db-spec (env :database-url))
198

209
(defn create-users-table
2110
[]
2211
(sql/with-connection db-spec
2312
(sql/create-table
24-
:users
25-
[:id "integer PRIMARY KEY AUTO_INCREMENT"]
26-
[:first_name "varchar(30)"]
27-
[:last_name "varchar(30)"]
28-
[:email "varchar(30)"]
29-
[:admin :boolean]
30-
[:current_sign_in_at :time]
31-
[:last_sign_in_at :time]
32-
[:password "varchar(100)"])))
13+
:users
14+
[:id "serial PRIMARY KEY"]
15+
[:first_name "varchar(30)"]
16+
[:last_name "varchar(30)"]
17+
[:email "varchar(30)"]
18+
[:admin :boolean]
19+
[:current_sign_in_at :time]
20+
[:last_sign_in_at :time]
21+
[:password "varchar(100)"])))
3322

3423
(defn create-bids-table
3524
[]
3625
(sql/with-connection db-spec
3726
(sql/create-table
38-
:bids
39-
[:id "integer PRIMARY KEY AUTO_INCREMENT"]
40-
[:url "varchar(2000)"]
41-
[:offer "bigint"]
42-
[:ask "bigint"]
43-
[:user_id "integer NOT NULL"])))
27+
:bids
28+
[:id "serial PRIMARY KEY"]
29+
[:url "varchar(2000)"]
30+
[:offer "bigint"]
31+
[:ask "bigint"]
32+
[:user_id "integer NOT NULL"])))
4433

4534
(defn create-tables
4635
"creates the database tables used by the application"

‎src/patronage/routes/api.clj

+2-2
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,6 @@
5757

5858
(defroutes api-routes
5959
(ANY ["/bids/:id" :id #".*"] [id]
60-
(friend/authenticated (bid id)))
60+
(bid id))
6161
(ANY "/bids" []
62-
(friend/authenticated bids)))
62+
bids))

‎test/patronage/routes/api_test.clj

+3-3
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@
1717
:offer 5.0
1818
:ask 50.0}]]
1919
(with-redefs [db/get-all-bids (fn [] bids)
20-
db/create-bid (fn [bid] {::id 1})
20+
db/create-bid! (fn [bid] {::id 1})
2121
db/get-bid (fn [id] (assoc bid :id 1))
22-
db/update-bid (fn [id new-bid]
22+
db/update-bid! (fn [id new-bid]
2323
(merge bid new-bid))
24-
db/delete-bid (fn [id] nil)]
24+
db/delete-bid! (fn [id] nil)]
2525
(testing "listing all bids"
2626
(let [response (-> (request :get "/bids")
2727
api-routes)]

0 commit comments

Comments
 (0)
Please sign in to comment.