diff --git a/resources/sql/queries.sql b/resources/sql/queries.sql index f048626..a599043 100644 --- a/resources/sql/queries.sql +++ b/resources/sql/queries.sql @@ -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 -- ------------ diff --git a/src/clj/daisyproducer2/documents/versions.clj b/src/clj/daisyproducer2/documents/versions.clj index 71ea839..510894d 100644 --- a/src/clj/daisyproducer2/documents/versions.clj +++ b/src/clj/daisyproducer2/documents/versions.clj @@ -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) diff --git a/src/clj/daisyproducer2/routes/services.clj b/src/clj/daisyproducer2/routes/services.clj index bb05d93..941735e 100644 --- a/src/clj/daisyproducer2/routes/services.clj +++ b/src/clj/daisyproducer2/routes/services.clj @@ -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" ["" diff --git a/test/rest-api.http b/test/rest-api.http index 149bcf8..978736a 100644 --- a/test/rest-api.http +++ b/test/rest-api.http @@ -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 # ##########