diff --git a/backend/src/uxbox/services/mutations/pages.clj b/backend/src/uxbox/services/mutations/pages.clj index 75fbdef38..67118fc99 100644 --- a/backend/src/uxbox/services/mutations/pages.clj +++ b/backend/src/uxbox/services/mutations/pages.clj @@ -13,7 +13,7 @@ [uxbox.common.data :as d] [uxbox.common.exceptions :as ex] [uxbox.common.pages :as cp] - [uxbox.common.migrations :as mg] + [uxbox.common.pages-migrations :as pmg] [uxbox.common.spec :as us] [uxbox.common.uuid :as uuid] [uxbox.config :as cfg] @@ -182,7 +182,7 @@ page (-> page (update :data blob/decode) - (update :data mg/migrate-data) + (update :data pmg/migrate-data) (update :data cp/process-changes changes) (update :data blob/encode) (update :revn inc) diff --git a/backend/src/uxbox/services/queries/pages.clj b/backend/src/uxbox/services/queries/pages.clj index 07ced67b7..fc85b2161 100644 --- a/backend/src/uxbox/services/queries/pages.clj +++ b/backend/src/uxbox/services/queries/pages.clj @@ -13,7 +13,7 @@ [promesa.core :as p] [uxbox.common.spec :as us] [uxbox.common.exceptions :as ex] - [uxbox.common.migrations :as mg] + [uxbox.common.pages-migrations :as pmg] [uxbox.db :as db] [uxbox.services.queries :as sq] [uxbox.services.queries.files :as files] @@ -40,7 +40,7 @@ (db/with-atomic [conn db/pool] (files/check-edition-permissions! conn profile-id file-id) (->> (retrieve-pages conn params) - (mapv #(update % :data mg/migrate-data))))) + (mapv #(update % :data pmg/migrate-data))))) (def ^:private sql:pages "select p.* @@ -67,7 +67,7 @@ (let [page (retrieve-page conn id)] (files/check-edition-permissions! conn profile-id (:file-id page)) (-> page - (update :data mg/migrate-data))))) + (update :data pmg/migrate-data))))) (def ^:private sql:page "select p.* from page as p where id=?") diff --git a/common/uxbox/common/pages.cljc b/common/uxbox/common/pages.cljc index 5d3b99802..6054169da 100644 --- a/common/uxbox/common/pages.cljc +++ b/common/uxbox/common/pages.cljc @@ -16,7 +16,7 @@ [uxbox.common.exceptions :as ex] [uxbox.common.spec :as us])) -(def page-version 4) +(def page-version 5) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; Page Data Structure Helpers diff --git a/common/uxbox/common/migrations.cljc b/common/uxbox/common/pages_migrations.cljc similarity index 54% rename from common/uxbox/common/migrations.cljc rename to common/uxbox/common/pages_migrations.cljc index 4cb445150..4fc0a6ca9 100644 --- a/common/uxbox/common/migrations.cljc +++ b/common/uxbox/common/pages_migrations.cljc @@ -1,41 +1,60 @@ -(ns uxbox.common.migrations +(ns uxbox.common.pages-migrations (:require [uxbox.common.pages :as p] [uxbox.common.geom.shapes :as gsh] [uxbox.common.geom.point :as gpt] [uxbox.common.geom.matrix :as gmt] + [uxbox.common.spec :as us] [uxbox.common.uuid :as uuid] [uxbox.common.data :as d])) (defmulti migrate :version) (defn migrate-data + ([data] + (if (= (:version data) p/page-version) + data + (reduce #(migrate-data %1 %2 (inc %2)) + data + (range (:version data 0) p/page-version)))) + ([data from-version to-version] (-> data (assoc :version to-version) - (migrate))) - - ([data] - (try - (reduce #(migrate-data %1 %2 (inc %2)) - data - (range (:version data 0) p/page-version)) - - ;; If an error is thrown, we log the error and return the data without migrations - #?(:clj (catch Exception e (.printStackTrace e) data) - :cljs (catch :default e (.error js/console e) data))))) + (migrate)))) ;; Default handler, noop (defmethod migrate :default [data] data) ;; -- MIGRATIONS -- -(defmethod migrate 4 [data] - ;; We changed the internal model of the shapes so they have their selection rect - ;; and the vertices - - (letfn [;; Creates a new property `points` that stores the transformed points inside the shape - ;; this will be used for the snaps and the selection rect +(defn- generate-child-parent-index + [objects] + (reduce-kv + (fn [index id obj] + (into index (map #(vector % id) (:shapes obj [])))) + {} objects)) + +(defmethod migrate 5 + [data] + (update data :objects + (fn [objects] + (let [index (generate-child-parent-index objects)] + (d/mapm + (fn [id obj] + (let [parent-id (get index id)] + (assoc obj :parent-id parent-id))) + objects))))) + +;; We changed the internal model of the shapes so they have their +;; selection rect and the vertices + +(defmethod migrate 4 + [data] + + (letfn [;; Creates a new property `points` that stores the + ;; transformed points inside the shape this will be used for + ;; the snaps and the selection rect (calculate-shape-points [objects] (->> objects (d/mapm @@ -44,7 +63,8 @@ shape (assoc shape :points (gsh/shape->points shape))))))) - ;; Creates a new property `selrect` that stores the selection rect for the shape + ;; Creates a new property `selrect` that stores the + ;; selection rect for the shape (calculate-shape-selrects [objects] (->> objects (d/mapm @@ -53,7 +73,6 @@ shape (assoc shape :selrect (gsh/points->selrect (:points shape))))))))] (-> data - ;; Adds vertices to shapes (update :objects calculate-shape-points)