-
Notifications
You must be signed in to change notification settings - Fork 0
/
dev_tools.clj
62 lines (51 loc) · 2.33 KB
/
dev_tools.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
(ns andrewslai.clj.dev-tools
(:require [andrewslai.clj.persistence.postgres2 :as pg]
[andrewslai.clj.utils :as util]))
(defn postgres-db
([]
(postgres-db (util/pg-conn)))
([conn]
(pg/->Database conn)))
(comment
(require '[clojure.data.csv :as csv])
(require '[clojure.java.io :as io])
(defn read-csv-file [file-name]
(with-open [reader (io/reader file-name)]
(let [parse-first-row (fn [csv]
[(map keyword (first csv)) (rest csv)])
[first-row rows] (parse-first-row (doall (csv/read-csv reader)))]
(map #(apply hash-map (interleave first-row %)) rows))))
;; Dangerous!! Deletes all entries and repopulates
(defn repopulate-db [file-name table]
(let [convert-to-int (fn [field r]
(map #(update %1 field (fn [x] (Integer/parseInt (field %)))) r))
dirty-rows (read-csv-file file-name)
rows (convert-to-int :id dirty-rows)]
(sql/delete! pg-db table [])
(sql/insert-multi! pg-db table rows)))
(repopulate-db "/home/alai/dev/andrewslai/scripts/db/resume_cards/organizations.csv"
:organizations)
(repopulate-db "/home/alai/dev/andrewslai/scripts/db/resume_cards/skills.csv"
:skills)
)
(comment
(defn repopulate-db-projects [file-name table]
(let [convert-to-int (fn [field r]
(map #(update %1 field (fn [x] (Integer/parseInt (field %)))) r))
convert-to-map (fn [field r]
(map #(update %1 field (fn [x] (json/parse-string (field %)))) r))
convert-to-pg (fn [field r]
(map #(update %1 field (fn [x] (map json/generate-string (field %)))) r))
convert-to-array (fn [field r]
(map #(update %1 field (fn [x] (read-string (field %)))) r))
dirty-rows (read-csv-file file-name)
rows (->> dirty-rows
(convert-to-int :id)
(convert-to-array :organization_names)
(convert-to-array :skills_names)
(convert-to-pg :skills_names))]
(sql/delete! pg-db table [])
(sql/insert-multi! pg-db table rows)
rows))
(clojure.pprint/pprint (repopulate-db-projects "/home/alai/dev/andrewslai/scripts/db/resume_cards/projects.csv" :projects))
)