From 351362bb505d968edf2ccbb985bb46248cec9dac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20Moya?= Date: Wed, 23 Jul 2025 15:12:41 +0200 Subject: [PATCH] :bug: Fix migration from tokens lib version 1.2 --- common/src/app/common/types/tokens_lib.cljc | 41 ++++++++++++++++++--- 1 file changed, 36 insertions(+), 5 deletions(-) diff --git a/common/src/app/common/types/tokens_lib.cljc b/common/src/app/common/types/tokens_lib.cljc index ecfd92b5da..5323e5484e 100644 --- a/common/src/app/common/types/tokens_lib.cljc +++ b/common/src/app/common/types/tokens_lib.cljc @@ -167,6 +167,11 @@ (get-tokens [_] "return an ordered sequence of all tokens in the set") (get-tokens-map [_] "return a map of tokens in the set, indexed by token-name")) +;; TODO: this structure is temporary. It's needed to be able to migrate TokensLib +;; from 1.2 to 1.3 when TokenSet datatype was changed to a deftype. This should +;; be removed after migrations are consolidated. +(defrecord TokenSetLegacy [id name description modified-at tokens]) + (deftype TokenSet [id name description modified-at tokens] #?@(:clj [clojure.lang.IDeref (deref [_] {:id id @@ -255,6 +260,10 @@ [o] (instance? TokenSet o)) +(defn token-set-legacy? + [o] + (instance? TokenSetLegacy o)) + (def schema:token-set-attrs [:map {:title "TokenSet"} [:id ::sm/uuid] @@ -1717,10 +1726,11 @@ Will return a value that matches this schema: migrate-sets-node (fn recurse [node] - (if (token-set? node) - (assoc node - :id (uuid/next) - :tokens (d/update-vals (:tokens node) migrate-token)) + (if (token-set-legacy? node) + (make-token-set + (assoc node + :id (uuid/next) + :tokens (d/update-vals (:tokens node) migrate-token))) (d/update-vals node recurse))) sets @@ -1748,6 +1758,19 @@ Will return a value that matches this schema: (->TokensLib sets themes active-themes)))) +#?(:clj + (defn- read-tokens-lib-v1-3 + "Reads the tokens lib data structure and removes the TokenSetLegacy data type, + needed for a temporary migration step." + [r] + (let [sets (fres/read-object! r) + themes (fres/read-object! r) + active-themes (fres/read-object! r) + + sets (d/update-vals sets make-token-set)] + + (->TokensLib sets themes active-themes)))) + #?(:clj (defn- write-tokens-lib [n w ^TokensLib o] @@ -1776,6 +1799,11 @@ Will return a value that matches this schema: (make-token obj)))} {:name "penpot/token-set/v1" + :rfn (fn [r] + (let [obj (fres/read-object! r)] + (map->TokenSetLegacy obj)))} + + {:name "penpot/token-set/v2" :class TokenSet :wfn (fn [n w o] (fres/write-tag! w n 1) @@ -1803,8 +1831,11 @@ Will return a value that matches this schema: {:name "penpot/tokens-lib/v1.2" :rfn read-tokens-lib-v1-2} - ;; CURRENT TOKENS LIB READER & WRITTER {:name "penpot/tokens-lib/v1.3" + :rfn read-tokens-lib-v1-3} + + ;; CURRENT TOKENS LIB READER & WRITTER + {:name "penpot/tokens-lib/v1.4" :class TokensLib :wfn write-tokens-lib :rfn read-tokens-lib}))