Skip to content
Browse files

Make the database bootstrap itself on startup if things are missing, …

…rather than crashing in mysterious ways. Should make it easier for new contributors to get things running, and help *me* keep up with schema changes others are making
  • Loading branch information...
1 parent 25d5a3f commit fd925334fa0fde8f348022c2fd605c01e6a52277 @amalloy amalloy committed May 18, 2011
Showing with 71 additions and 38 deletions.
  1. +2 −15 src/foreclojure/core.clj
  2. +0 −23 src/foreclojure/db_utils.clj
  3. +69 −0 src/foreclojure/mongo.clj
View
17 src/foreclojure/core.clj
@@ -1,7 +1,7 @@
(ns foreclojure.core
(:use compojure.core
[foreclojure static problems login register golf
- users config social version graphs db-utils utils]
+ users config social version graphs mongo utils]
ring.adapter.jetty
somnium.congomongo
(ring.middleware (reload :only [wrap-reload])
@@ -10,20 +10,7 @@
[sandbar.stateful-session :as session]
[ring.util.response :as response]))
-(mongo!
- :host (if-let [host (:db-host config)]
- host
- "localhost")
- :db "mydb")
-
-(if-let [db-user (:db-user config)]
- (if-let [db-pwd (:db-pwd config)]
- (authenticate db-user db-pwd)))
-
-(add-index! :users [:user] :unique true)
-(add-index! :users [[:solved -1]])
-
-(reconcile-solved-count)
+(prepare-mongo)
(defroutes main-routes
(GET "/" [] (welcome-page))
View
23 src/foreclojure/db_utils.clj
@@ -1,23 +0,0 @@
-(ns foreclojure.db-utils
- (:use [foreclojure.problems]
- [foreclojure.users]
- [somnium.congomongo]))
-
-(defn reconcile-solved-count
- "Overwrites the times-solved field in the problems collection based on data from the users collection. Should only be called on server startup since it isn't a safe operation. Also updates the total-solved agent."
- []
- (send
- total-solved +
- (let [problems (get-problem-list)]
- (reduce
- #(do
- (update! :problems
- {:_id (first %2)}
- {:$set {:times-solved (last %2)}})
- (+ %1 (last %2)))
- 0
- (reduce #(update-in %1 [%2] inc)
- (reduce #(conj %1 [%2 0])
- {}
- (map :_id problems))
- (mapcat :solved (get-users)))))))
View
69 src/foreclojure/mongo.clj
@@ -0,0 +1,69 @@
+(ns foreclojure.mongo
+ (:use somnium.congomongo
+ [foreclojure.data-set :only [load-problems]]
+ [foreclojure.config :only [config]]
+ [foreclojure.problems :only [total-solved get-problem-list]]
+ [foreclojure.users :only [get-users]]))
+
+(defn connect-to-db []
+ (let [{:keys [db-user db-pwd db-host]} config]
+ (mongo!
+ :host (or db-host "localhost")
+ :db "mydb")
+ (when (and db-user db-pwd)
+ (authenticate db-user db-pwd))))
+
+(defn prepare-problems []
+ (when-not (fetch-one :problems)
+ (load-problems))
+ (add-index! :problems [:solved]))
+
+(defn prepare-seqs []
+ (update! :seqs
+ {:_id "problems"}
+ {:$set {:seq (apply max
+ (map :_id
+ (fetch :problems
+ :only [:_id])))}}))
+
+;; make it easier to get off the ground by marking contributors automatically
+;; useful since some "in-development" features aren't enabled for all users
+(defn prepare-users []
+ (add-index! :users [:user] :unique true)
+ (add-index! :users [[:solved -1]])
+ (update! :users
+ {:user {:$in ["amalloy" "dbyrne" "raynes" "cmeier" "devn"
+ "citizen428" "daviddavis" "clinteger"]}}
+ {:$set {:contributor true}}
+ :upsert false
+ :multiple true))
+
+(defn prepare-solutions []
+ (add-index! :solutions [:user :problem]))
+
+(defn reconcile-solved-count
+ "Overwrites the times-solved field in the problems collection based on data from the users collection. Should only be called on server startup since it isn't a safe operation. Also updates the total-solved agent."
+ []
+ (send
+ total-solved +
+ (let [problems (get-problem-list)]
+ (reduce
+ #(do
+ (update! :problems
+ {:_id (first %2)}
+ {:$set {:times-solved (last %2)}})
+ (+ %1 (last %2)))
+ 0
+ (reduce #(update-in %1 [%2] inc)
+ (reduce #(conj %1 [%2 0])
+ {}
+ (map :_id problems))
+ (mapcat :solved (get-users)))))))
+
+(defn prepare-mongo []
+ (connect-to-db)
+ (prepare-problems)
+ (prepare-seqs)
+ (prepare-users)
+ (prepare-solutions)
+ (reconcile-solved-count))

0 comments on commit fd92533

Please sign in to comment.
Something went wrong with that request. Please try again.