Skip to content

Commit 8db61a0

Browse files
authored
Merge pull request #3213 from CSCfi/release-2.34.1
Release 2.34.1
2 parents 96bfb99 + 51fb7f6 commit 8db61a0

File tree

5 files changed

+135
-3
lines changed

5 files changed

+135
-3
lines changed

CHANGELOG.md

+8-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,14 @@ have notable changes.
66

77
## Unreleased
88

9-
Changes since v2.34
9+
Changes since v2.34.1
10+
11+
## v2.34.1 "Santakatu +1" 2023-11-03
12+
13+
**NB: This release contains migrations!**
14+
15+
### Fixes
16+
- A bug in event selection prevented migration (introduced in v2.34) from applying properly. This release contains a fixed version of that migration.
1017

1118
## v2.34 "Santakatu" 2023-10-31
1219

project.clj

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
(defproject rems "2.34"
1+
(defproject rems "2.34.1"
22
:description "Resource Entitlement Management System is a tool for managing access rights to resources, such as research datasets."
33
:url "https://github.com/CSCfi/rems"
44

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{:ns rems.migrations.change-expiration-events-1
2+
:up-fn migrate-up
3+
:down-fn migrate-down}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
(ns ^{:added "2.34.1"} rems.migrations.change-expiration-events-1
2+
"Migrates event `:expires-on` to `:application/expires-on`
3+
and removes the `:last-activity`.
4+
5+
Runs the same migration as `rems.migrations.change-expiration-events`,
6+
but fixes issue where no events would get migrated, and renames :expires-on
7+
so that migrate-up does not result in schema error due to extra keys."
8+
(:require [clojure.set]
9+
[hugsql.core :as hugsql]
10+
[rems.db.applications]
11+
[rems.db.core]
12+
[rems.json :as json]))
13+
14+
(def migration-id 20231103083019)
15+
16+
(declare get-events set-event!)
17+
(hugsql/def-db-fns-from-string
18+
"
19+
-- :name get-events :? :*
20+
SELECT id, eventdata::TEXT
21+
FROM application_event;
22+
23+
-- :name set-event! :!
24+
UPDATE application_event
25+
SET eventdata = :eventdata::jsonb
26+
WHERE id = :id;
27+
")
28+
29+
(defn attributes-up [eventdata]
30+
(-> eventdata
31+
(clojure.set/rename-keys {:expires-on :application/expires-on})
32+
(dissoc :last-activity)))
33+
34+
(defn attributes-down [eventdata]
35+
(-> eventdata
36+
(clojure.set/rename-keys {:application/expires-on :expires-on})
37+
(assoc :last-activity (:event/time eventdata)))) ; NB: not the same but it would be difficult to restore
38+
39+
(defn migrate-event [event migrator]
40+
(update event :eventdata migrator))
41+
42+
(def event-from-db #(update % :eventdata json/parse-string))
43+
44+
(defn migrate-events! [conn xf migrator]
45+
(doseq [event (sequence (comp (map event-from-db) xf) (get-events conn))
46+
:let [new-event (migrate-event event migrator)]]
47+
(set-event! conn
48+
(update new-event :eventdata json/generate-string))))
49+
50+
(def old-event? #(contains? (:eventdata %) :expires-on))
51+
(def new-event? #(contains? (:eventdata %) :application/expires-on))
52+
53+
(defn migrate-up [{:keys [conn]}]
54+
(migrate-events! conn (filter old-event?) attributes-up))
55+
56+
(defn migrate-down [{:keys [conn]}]
57+
(migrate-events! conn (filter new-event?) attributes-down))
58+
59+
(comment
60+
(migrate-up {:conn rems.db.core/*db*})
61+
(migrate-down {:conn rems.db.core/*db*}))

test/clj/rems/test_migrations.clj

+62-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
(ns ^:integration rems.test-migrations
2+
"Migration tests that use database."
23
(:require [clojure.test :refer [deftest is testing use-fixtures]]
34
[hugsql.core :as hugsql]
45
[luminus-migrations.core]
@@ -8,7 +9,9 @@
89
[rems.db.testing :refer [test-db-fixture reset-db-fixture rollback-db-fixture]]
910
[rems.config]
1011
[rems.json :as json]
11-
[rems.migrations.event-public :as event-public]))
12+
[rems.migrations.event-public :as event-public]
13+
[rems.migrations.change-expiration-events]
14+
[rems.migrations.change-expiration-events-1]))
1215

1316
(use-fixtures
1417
:once
@@ -79,3 +82,61 @@
7982
(->> (get-events)
8083
(mapv (comp json/parse-string :eventdata))))))))
8184

85+
(deftest test-change-expiration-events
86+
(let [create-application!
87+
(create-db-fn
88+
"-- :name create-application! :insert
89+
INSERT INTO catalogue_item_application (id)
90+
VALUES (nextval ('catalogue_item_application_id_seq'))
91+
RETURNING id;
92+
")
93+
add-application-event!
94+
(create-db-fn
95+
"-- :name add-application-event! :returning-execute :1
96+
INSERT INTO application_event (appId, eventData)
97+
VALUES (:application, :eventdata::jsonb)
98+
RETURNING id, eventData::TEXT;
99+
")
100+
get-events #(->> (rems.migrations.change-expiration-events/get-events rems.db.core/*db*)
101+
(sort-by :id))
102+
previous-migration-id rems.migrations.change-expiration-events/migration-id]
103+
104+
(testing "create test data"
105+
(rollback-until-just-before previous-migration-id)
106+
107+
(let [app (first (create-application!))
108+
_ (is (:id app))
109+
create-event #(do {:application (:id app) :eventdata (json/generate-string %)})]
110+
(is (:id (first (add-application-event!
111+
(create-event {:expires-on "2023-11-03T11:53:31.469Z"
112+
:event/time "2023-10-01T00:00:00.000Z"
113+
:last-activity "2023-10-27T11:53:31.469Z"})))))
114+
(is (:id (first (add-application-event!
115+
(create-event {:expires-on "2100-01-01T00:00:00.000Z"
116+
:event/time "2099-10-01T00:00:00.000Z"})))))
117+
(is (:id (first (add-application-event!
118+
(create-event {:event/actor "alice"})))))
119+
(is (= [{:expires-on "2023-11-03T11:53:31.469Z" :event/time "2023-10-01T00:00:00.000Z" :last-activity "2023-10-27T11:53:31.469Z"}
120+
{:expires-on "2100-01-01T00:00:00.000Z" :event/time "2099-10-01T00:00:00.000Z"}
121+
{:event/actor "alice"}]
122+
(->> (get-events)
123+
(mapv (comp json/parse-string :eventdata)))))))
124+
125+
(testing "migrate up"
126+
(rems.migrations.change-expiration-events/migrate-up {:conn rems.db.core/*db*}) ; does not change events but here for consistency
127+
(rems.migrations.change-expiration-events-1/migrate-up {:conn rems.db.core/*db*})
128+
(is (= [{:application/expires-on "2023-11-03T11:53:31.469Z" :event/time "2023-10-01T00:00:00.000Z"}
129+
{:application/expires-on "2100-01-01T00:00:00.000Z" :event/time "2099-10-01T00:00:00.000Z"}
130+
{:event/actor "alice"}]
131+
(->> (get-events)
132+
(mapv (comp json/parse-string :eventdata))))))
133+
134+
(testing "migrate down"
135+
(rems.migrations.change-expiration-events-1/migrate-down {:conn rems.db.core/*db*})
136+
(rems.migrations.change-expiration-events/migrate-down {:conn rems.db.core/*db*})
137+
(is (= [{:expires-on "2023-11-03T11:53:31.469Z" :event/time "2023-10-01T00:00:00.000Z" :last-activity "2023-10-01T00:00:00.000Z"}
138+
{:expires-on "2100-01-01T00:00:00.000Z" :event/time "2099-10-01T00:00:00.000Z" :last-activity "2099-10-01T00:00:00.000Z"}
139+
{:event/actor "alice"}]
140+
(->> (get-events)
141+
(mapv (comp json/parse-string :eventdata))))))))
142+

0 commit comments

Comments
 (0)