Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: migrate forms and field-values
- from :form/id ... to :application/forms [{:form/id ...}] - from :field-values {field value} to :field-values [{:form ... :field ... :value ...}]
- Loading branch information
Showing
2 changed files
with
71 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{:ns rems.migrations.multiple-forms | ||
:up-fn migrate-up | ||
:down-fn nil} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
(ns rems.migrations.multiple-forms | ||
(:require [hugsql.core :as hugsql] | ||
[medley.core :refer [map-keys]] | ||
[rems.json :as json] | ||
[rems.util :refer [getx update-present]])) | ||
|
||
(hugsql/def-db-fns-from-string | ||
" | ||
-- :name get-application-events :? :* | ||
SELECT id, eventdata::TEXT | ||
FROM application_event | ||
WHERE 1=1 | ||
/*~ (when (:application params) */ | ||
AND appId = :application | ||
/*~ ) ~*/ | ||
ORDER BY id ASC; | ||
-- :name set-event! :! | ||
UPDATE application_event | ||
SET eventdata = (:eventdata::jsonb) | ||
WHERE id = :id; | ||
") | ||
|
||
(defn- get-application-forms [conn] | ||
(reduce (fn [m {:keys [id eventdata]}] | ||
(let [event (json/parse-string eventdata)] | ||
(if (and (= "application.event/created" (:event/type event)) | ||
(:form/id event)) | ||
(assoc m (:application/id event) (:form/id event)) | ||
m))) | ||
{} | ||
(get-application-events conn))) | ||
|
||
(defmulti migrate-event (fn [id event application-forms] (:event/type event))) | ||
|
||
(defmethod migrate-event :default [id event application-forms] | ||
nil) | ||
|
||
(defmethod migrate-event "application.event/draft-saved" | ||
[id event application-forms] | ||
(when (map? (:application/field-values event)) | ||
(let [form-id (getx application-forms (:application/id event))] | ||
(update event | ||
:application/field-values | ||
(fn [m] | ||
(vec (for [[field-id field-value] m] | ||
{:form form-id :field field-id :value field-value}))))))) | ||
|
||
(defmethod migrate-event "application.event/created" | ||
[id event _application-forms] | ||
(when (:form/id event) | ||
(-> event | ||
(dissoc :form/id) | ||
(assoc :application/forms [{:form/id (:form/id event)}])))) | ||
|
||
(defn- migrate-events [conn application-forms] | ||
(doseq [{:keys [id eventdata]} (get-application-events conn)] | ||
(let [event (json/parse-string eventdata) | ||
new-event (migrate-event id event application-forms)] | ||
(when new-event | ||
(set-event! conn {:id id :eventdata (json/generate-string new-event)}))))) | ||
|
||
(defn migrate-up [{:keys [conn]}] | ||
(let [application-forms (get-application-forms conn)] | ||
(migrate-events conn application-forms))) | ||
|
||
(comment | ||
(migrate-up {:conn rems.db.core/*db*})) |