Improve snapshot related internal API

This commit also adds the ability to take snapshot of all files
of the team in a single run/transaction.
This commit is contained in:
Andrey Antukh 2024-02-08 15:21:31 +01:00 committed by Andrés Moya
parent aaf457a792
commit 6d35cb2eb4
7 changed files with 433 additions and 286 deletions

View file

@ -12,7 +12,6 @@
[app.db :as db]
[app.features.components-v2 :as feat]
[app.main :as main]
[app.rpc.commands.files-snapshot :as rpc]
[app.srepl.helpers :as h]
[app.svgo :as svgo]
[app.util.cache :as cache]
@ -634,42 +633,3 @@
:file-name (:name file))
(assoc file :deleted-at (dt/now)))
file))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; RESTORE SNAPSHOT
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(def ^:private sql:snapshots-with-file
"SELECT f.id AS file_id,
fc.id AS id
FROM file AS f
JOIN file_change AS fc ON (fc.file_id = f.id)
WHERE fc.label = ? AND f.id = ANY(?)")
(defn restore-team!
[team-id label & {:keys [rollback?] :or {rollback? true}}]
(let [team-id (h/parse-uuid team-id)
get-file-snapshots
(fn [conn ids]
(let [label (str "migration/" label)]
(db/exec! conn [sql:snapshots-with-file label
(db/create-array conn "uuid" ids)])))
restore-snapshot
(fn [{:keys [::db/conn] :as system}]
(let [ids (into #{} (feat/get-and-lock-files conn team-id))
snap (get-file-snapshots conn ids)
ids' (into #{} (map :file-id) snap)
team (-> (feat/get-team conn team-id)
(update :features disj "components/v2"))]
(when (not= ids ids')
(throw (RuntimeException. "no uniform snapshot available")))
(feat/update-team! conn team)
(run! (partial rpc/restore-file-snapshot! system) snap)))]
(-> (assoc main/system ::db/rollback rollback?)
(db/tx-run! restore-snapshot))))