Skip to content

Instantly share code, notes, and snippets.

@mjamesruggiero
Created December 22, 2016 23:51
Show Gist options
  • Save mjamesruggiero/0db8e56a6efed9bf4dd32cf6140c3770 to your computer and use it in GitHub Desktop.
Save mjamesruggiero/0db8e56a6efed9bf4dd32cf6140c3770 to your computer and use it in GitHub Desktop.
;; 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")
@mjamesruggiero
Copy link
Author

db-config is fairly messy

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment