Skip to content

Commit

Permalink
Add support for deleting old versions
Browse files Browse the repository at this point in the history
  • Loading branch information
egli committed Feb 14, 2024
1 parent dd1889d commit 71ec2e4
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 1 deletion.
14 changes: 14 additions & 0 deletions resources/sql/queries.sql
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,20 @@ VALUES (:comment, :document_id, :content, :user)
-- :doc Delete a version.
DELETE FROM documents_version WHERE id = :id

-- :name delete-old-versions :! :n
-- :doc Delete all but the latest versions for given `document_id`.
-- FIXME: really this should be DELETE FROM documents_version WHERE document_id = :document_id AND id NOT IN (SELECT id FROM documents_version WHERE document_id = :document_id ORDER BY created_at desc LIMIT 1)
-- but this is apparently not supported yet by the version of mariadb that we have
DELETE FROM documents_version
WHERE document_id = :document_id
AND id <> (
SELECT * FROM (
SELECT id FROM documents_version
WHERE document_id = :document_id
ORDER BY created_at DESC
LIMIT 1)
AS dt)

------------
-- Images --
------------
Expand Down
16 changes: 16 additions & 0 deletions src/clj/daisyproducer2/documents/versions.clj
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,22 @@
deletions)
0)) ;; since we could not find the version we'll return zero deletions

(defn delete-old-versions
"Delete all but the latest version given a `document-id`. Return the number of rows affected."
[document-id]
;; we need to fetch the versions first to know the path to the xml file, which we
;; will have to delete also
(let [old-versions (rest (db/get-versions {:document_id document-id}))]
(if (seq old-versions)
(do
(doseq [old-version old-versions]
(when-not (fs/delete-if-exists (version-path old-version))
;; if an version file does not exist we simply log that fact, but do
;; not raise an exception
(log/errorf "Attempting to delete non-existing version file %s" (version-path old-version))))
(db/delete-old-versions {:document_id document-id}))
0)))

(prometheus/instrument! metrics/registry #'get-versions)
(prometheus/instrument! metrics/registry #'get-version)
(prometheus/instrument! metrics/registry #'get-latest)
Expand Down
10 changes: 9 additions & 1 deletion src/clj/daisyproducer2/routes/services.clj
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,15 @@
message (ex-message e)]
(log/warn message error-id errors)
(bad-request {:status-text (ex-message e) :errors errors})))
))}}]
))}
:delete {:summary "Delete all but the latest versions of a given document"
:middleware [wrap-restricted]
:swagger {:security [{:apiAuth []}]}
:parameters {:path {:id int?}}
:handler (fn [{{{:keys [id]} :path} :parameters
{{uid :uid} :user} :identity}]
(let [deleted (versions/delete-old-versions id)]
(ok {:deleted deleted})))}}]

["/:version-id"
[""
Expand Down
4 changes: 4 additions & 0 deletions test/rest-api.http
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,10 @@ Authorization: :auth-token
DELETE http://localhost:3000/api/documents/105/versions/8449
Authorization: :auth-token

# Delete old versions
DELETE http://localhost:3000/api/documents/105/versions
Authorization: :auth-token

##########
# Images #
##########
Expand Down

0 comments on commit 71ec2e4

Please sign in to comment.