1212
1313; ;;; Preparation
1414
15- (def ^:private db-lock (ReentrantLock. ))
16-
17- (defn- db-options []
18- {:dbtype " sqlite"
19- :dbname (@config :db :path )})
20-
21- (defn- migratus-config []
15+ (defn- migratus-config [connectable]
2216 {:store :database
2317 :migration-dir " migrations/"
2418 :init-script " init.sql"
2519 :init-in-transaction? false
26- :db ( db-options ) })
20+ :db connectable })
2721
28- (defn migrate []
29- (with-locking db-lock
30- (migratus/init (migratus-config ))
31- (migratus/migrate (migratus-config ))))
32-
33- #_(migrate )
22+ (defn- migrate [db]
23+ (with-locking (:lock db)
24+ (let [config (migratus-config db)]
25+ (migratus/init config)
26+ (migratus/migrate config))))
3427
3528(mount/defstate db
36- :start (migrate ))
29+ :start (doto {:dbtype " sqlite"
30+ :dbname (@config :db :path )
31+ :lock (ReentrantLock. )}
32+ migrate))
33+
34+ #_(mount/start #'db)
3735
3836; ;;; DB interaction
3937
4240 (if (<= tries 0 )
4341 (raise 500 " Can't create a proper unused ID." )
4442 (let [id (new-id )
45- res (with-locking db- lock
46- (jdbc/execute-one! ( db-options ) [" SELECT count(id) AS cnt FROM profile WHERE id = ?" id]))]
43+ res (with-locking ( : lock @db)
44+ (jdbc/execute-one! @db [" SELECT count(id) AS cnt FROM profile WHERE id = ?" id]))]
4745 (if (zero? (:cnt res))
4846 id
4947 (recur (dec tries)))))))
5553 (let [{:keys [id file_path profile_type sample_count owner upload_ts
5654 edit_token is_public]} profile]
5755 (log/infof " Inserting profile %s from %s" id owner)
58- (with-locking db-lock
59- (sql-helpers/insert! (db-options ) :profile
60- {:id id
61- :file_path file_path
62- :profile_type (name profile_type)
63- :upload_ts (str upload_ts)
64- :sample_count sample_count
65- :is_public is_public
66- :edit_token edit_token
67- :owner owner}))
56+ (with-locking (:lock @db)
57+ (jdbc/with-transaction [tx @db]
58+ (sql-helpers/insert! tx :profile
59+ {:id id
60+ :file_path file_path
61+ :profile_type (name profile_type)
62+ :upload_ts (str upload_ts)
63+ :sample_count sample_count
64+ :is_public is_public
65+ :edit_token edit_token
66+ :owner owner})
67+ (jdbc/execute-one! tx [" UPDATE stats SET val = val + 1 WHERE stat = 'total_uploaded'" ])))
6868 profile))
6969
70- #_(jdbc/execute! ( db-options ) [" SELECT * FROM profile" ])
70+ #_(jdbc/execute! @db [" SELECT * FROM profile" ])
7171
7272(defn- unqualify-keys [m] (update-keys m (comp keyword name)))
7373
7474(defn list-profiles []
75- (with-locking db- lock
76- (->> (jdbc/execute! ( db-options ) [" SELECT id, file_path, profile_type, sample_count, owner, upload_ts, is_public FROM profile" ])
75+ (with-locking ( : lock @db)
76+ (->> (jdbc/execute! @db [" SELECT id, file_path, profile_type, sample_count, owner, upload_ts, is_public FROM profile" ])
7777 (mapv #(-> (unqualify-keys %)
7878 (assoc :edit_token nil )
7979 (coerce Profile))))))
8080
8181(defn list-public-profiles [n]
82- (with-locking db- lock
83- (->> (jdbc/execute! ( db-options ) [" SELECT id, file_path, profile_type, sample_count, owner, upload_ts, is_public, edit_token FROM profile
82+ (with-locking ( : lock @db)
83+ (->> (jdbc/execute! @db [" SELECT id, file_path, profile_type, sample_count, owner, upload_ts, is_public, edit_token FROM profile
8484WHERE is_public = 1 ORDER BY upload_ts DESC LIMIT ?" n])
8585 (mapv #(-> (unqualify-keys %)
8686 (coerce Profile))))))
8787
8888(defn get-profile [profile-id]
89- (with-locking db- lock
89+ (with-locking ( : lock @db)
9090 (let [q [" SELECT id, file_path, profile_type, sample_count, owner, upload_ts, edit_token, is_public FROM profile WHERE id = ?" profile-id]
91- row (some-> (jdbc/execute-one! ( db-options ) q)
91+ row (some-> (jdbc/execute-one! @db q)
9292 unqualify-keys
9393 (coerce Profile))]
9494 (or row
@@ -97,16 +97,16 @@ WHERE is_public = 1 ORDER BY upload_ts DESC LIMIT ?" n])
9797 (raise 404 msg))))))
9898
9999(defn delete-profile [profile-id]
100- (with-locking db- lock
100+ (with-locking ( : lock @db)
101101 (let [q [" DELETE FROM profile WHERE id = ?" profile-id]
102- {::jdbc/keys [update-count]} (jdbc/execute-one! ( db-options ) q)]
102+ {::jdbc/keys [update-count]} (jdbc/execute-one! @db q)]
103103 (when (zero? update-count)
104104 (raise 404 (format " Profile with ID '%s' not found." profile-id))))))
105105
106106(defn clear-db []
107- (with-locking db- lock
107+ (with-locking ( : lock @db)
108108 (.delete (clojure.java.io/file (@config :db :path )))
109- (migrate )))
109+ (migrate @db )))
110110
111111(comment
112112 (clear-db )
0 commit comments