-
Notifications
You must be signed in to change notification settings - Fork 23
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Save compaction #2966
Save compaction #2966
Changes from all commits
df8ef52
a72628d
2049e8c
94cb86d
3d55c7a
d66c881
5d83db0
329fdaf
711ec1a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
(ns rems.db.events | ||
(:require [rems.application.events :as events] | ||
[rems.config :refer [env]] | ||
[rems.db.core :as db] | ||
[rems.json :as json] | ||
[rems.schema-base :as schema-base] | ||
|
@@ -41,7 +42,7 @@ | |
(fix-event-from-db (db/get-latest-application-event {}))) | ||
|
||
(defn add-event! | ||
"Add event to database. Returns the event as it went into the db." | ||
"Add `event` to database. Returns the event as it went into the db." | ||
[event] | ||
(fix-event-from-db (db/add-application-event! {:application (:application/id event) | ||
:eventdata (event->json event)}))) | ||
|
@@ -51,9 +52,44 @@ | |
[event] | ||
(let [old-event (fix-event-from-db (first (db/get-application-event {:id (:event/id event)}))) | ||
_ (assert old-event) | ||
event (-> old-event | ||
(merge event) | ||
(dissoc :event/id))] | ||
(fix-event-from-db (db/update-application-event! {:id (:event/id old-event) | ||
:application (:application/id event) | ||
:eventdata (event->json event)})))) | ||
event (merge old-event event)] | ||
(db/update-application-event! {:id (:event/id old-event) | ||
:application (:application/id event) | ||
:eventdata (event->json (dissoc event :event/id))}) | ||
event)) | ||
|
||
(defn replace-event! | ||
"Replaces an event on top of an old one. | ||
|
||
Differs from `add-event!` in that it replaces an old event. | ||
Differs from `update-event!` in that the event id is a new one. | ||
|
||
Returns the event as it went into the db." | ||
[event] | ||
(let [old-event (fix-event-from-db (first (db/get-application-event {:id (:event/id event)}))) | ||
_ (assert old-event) | ||
event (merge old-event event)] | ||
(fix-event-from-db (db/replace-application-event! {:id (:event/id old-event) | ||
:application (:application/id event) | ||
:eventdata (event->json (dissoc event :event/id))})))) | ||
|
||
(defn add-event-with-compaction! | ||
"Add `event` to database. | ||
|
||
Consecutive `:draft-saved` events of `application` are compacted into one by replacing | ||
the last event of `application` instead of creating a new one. | ||
|
||
Returns the event as it went into the db." | ||
[application event] | ||
(let [last-event (-> application | ||
:application/events | ||
last)] | ||
(if (and (:enable-save-compaction env) | ||
(= :application.event/draft-saved | ||
(:event/type event) | ||
(:event/type last-event)) | ||
(= (:application/id event) | ||
(:application/id last-event))) | ||
Comment on lines
+91
to
+92
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can we omit this check, i think application id should always be the same? (= (:application/id event)
(:application/id last-event)) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Now that I look like it, yes we could. I added the condition because it didn't seem to work otherwise, but with a closer inspection it mostly should have and it was probably something else. Technically there can be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think I'll leave the check here as it is cheap. So it's kind of regression protection if we choose to create more complex commands or use the function from elsewhere. I don't want to fetch the application of each event here with |
||
(replace-event! (merge event | ||
(select-keys last-event [:event/id]))) | ||
(add-event! event)))) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the existing SQL function has a little silly return, considering the name indicates it should return single event but it returns a list of events. maybe that could be refactored at some point :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I think the correct naming convention that we use elsewhere is
:get-application-events
(which does exist too). There could be a db function named:get-application-event
but it should have been declared likeget-latest-application-event
i.e.:1
. This is a bit of leftover from my refactoring of combining together some previous functions, but choosing the wrong name / not finishing all the changes. "search" functions should accept many parameters and return0..n
and singular "get" functions should always return0..1
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#2971