-
Notifications
You must be signed in to change notification settings - Fork 2
/
collection_header.clj
67 lines (60 loc) · 2.81 KB
/
collection_header.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
63
64
65
66
67
(ns com.timezynk.domain.migrations.collection-header
"The collection header adds information about migrations"
(:require [com.timezynk.domain.core :as dom]
; [com.timezynk.domain.schema :as s]
[somnium.congomongo :as mongo]))
(comment
(def collection-headers
(-> (dom/dom-type-collection
:name :collection-headers
:description "The collection header adds information about migrations"
:properties {:migrations (s/any :default (fn [_] {}))
:info (s/any :default (fn [_] {}))})
(update-in [:properties]
dissoc
:created
:created-by
:changed-by
:company-id)
(assoc-in [:properties :id] (s/string)))))
(defn by-collection [collection]
(mongo/fetch-one :collection.headers
:where {:_id (if (keyword? collection)
collection
(dom/collection-name collection))}))
(defn migration-made? [collection migr-name]
(let [collection-name (if (keyword? collection)
collection
(dom/collection-name collection))]
(boolean
(mongo/fetch-one :collection.headers
:where {:_id collection-name
(str "migrations." (name migr-name)) {:$exists true}}))))
(defn create! [collection migration-name source-collection]
(let [collection-name (if (keyword? collection)
collection
(dom/collection-name collection))
header? (mongo/fetch-one :collection.headers
:where {:_id collection-name})]
(when-not header?
(mongo/insert! :collection.headers
{:_id collection-name
migration-name {:source-collection source-collection}}))))
(defn update! [dom-type-collection new-doc]
(let [collection-name (dom/collection-name dom-type-collection)]
(mongo/update! :collection.headers
{:_id collection-name}
new-doc)))
(defn set-migration-not-made! [collection migr-name]
(mongo/update! :collection.headers
{:_id (if (keyword? collection)
collection
(dom/collection-name collection))}
{:$set {(str "migrations." (name migr-name)) false}}))
(defn migration-finished [collection migr-name]
(let [collection-name (if (keyword? collection)
collection
(dom/collection-name collection))]
(mongo/update! :collection.headers
{:_id collection-name}
{:$set {(str "migrations." (name migr-name)) (System/currentTimeMillis)}})))