seed a game on user migration. add some helpers
This commit is contained in:
parent
7728db5e3c
commit
839ebb1131
1 changed files with 55 additions and 3 deletions
58
dev/user.clj
58
dev/user.clj
|
|
@ -3,19 +3,71 @@
|
||||||
[clojure.java.io :as io]
|
[clojure.java.io :as io]
|
||||||
[clojure.tools.logging :as log]
|
[clojure.tools.logging :as log]
|
||||||
[clojure.tools.namespace.repl :as tn-repl]
|
[clojure.tools.namespace.repl :as tn-repl]
|
||||||
|
[migration :as migration]
|
||||||
[com.score-the-pigs :as main :refer [system]]
|
[com.score-the-pigs :as main :refer [system]]
|
||||||
[com.score-the-pigs.util.db :as db]
|
[com.score-the-pigs.util.db :as db]
|
||||||
[honey.sql :as sql]
|
[honey.sql :as sql]
|
||||||
[honey.sql.helpers :refer [drop-table]]
|
[honey.sql.helpers :refer [drop-table]]
|
||||||
[next.jdbc :as jdbc]))
|
[next.jdbc :as jdbc]
|
||||||
|
[migratus.core :as migratus]))
|
||||||
|
|
||||||
(defn reset-db! []
|
(defn reset-db! []
|
||||||
(let [{:keys [example/ds]} @system
|
(let [{:keys [example/ds]} @system
|
||||||
tables [:user :player]
|
tables [:game :player :schema_migrations]
|
||||||
drop-statements (map (fn [table] (sql/format (drop-table :if-exists table))) tables)]
|
drop-statements (map (fn [table] (sql/format (drop-table :if-exists table))) tables)]
|
||||||
|
(println ds)
|
||||||
(db/execute-all! ds drop-statements)
|
(db/execute-all! ds drop-statements)
|
||||||
|
(migratus/migrate migration/config)
|
||||||
|
(let [game-code "test"]
|
||||||
|
(jdbc/execute! ds
|
||||||
|
(sql/format
|
||||||
|
{:insert-into :game
|
||||||
|
:values [{:code game-code,
|
||||||
|
:current_player 0,
|
||||||
|
:player_count 3,
|
||||||
|
:scoring_option "first-to-100"}]}))
|
||||||
|
(jdbc/execute! ds
|
||||||
|
(sql/format
|
||||||
|
{:insert-into :player
|
||||||
|
:values [{:game_score 0,
|
||||||
|
:round_score 0,
|
||||||
|
:name "luci",
|
||||||
|
:game_code game-code,
|
||||||
|
:play_order 0}
|
||||||
|
{:game_score 0,
|
||||||
|
:round_score 0,
|
||||||
|
:name "lily",
|
||||||
|
:game_code game-code,
|
||||||
|
:play_order 1}
|
||||||
|
{:game_score 0,
|
||||||
|
:round_score 0,
|
||||||
|
:name "delta",
|
||||||
|
:game_code game-code,
|
||||||
|
:play_order 2}]})))))
|
||||||
|
|
||||||
(jdbc/execute! ds [(slurp (io/resource "migrations.sql"))])))
|
(defn game [c]
|
||||||
|
(let [{:keys [example/ds]} @system]
|
||||||
|
(->> {:select :* :from :game :where [:= :game.code c]}
|
||||||
|
sql/format
|
||||||
|
(jdbc/execute-one! ds))))
|
||||||
|
|
||||||
|
(defn games []
|
||||||
|
(let [{:keys [example/ds]} @system]
|
||||||
|
(->> {:select :* :from :game}
|
||||||
|
sql/format
|
||||||
|
(jdbc/execute! ds))))
|
||||||
|
|
||||||
|
(defn players []
|
||||||
|
(let [{:keys [example/ds]} @system]
|
||||||
|
(->> {:select :* :from :player}
|
||||||
|
sql/format
|
||||||
|
(jdbc/execute! ds))))
|
||||||
|
|
||||||
|
(defn game-players [c]
|
||||||
|
(let [{:keys [example/ds]} @system]
|
||||||
|
(->> {:select :* :from :player :where [:= :player.game-code c]}
|
||||||
|
sql/format
|
||||||
|
(jdbc/execute! ds))))
|
||||||
|
|
||||||
(defn refresh []
|
(defn refresh []
|
||||||
(doseq [f (:biff/stop @system)]
|
(doseq [f (:biff/stop @system)]
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue