Merge pull request #6784 from penpot/niwinz-staging-hotfix-3

🐛 Fix incorrect library color cleaning mechanism
This commit is contained in:
Alejandro Alonso 2025-06-25 16:21:58 +02:00 committed by GitHub
commit 61109c91e3
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 76 additions and 6 deletions

View file

@ -0,0 +1,71 @@
;; This Source Code Form is subject to the terms of the Mozilla Public
;; License, v. 2.0. If a copy of the MPL was not distributed with this
;; file, You can obtain one at http://mozilla.org/MPL/2.0/.
;;
;; Copyright (c) KALEIDOS INC
(ns app.srepl.fixes.lost-colors
"A collection of adhoc fixes scripts."
(:require
[app.binfile.common :as bfc]
[app.common.logging :as l]
[app.common.types.color :as types.color]
[app.db :as db]
[app.srepl.helpers :as h]))
(def sql:get-affected-files
"SELECT fm.file_id AS id FROM file_migration AS fm WHERE fm.name = '0008-fix-library-colors-v2'")
(def sql:get-matching-snapshot
"SELECT * FROM file_change
WHERE file_id = ?
AND created_at <= ?
AND label IS NOT NULL
AND data IS NOT NULL
ORDER BY created_at DESC
LIMIT 1")
(defn get-affected-migration
[conn file-id]
(db/get* conn :file-migration
{:name "0008-fix-library-colors-v2"
:file-id file-id}))
(defn get-last-valid-snapshot
[conn migration]
(when-let [snapshot (db/exec-one! conn [sql:get-matching-snapshot
(:file-id migration)
(:created-at migration)])]
(let [snapshot (assoc snapshot :id (:file-id snapshot))]
(bfc/decode-file h/*system* snapshot))))
(defn restore-color
[{:keys [data] :as snapshot} color]
(when-let [scolor (get-in data [:colors (:id color)])]
(-> (select-keys scolor types.color/library-color-attrs)
(types.color/check-library-color))))
(defn restore-missing-colors
[{:keys [id] :as file} & _opts]
(when-let [colors (-> file :data :colors not-empty)]
(when-let [migration (get-affected-migration h/*system* id)]
(when-let [snapshot (get-last-valid-snapshot h/*system* migration)]
(let [colors (reduce-kv (fn [colors color-id color]
(if-let [result (restore-color snapshot color)]
(do
(l/inf :hint "restored color" :file-id (str id) :color-id (str color-id))
(assoc colors color-id result))
(do
(l/wrn :hint "ignoring color" :file-id (str id) :color (pr-str color))
colors)))
colors
colors)
file (-> file
(update :data assoc :colors colors)
(update :migrations disj "0008-fix-library-colors-v2"))]
(db/delete! h/*system* :file-migration
{:name "0008-fix-library-colors-v2"
:file-id (:id file)})
file)))))

View file

@ -474,7 +474,8 @@
:index idx)
(let [system (assoc main/system ::db/rollback rollback?)]
(db/tx-run! system (fn [system]
(binding [h/*system* system]
(binding [h/*system* system
db/*conn* (db/get-connection system)]
(h/process-file! system file-id update-fn opts)))))
(catch Throwable cause

View file

@ -1480,7 +1480,7 @@
(update :pages-index d/update-vals update-container)
(d/update-when :components d/update-vals update-container))))
(defmethod migrate-data "0008-fix-library-colors-v2"
(defmethod migrate-data "0008-fix-library-colors-v3"
[data _]
(letfn [(clear-color-opacity [color]
(if (and (contains? color :opacity)
@ -1491,8 +1491,6 @@
(clear-color [color]
(-> color
(select-keys types.color/library-color-attrs)
(d/without-nils)
(d/without-qualified)
(clear-color-opacity)))]
(d/update-when data :colors d/update-vals clear-color)))
@ -1560,4 +1558,4 @@
"0005-deprecate-image-type"
"0006-fix-old-texts-fills"
"0007-clear-invalid-strokes-and-fills-v2"
"0008-fix-library-colors-v2"]))
"0008-fix-library-colors-v3"]))

View file

@ -151,7 +151,7 @@
[:fn has-valid-color-attrs?]])
(def library-color-attrs
(sm/keys schema:library-color-attrs))
(into required-color-attrs (sm/keys schema:library-color-attrs)))
(def valid-color?
(sm/lazy-validator schema:color))