diff --git a/backend/src/app/srepl/fixes/lost_colors.clj b/backend/src/app/srepl/fixes/lost_colors.clj new file mode 100644 index 0000000000..6ea8bb23ae --- /dev/null +++ b/backend/src/app/srepl/fixes/lost_colors.clj @@ -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))))) diff --git a/backend/src/app/srepl/main.clj b/backend/src/app/srepl/main.clj index 7a07f0671e..915499bc52 100644 --- a/backend/src/app/srepl/main.clj +++ b/backend/src/app/srepl/main.clj @@ -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 diff --git a/common/src/app/common/files/migrations.cljc b/common/src/app/common/files/migrations.cljc index c49843a787..324254902d 100644 --- a/common/src/app/common/files/migrations.cljc +++ b/common/src/app/common/files/migrations.cljc @@ -1481,7 +1481,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) @@ -1492,8 +1492,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))) @@ -1586,5 +1584,5 @@ "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" "0009-add-partial-text-touched-flags"])) diff --git a/common/src/app/common/types/color.cljc b/common/src/app/common/types/color.cljc index 6b3893d3ef..7d55b99c19 100644 --- a/common/src/app/common/types/color.cljc +++ b/common/src/app/common/types/color.cljc @@ -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))