mirror of
https://github.com/penpot/penpot.git
synced 2025-05-13 14:46:38 +02: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.data :as d]
|
||||||
[uxbox.common.exceptions :as ex]
|
[uxbox.common.exceptions :as ex]
|
||||||
[uxbox.common.pages :as cp]
|
[uxbox.common.pages :as cp]
|
||||||
[uxbox.common.migrations :as mg]
|
[uxbox.common.pages-migrations :as pmg]
|
||||||
[uxbox.common.spec :as us]
|
[uxbox.common.spec :as us]
|
||||||
[uxbox.common.uuid :as uuid]
|
[uxbox.common.uuid :as uuid]
|
||||||
[uxbox.config :as cfg]
|
[uxbox.config :as cfg]
|
||||||
|
@ -182,7 +182,7 @@
|
||||||
|
|
||||||
page (-> page
|
page (-> page
|
||||||
(update :data blob/decode)
|
(update :data blob/decode)
|
||||||
(update :data mg/migrate-data)
|
(update :data pmg/migrate-data)
|
||||||
(update :data cp/process-changes changes)
|
(update :data cp/process-changes changes)
|
||||||
(update :data blob/encode)
|
(update :data blob/encode)
|
||||||
(update :revn inc)
|
(update :revn inc)
|
||||||
|
|
|
@ -13,7 +13,7 @@
|
||||||
[promesa.core :as p]
|
[promesa.core :as p]
|
||||||
[uxbox.common.spec :as us]
|
[uxbox.common.spec :as us]
|
||||||
[uxbox.common.exceptions :as ex]
|
[uxbox.common.exceptions :as ex]
|
||||||
[uxbox.common.migrations :as mg]
|
[uxbox.common.pages-migrations :as pmg]
|
||||||
[uxbox.db :as db]
|
[uxbox.db :as db]
|
||||||
[uxbox.services.queries :as sq]
|
[uxbox.services.queries :as sq]
|
||||||
[uxbox.services.queries.files :as files]
|
[uxbox.services.queries.files :as files]
|
||||||
|
@ -40,7 +40,7 @@
|
||||||
(db/with-atomic [conn db/pool]
|
(db/with-atomic [conn db/pool]
|
||||||
(files/check-edition-permissions! conn profile-id file-id)
|
(files/check-edition-permissions! conn profile-id file-id)
|
||||||
(->> (retrieve-pages conn params)
|
(->> (retrieve-pages conn params)
|
||||||
(mapv #(update % :data mg/migrate-data)))))
|
(mapv #(update % :data pmg/migrate-data)))))
|
||||||
|
|
||||||
(def ^:private sql:pages
|
(def ^:private sql:pages
|
||||||
"select p.*
|
"select p.*
|
||||||
|
@ -67,7 +67,7 @@
|
||||||
(let [page (retrieve-page conn id)]
|
(let [page (retrieve-page conn id)]
|
||||||
(files/check-edition-permissions! conn profile-id (:file-id page))
|
(files/check-edition-permissions! conn profile-id (:file-id page))
|
||||||
(-> page
|
(-> page
|
||||||
(update :data mg/migrate-data)))))
|
(update :data pmg/migrate-data)))))
|
||||||
|
|
||||||
(def ^:private sql:page
|
(def ^:private sql:page
|
||||||
"select p.* from page as p where id=?")
|
"select p.* from page as p where id=?")
|
||||||
|
|
|
@ -16,7 +16,7 @@
|
||||||
[uxbox.common.exceptions :as ex]
|
[uxbox.common.exceptions :as ex]
|
||||||
[uxbox.common.spec :as us]))
|
[uxbox.common.spec :as us]))
|
||||||
|
|
||||||
(def page-version 4)
|
(def page-version 5)
|
||||||
|
|
||||||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
||||||
;; Page Data Structure Helpers
|
;; Page Data Structure Helpers
|
||||||
|
|
|
@ -1,41 +1,60 @@
|
||||||
(ns uxbox.common.migrations
|
(ns uxbox.common.pages-migrations
|
||||||
(:require
|
(:require
|
||||||
[uxbox.common.pages :as p]
|
[uxbox.common.pages :as p]
|
||||||
[uxbox.common.geom.shapes :as gsh]
|
[uxbox.common.geom.shapes :as gsh]
|
||||||
[uxbox.common.geom.point :as gpt]
|
[uxbox.common.geom.point :as gpt]
|
||||||
[uxbox.common.geom.matrix :as gmt]
|
[uxbox.common.geom.matrix :as gmt]
|
||||||
|
[uxbox.common.spec :as us]
|
||||||
[uxbox.common.uuid :as uuid]
|
[uxbox.common.uuid :as uuid]
|
||||||
[uxbox.common.data :as d]))
|
[uxbox.common.data :as d]))
|
||||||
|
|
||||||
(defmulti migrate :version)
|
(defmulti migrate :version)
|
||||||
|
|
||||||
(defn migrate-data
|
(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 from-version to-version]
|
||||||
(-> data
|
(-> data
|
||||||
(assoc :version to-version)
|
(assoc :version to-version)
|
||||||
(migrate)))
|
(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)))))
|
|
||||||
|
|
||||||
;; Default handler, noop
|
;; Default handler, noop
|
||||||
(defmethod migrate :default [data] data)
|
(defmethod migrate :default [data] data)
|
||||||
|
|
||||||
;; -- MIGRATIONS --
|
;; -- MIGRATIONS --
|
||||||
|
|
||||||
(defmethod migrate 4 [data]
|
(defn- generate-child-parent-index
|
||||||
;; We changed the internal model of the shapes so they have their selection rect
|
[objects]
|
||||||
;; and the vertices
|
(reduce-kv
|
||||||
|
(fn [index id obj]
|
||||||
|
(into index (map #(vector % id) (:shapes obj []))))
|
||||||
|
{} objects))
|
||||||
|
|
||||||
(letfn [;; Creates a new property `points` that stores the transformed points inside the shape
|
(defmethod migrate 5
|
||||||
;; this will be used for the snaps and the selection rect
|
[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]
|
(calculate-shape-points [objects]
|
||||||
(->> objects
|
(->> objects
|
||||||
(d/mapm
|
(d/mapm
|
||||||
|
@ -44,7 +63,8 @@
|
||||||
shape
|
shape
|
||||||
(assoc shape :points (gsh/shape->points 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]
|
(calculate-shape-selrects [objects]
|
||||||
(->> objects
|
(->> objects
|
||||||
(d/mapm
|
(d/mapm
|
||||||
|
@ -53,7 +73,6 @@
|
||||||
shape
|
shape
|
||||||
(assoc shape :selrect (gsh/points->selrect (:points shape))))))))]
|
(assoc shape :selrect (gsh/points->selrect (:points shape))))))))]
|
||||||
(-> data
|
(-> data
|
||||||
|
|
||||||
;; Adds vertices to shapes
|
;; Adds vertices to shapes
|
||||||
(update :objects calculate-shape-points)
|
(update :objects calculate-shape-points)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue