From 188126a895344cf38724b51e84f930873098ef50 Mon Sep 17 00:00:00 2001 From: Andrey Antukh Date: Wed, 19 May 2021 16:02:38 +0200 Subject: [PATCH] :sparkles: Properly use dumped objects on initial data load process. --- backend/src/app/migrations.clj | 3 +++ .../sql/0055-mod-file-media-object-table.sql | 4 ++++ backend/src/app/rpc/mutations/management.clj | 12 ++++++------ backend/src/app/setup/initial_data.clj | 16 +++++++++++----- 4 files changed, 24 insertions(+), 11 deletions(-) create mode 100644 backend/src/app/migrations/sql/0055-mod-file-media-object-table.sql diff --git a/backend/src/app/migrations.clj b/backend/src/app/migrations.clj index 26e28f181..8e6350995 100644 --- a/backend/src/app/migrations.clj +++ b/backend/src/app/migrations.clj @@ -172,6 +172,9 @@ {:name "0054-add-audit-log-table" :fn (mg/resource "app/migrations/sql/0054-add-audit-log-table.sql")} + + {:name "0055-mod-file-media-object-table" + :fn (mg/resource "app/migrations/sql/0055-mod-file-media-object-table.sql")} ]) diff --git a/backend/src/app/migrations/sql/0055-mod-file-media-object-table.sql b/backend/src/app/migrations/sql/0055-mod-file-media-object-table.sql new file mode 100644 index 000000000..7017cb8d9 --- /dev/null +++ b/backend/src/app/migrations/sql/0055-mod-file-media-object-table.sql @@ -0,0 +1,4 @@ +ALTER TABLE file_media_object + DROP CONSTRAINT file_media_object_thumbnail_id_fkey, + ADD CONSTRAINT file_media_object_thumbnail_id_fkey + FOREIGN KEY (thumbnail_id) REFERENCES storage_object (id) ON DELETE SET NULL; diff --git a/backend/src/app/rpc/mutations/management.clj b/backend/src/app/rpc/mutations/management.clj index 177d02964..cc38374b2 100644 --- a/backend/src/app/rpc/mutations/management.clj +++ b/backend/src/app/rpc/mutations/management.clj @@ -91,21 +91,21 @@ (def sql:retrieve-used-media-objects "select fmo.* from file_media_object as fmo - inner join storage_object as o on (fmo.media_id = o.id) + inner join storage_object as so on (fmo.media_id = so.id) where fmo.file_id = ? - and o.deleted_at is null") + and so.deleted_at is null") (defn duplicate-file - [conn {:keys [profile-id file index project-id name]} {:keys [reset-shared-flag] :as opts}] - (let [flibs (db/exec! conn [sql:retrieve-used-libraries (:id file)]) - fmeds (db/exec! conn [sql:retrieve-used-media-objects (:id file)]) + [conn {:keys [profile-id file index project-id name flibs fmeds]} {:keys [reset-shared-flag] :as opts}] + (let [flibs (or flibs (db/exec! conn [sql:retrieve-used-libraries (:id file)])) + fmeds (or fmeds (db/exec! conn [sql:retrieve-used-media-objects (:id file)])) ;; memo uniform creation/modification date now (dt/now) ignore (dt/plus now (dt/duration {:seconds 5})) ;; add to the index all file media objects. - index (reduce #(assoc %1 (:id %2) (uuid/next)) index fmeds) + index (reduce #(assoc %1 (:id %2) (uuid/next)) index fmeds) flibs-xf (comp (map #(remap-id % index :file-id)) diff --git a/backend/src/app/setup/initial_data.clj b/backend/src/app/setup/initial_data.clj index 5514e7103..6532a54e5 100644 --- a/backend/src/app/setup/initial_data.clj +++ b/backend/src/app/setup/initial_data.clj @@ -8,7 +8,7 @@ (:refer-clojure :exclude [load]) (:require [app.common.uuid :as uuid] - [app.config :as cfg] + [app.config :as cf] [app.db :as db] [app.rpc.mutations.management :refer [duplicate-file]] [app.rpc.mutations.projects :refer [create-project create-project-role]] @@ -36,7 +36,7 @@ ([system project-id {:keys [skey project-name] :or {project-name "Penpot Onboarding"}}] (db/with-atomic [conn (:app.db/pool system)] - (let [skey (or skey (cfg/get :initial-project-skey)) + (let [skey (or skey (cf/get :initial-project-skey)) files (db/exec! conn [sql:file project-id]) flibs (db/exec! conn [sql:file-library-rel project-id]) fmeds (db/exec! conn [sql:file-media-object project-id]) @@ -65,7 +65,7 @@ (defn load-initial-project! ([conn profile] (load-initial-project! conn profile nil)) ([conn profile opts] - (let [skey (or (:skey opts) (cfg/get :initial-project-skey)) + (let [skey (or (:skey opts) (cf/get :initial-project-skey)) data (retrieve-data conn skey)] (when data (let [index (reduce #(assoc %1 (:id %2) (uuid/next)) {} (:files data)) @@ -82,10 +82,16 @@ :role :owner}) (doseq [file (:files data)] - (let [params {:profile-id (:id profile) + (let [flibs (filterv #(= (:id file) (:file-id %)) (:flibs data)) + fmeds (filterv #(= (:id file) (:file-id %)) (:fmeds data)) + + params {:profile-id (:id profile) :project-id (:id project) :file file - :index index} + :index index + :flibs flibs + :fmeds fmeds} + opts {:reset-shared-flag false}] (duplicate-file conn params opts))))))))