mirror of
https://github.com/penpot/penpot.git
synced 2025-01-09 00:10:11 -05:00
✨ Improve page data migrations.
This commit is contained in:
parent
ef42ffee65
commit
3e00d67504
4 changed files with 45 additions and 26 deletions
|
@ -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)
|
||||
|
|
|
@ -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=?")
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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)
|
||||
|
Loading…
Reference in a new issue