From ce7eed5ea07e59b357179ab16b0f7b1696ccb53e Mon Sep 17 00:00:00 2001 From: Andrey Antukh Date: Mon, 5 Sep 2022 13:17:01 +0200 Subject: [PATCH] :bug: Fix issues on deleting library which is in use by deleted files --- CHANGES.md | 2 ++ backend/src/app/rpc/mutations/files.clj | 32 +++++++++++-------------- backend/src/app/rpc/queries/files.clj | 9 +++---- 3 files changed, 21 insertions(+), 22 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index 346170458..f5194aa21 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -23,6 +23,8 @@ - Fix unexpected layers ungrouping on moving it [Taiga #3932](https://tree.taiga.io/project/penpot/issue/3932) by @andrewzhurov - Fix artboards moving with comment tool selected [Taiga #3938](https://tree.taiga.io/project/penpot/issue/3938) - Fix undo on delete page does not preserve its order [Taiga #3375](https://tree.taiga.io/project/penpot/issue/3375) +- Fix unexpected 404 on deleting library that is used by deleted files +- Fix inconsistent message on deleting library when a library is linked from deleted files ### :arrow_up: Deps updates ### :heart: Community contributions by (Thank you!) diff --git a/backend/src/app/rpc/mutations/files.clj b/backend/src/app/rpc/mutations/files.clj index 305ac8e2c..4cd4f1ab4 100644 --- a/backend/src/app/rpc/mutations/files.clj +++ b/backend/src/app/rpc/mutations/files.clj @@ -162,25 +162,21 @@ "Find all files using a shared library, and absorb all library assets into the file local libraries" [conn {:keys [id] :as params}] - (let [library (->> (db/get-by-id conn :file id) - (files/decode-row) - (pmg/migrate-file))] + (let [library (db/get-by-id conn :file id)] (when (:is-shared library) - (let [process-file - (fn [row] - (let [ts (dt/now) - file (->> (db/get-by-id conn :file (:file-id row)) - (files/decode-row) - (pmg/migrate-file)) - updated-data (ctf/absorb-assets (:data file) (:data library))] - - (db/update! conn :file - {:revn (inc (:revn file)) - :data (blob/encode updated-data) - :modified-at ts} - {:id (:id file)})))] - - (run! process-file (db/query conn :file-library-rel {:library-file-id id})))))) + (let [ldata (-> library files/decode-row pmg/migrate-file :data)] + (->> (db/query conn :file-library-rel {:library-file-id id}) + (keep (fn [{:keys [file-id]}] + (some->> (db/get-by-id conn :file file-id {:check-not-found false}) + (files/decode-row) + (pmg/migrate-file)))) + (run! (fn [{:keys [id data revn] :as file}] + (let [data (ctf/absorb-assets data ldata)] + (db/update! conn :file + {:revn (inc revn) + :data (blob/encode data) + :modified-at (dt/now)} + {:id id}))))))))) ;; --- Mutation: Link file to library diff --git a/backend/src/app/rpc/queries/files.clj b/backend/src/app/rpc/queries/files.clj index e8f6e6636..eb776d6b2 100644 --- a/backend/src/app/rpc/queries/files.clj +++ b/backend/src/app/rpc/queries/files.clj @@ -488,11 +488,12 @@ ;; --- Query: Files that use this File library (def ^:private sql:library-using-files - "SELECT f.id, + "SELECT f.id, f.name - FROM file_library_rel AS flr - INNER JOIN file AS f ON f.id = flr.file_id - WHERE flr.library_file_id = ?;") + FROM file_library_rel AS flr + JOIN file AS f ON (f.id = flr.file_id) + WHERE flr.library_file_id = ? + AND (f.deleted_at IS NULL OR f.deleted_at > now())") (s/def ::library-using-files (s/keys :req-un [::profile-id ::file-id]))