Created
December 22, 2016 23:51
-
-
Save mjamesruggiero/0db8e56a6efed9bf4dd32cf6140c3770 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; note that project loads this in :dependencies | |
;; [org.xerial/sqlite-jdbc "3.7.2"]] | |
(ns piney.pagerduty | |
(:require [piney.utils :refer :all] | |
[clojure.java.jdbc :refer :all] | |
[clojure.java.io :as io] | |
[clojure.edn :as edn])) | |
(defn load-config | |
"loads EDN config from a file" | |
[filename] | |
(edn/read-string (slurp filename))) | |
(defn db-config | |
"Load db config; defaults to Dev config but will optionally | |
take key/val pair :config-file <file-name> | |
(e.g. 'test-config.edn')" | |
[& {:keys [config-file] | |
:or {config-file "dev-config.edn"}}] | |
(let [file-path (or (System/getenv "PINEY_CONFIG") config-file) | |
conf (load-config (io/resource file-path))] | |
{:classname "org.sqlite.JDBC" | |
:subprotocol "sqlite" | |
:subname (:subname conf) | |
:user (:user conf) | |
:password (:password conf)})) | |
(def db | |
(db-config)) | |
(defn incident-exists? | |
[db record-id] | |
(let [q "SELECT COUNT(*) AS count FROM incidents WHERE id=?" | |
count (:count (first (query db [q record-id])))] | |
(= count 1))) | |
(defn create-db | |
[] | |
(try | |
(db-do-commands db | |
(create-table-ddl :incidents | |
[:id :text] | |
[:description :text] | |
[:service_name :text] | |
[:resolved_by_user_name :text] | |
[:created_on :date] | |
[:seconds_to_resolve :integer])) | |
(catch Exception e (println e)))) | |
;; (def testdata | |
;; {:id "fake-id" | |
;; :description "Fake description" | |
;; :service_name "Service A" | |
;; :resolved_by_user_name "Bob" | |
;; :created_on "2016-12-11T00:00:00" | |
;; :seconds_to_resolve 100}) | |
;; (create-db) | |
;; (insert! db :incidents testdata) | |
;; (query db "SELECT * FROM incidents") | |
;; (incident-exists? db "fake-id") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
db-config
is fairly messy