♻️ Add minor refactor to file migrations

Relevant changes:

- Add the ability to create migration in both directions, defaulting
  to identity if not provided
- Move the version attribute to file table column for to make it more
  accessible (previously it was on data blob)
- Reduce db update operations on file-update rpc method
This commit is contained in:
Andrey Antukh 2024-02-14 11:37:27 +01:00
parent 7ac4b89a0e
commit b718a282e0
16 changed files with 240 additions and 151 deletions

View file

@ -7,11 +7,42 @@
(ns common-tests.files-migrations-test
(:require
[app.common.data :as d]
[app.common.files.migrations :as cpm]
[app.common.files.migrations :as cfm]
[app.common.pprint :as pp]
[app.common.uuid :as uuid]
[clojure.pprint :refer [pprint]]
[clojure.test :as t]))
(t/deftest test-generic-migration-subsystem-1
(let [migrations [{:id 1 :migrate-up (comp inc inc) :migrate-down (comp dec dec)}
{:id 2 :migrate-up (comp inc inc) :migrate-down (comp dec dec)}
{:id 3 :migrate-up (comp inc inc) :migrate-down (comp dec dec)}
{:id 4 :migrate-up (comp inc inc) :migrate-down (comp dec dec)}
{:id 5 :migrate-up (comp inc inc) :migrate-down (comp dec dec)}
{:id 6 :migrate-up (comp inc inc) :migrate-down (comp dec dec)}
{:id 7 :migrate-up (comp inc inc) :migrate-down (comp dec dec)}
{:id 8 :migrate-up (comp inc inc) :migrate-down (comp dec dec)}
{:id 9 :migrate-up (comp inc inc) :migrate-down (comp dec dec)}
{:id 10 :migrate-up (comp inc inc) :migrate-down (comp dec dec)}
{:id 11 :migrate-up (comp inc inc) :migrate-down (comp dec dec)}
{:id 12 :migrate-up (comp inc inc) :migrate-down (comp dec dec)}
{:id 13 :migrate-up (comp inc inc) :migrate-down (comp dec dec)}]]
(t/testing "migrate up 1"
(let [result (cfm/migrate-data 0 migrations 0 2)]
(t/is (= result 4))))
(t/testing "migrate up 2"
(let [result (cfm/migrate-data 0 migrations 0 20)]
(t/is (= result 26))))
(t/testing "migrate down 1"
(let [result (cfm/migrate-data 12 migrations 6 3)]
(t/is (= result 6))))
(t/testing "migrate down 2"
(let [result (cfm/migrate-data 12 migrations 6 0)]
(t/is (= result 0))))))
(t/deftest test-migration-8-1
(let [page-id (uuid/custom 0 0)
objects [{:type :rect :id (uuid/custom 1 0)}
@ -34,16 +65,11 @@
{:type :path :id (uuid/custom 1 5)}]
data {:pages-index {page-id {:objects (d/index-by :id objects)}}
:components {}
:version 7}
:components {}}
res (cpm/migrate-data data 8)]
res (cfm/migrate-data data cfm/migrations 7 8)]
;; (pprint data)
;; (pprint res)
(t/is (= (dissoc data :version)
(dissoc res :version)))))
(t/is (= data res))))
(t/deftest test-migration-8-2
(let [page-id (uuid/custom 0 0)
@ -67,8 +93,7 @@
{:type :path :id (uuid/custom 1 5)}]
data {:pages-index {page-id {:objects (d/index-by :id objects)}}
:components {}
:version 7}
:components {}}
expect (-> data
(update-in [:pages-index page-id :objects] dissoc
@ -80,10 +105,9 @@
(let [id (uuid/custom 1 2)]
(into [] (remove #(= id %)) shapes)))))
res (cpm/migrate-data data 8)]
res (cfm/migrate-data data cfm/migrations 7 8)]
;; (pprint res)
;; (pprint expect)
(t/is (= (dissoc expect :version)
(dissoc res :version)))))
(t/is (= expect res))))