Skip to content

Commit 21c3831

Browse files
committed
Deref when transacting schema, skip non-message reactions
When transacting the schema upon startup, deref the tx-data so that exceptions will bubble. This should also means deploys roll back when the schema fails to transact. Skip reaction messages unless we get a channel and message id, because we can have reactions on non-message items (i.e. files).
1 parent 85202ac commit 21c3831

File tree

3 files changed

+49
-10
lines changed

3 files changed

+49
-10
lines changed

repl/emoji_cleanup.clj

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
(ns emoji-cleanup
2+
(:require [clojurians-log.datomic :as d]
3+
[clojurians-log.repl :as repl]
4+
[clojurians-log.slack-api :as slack]))
5+
6+
;; Our :emoji/shortcode property did not originally have an index, meaning we
7+
;; kept creating more entities for emojis, instead of upserting them.
8+
;;
9+
;; To fix this, first retract all shortcode attributes, then transact the schema
10+
;; so it gets the index, then re-import
11+
12+
;; Do the following before deploying the new schema changes:
13+
14+
(def shortcodes (d/q '[:find ?i ?s :where [?i :emoji/shortcode ?s]] (repl/db)))
15+
(def tx-data (for [[i s] shortcodes]
16+
[:db/retract i :emoji/shortcode s]))
17+
18+
(run! (partial d/transact (repl/conn)) (partition-all 1000 tx-data))
19+
20+
@(d/transact (repl/conn) [{:db/ident :emoji/shortcode
21+
:db/valueType :db.type/string
22+
:db/cardinality :db.cardinality/one
23+
:db/unique :db.unique/identity}])
24+
25+
(slack/import-emojis! (repl/conn))
26+
27+
;; After deploying the new version, re-import the data to get the emoji reactions in there
28+
29+
(def result (load-files! (log-files))) ;; [(volatile count) (promise)]
30+
31+
(future
32+
(while (not (realized? (second result)))
33+
(println @(first result))
34+
(Thread/sleep 2000))
35+
(println :done))

src/clojurians_log/components/datomic_schema.clj

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66
(defrecord DatomicSchema [datomic]
77
component/Lifecycle
88
(start [this]
9-
(d/transact (:conn datomic)
10-
(if d/cloud?
11-
(map #(dissoc % :db/index) schema/full-schema)
12-
schema/full-schema)))
9+
@(d/transact (:conn datomic)
10+
(if d/cloud?
11+
(map #(dissoc % :db/index) schema/full-schema)
12+
schema/full-schema)))
1313
(stop [this]))
1414

1515
(defn new-datomic-schema []

src/clojurians_log/db/import.clj

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -94,12 +94,16 @@
9494
#:emoji {:shortcode (name shortcode)
9595
:url url})
9696

97-
(defmethod event->tx ["reaction_added" nil] [{:keys [user item reaction item_user event_ts ts]}]
98-
{:reaction/type "reaction_added"
99-
:reaction/emoji {:emoji/shortcode reaction}
100-
:reaction/ts ts
101-
:reaction/user [:user/slack-id user]
102-
:reaction/message {:message/key (message-key item)}})
97+
;; TODO: deal with reactions on files (I guess this will depend on us actually dealing with files in the first place :))
98+
;; {:reaction "joy", :event_ts "1521818444.000850", :item {:type "file", :file "F9W0B0LHM"}, :user "U06P56UUB", :item_user "U4E5W80P7", :type "reaction_added"}
99+
100+
(defmethod event->tx ["reaction_added" nil] [{:keys [user item reaction item_user event_ts ts] :as msg}]
101+
(when (and (:channel item) (:ts item)) ; exclude reactions on things other than messages
102+
{:reaction/type "reaction_added"
103+
:reaction/emoji {:emoji/shortcode reaction}
104+
:reaction/ts ts
105+
:reaction/user [:user/slack-id user]
106+
:reaction/message {:message/key (message-key item)}}))
103107

104108
(defmethod event->tx ["reaction_removed" nil] [{:keys [user item reaction item_user event_ts ts]}]
105109
;; Placeholder just to show that we're getting some data.

0 commit comments

Comments
 (0)