From 932544996f10cdefd95491f95330f2347bd64229 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=81lvaro=20Tejero-Cantero?= <807608+alvorithm@users.noreply.github.com> Date: Fri, 20 Nov 2026 13:15:35 +0100 Subject: [PATCH] :sparkles: Add a flag that makes file-gc skip its media clean-up (#13220) The new backend flag `file-change-retention`, off by default, makes file-gc skip its media clean-up (deleted components, unused media objects, and thumbnails). That clean-up roots only in the head and the snapshots, so it would remove media that a kept revision row still names. The file and fragment clean-up still run: revision rows name neither. penpot#13187 keeps revision rows under the same flag. Set the flag only where both are deployed. An instance that rolls back to this release with the flag still set keeps the media its retained history needs. AI-assisted-by: claude-opus-5-5 --- backend/src/app/tasks/file_gc.clj | 9 +++++++-- backend/test/backend_tests/rpc_file_test.clj | 16 ++++++++++++++++ common/src/app/common/flags.cljc | 5 +++++ 3 files changed, 28 insertions(+), 2 deletions(-) diff --git a/backend/src/app/tasks/file_gc.clj b/backend/src/app/tasks/file_gc.clj index 1cfc71b9a3..d168bda095 100644 --- a/backend/src/app/tasks/file_gc.clj +++ b/backend/src/app/tasks/file_gc.clj @@ -272,9 +272,14 @@ (feat.fdata/with-pointers cfg file-id (let [liveness (collect-snapshot-liveness cfg file-id) original (feat.fdata/original-pointers (:data file)) + file (bfl/clean-file file) + ;; With retention on, kept revision rows can name media and + ;; components that neither the head nor the snapshots name, + ;; and the media cleaners do not read those rows. + file (if (contains? cf/flags :file-change-retention) + file + (clean-media! cfg liveness file)) file (->> file - (bfl/clean-file) - (clean-media! cfg liveness) (feat.fdata/reattach-unchanged original) (clean-fragments! cfg (:pointers liveness))) file (assoc file :has-media-trimmed true)] diff --git a/backend/test/backend_tests/rpc_file_test.clj b/backend/test/backend_tests/rpc_file_test.clj index 376f412b1b..3c6a8a46ea 100644 --- a/backend/test/backend_tests/rpc_file_test.clj +++ b/backend/test/backend_tests/rpc_file_test.clj @@ -3537,6 +3537,22 @@ (t/is (true? (th/run-task! :file-gc {:file-id (:id file)}))) (t/is (empty? (:call-args-list @audit-mock))))))) +(t/deftest file-gc-keeps-unused-media-with-the-retention-flag + ;; A kept revision row can name a media object that the head no longer + ;; names, and file-gc does not read revision rows, so the flag turns its + ;; media clean-up off. + (let [[_ file-1] (unused-media-fixture 1) + [_ file-2] (unused-media-fixture 2) + media-row (fn [file] (th/db-get :file-media-object {:file-id (:id file)} + {::db/remove-deleted false}))] + (t/is (true? (th/run-task! :file-gc {:file-id (:id file-1)}))) + (with-redefs [cf/flags (conj cf/flags :file-change-retention)] + (t/is (true? (th/run-task! :file-gc {:file-id (:id file-2)})))) + + (t/is (ct/inst? (:deleted-at (media-row file-1)))) + (t/is (nil? (:deleted-at (media-row file-2)))) + (t/is (true? (:has-media-trimmed (th/db-get :file {:id (:id file-2)})))))) + (t/deftest update-file-audit-event-carries-the-file-change-id ;; The event says that a file was saved, but not what changed: the ;; change row (xlog) is the only place with that, so its id travels diff --git a/common/src/app/common/flags.cljc b/common/src/app/common/flags.cljc index e76dc92f84..08bae79bd0 100644 --- a/common/src/app/common/flags.cljc +++ b/common/src/app/common/flags.cljc @@ -90,6 +90,11 @@ ;; on the database (server-error-report) and notified to mattermost ;; (only when the webhook url is also configured). :error-reporting + ;; Disabled by default. When enabled, file-gc skips its media clean-up, + ;; which does not read revision rows, and the revision writer keeps + ;; revision rows with no expiry (penpot#13187). Enable it only where + ;; both are deployed. + :file-change-retention ;; Activates the schema validation during update file. :file-schema-validation ;; Reports the schema validation errors internally.