mirror of
https://github.com/penpot/penpot.git
synced 2025-06-10 11:51:37 +02:00
🐛 Simplify the impl of profile deletion.
This commit is contained in:
parent
1210924562
commit
afedd397a7
1 changed files with 34 additions and 52 deletions
|
@ -5,20 +5,24 @@
|
||||||
;; This Source Code Form is "Incompatible With Secondary Licenses", as
|
;; This Source Code Form is "Incompatible With Secondary Licenses", as
|
||||||
;; defined by the Mozilla Public License, v. 2.0.
|
;; defined by the Mozilla Public License, v. 2.0.
|
||||||
;;
|
;;
|
||||||
;; Copyright (c) 2020 UXBOX Labs SL
|
;; Copyright (c) 2020-2021 UXBOX Labs SL
|
||||||
|
|
||||||
(ns app.tasks.delete-profile
|
(ns app.tasks.delete-profile
|
||||||
"Task for permanent deletion of profiles."
|
"Task for permanent deletion of profiles."
|
||||||
(:require
|
(:require
|
||||||
[app.common.spec :as us]
|
[app.common.spec :as us]
|
||||||
[app.db :as db]
|
[app.db :as db]
|
||||||
|
[app.db.sql :as sql]
|
||||||
[app.metrics :as mtx]
|
[app.metrics :as mtx]
|
||||||
[clojure.spec.alpha :as s]
|
[clojure.spec.alpha :as s]
|
||||||
[clojure.tools.logging :as log]
|
[clojure.tools.logging :as log]
|
||||||
[integrant.core :as ig]))
|
[integrant.core :as ig]))
|
||||||
|
|
||||||
|
(declare delete-profile-data)
|
||||||
(declare handler)
|
(declare handler)
|
||||||
|
|
||||||
|
;; --- INIT
|
||||||
|
|
||||||
(defmethod ig/pre-init-spec ::handler [_]
|
(defmethod ig/pre-init-spec ::handler [_]
|
||||||
(s/keys :req-un [::db/pool ::mtx/metrics]))
|
(s/keys :req-un [::db/pool ::mtx/metrics]))
|
||||||
|
|
||||||
|
@ -31,74 +35,52 @@
|
||||||
:help "delete profile task timing"}
|
:help "delete profile task timing"}
|
||||||
(mtx/instrument handler))))
|
(mtx/instrument handler))))
|
||||||
|
|
||||||
(declare delete-profile-data)
|
;; This task is responsible to permanently delete a profile with all
|
||||||
(declare delete-teams)
|
;; the dependent data. As step (1) we delete all owned teams of the
|
||||||
(declare delete-files)
|
;; profile (that will cause to delete all underlying projects, files,
|
||||||
(declare delete-profile)
|
;; file_media and mark to be deleted storage_object's used by team,
|
||||||
|
;; profile and files previously deleted. Then, finally as step (2) we
|
||||||
|
;; proceed to delete the profile row.
|
||||||
|
;;
|
||||||
|
;; The storage_objects marked as deleted will be deleted by the
|
||||||
|
;; corresponding garbage collector task.
|
||||||
|
|
||||||
(s/def ::profile-id ::us/uuid)
|
(s/def ::profile-id ::us/uuid)
|
||||||
(s/def ::props
|
(s/def ::props (s/keys :req-un [::profile-id]))
|
||||||
(s/keys :req-un [::profile-id]))
|
|
||||||
|
|
||||||
(defn handler
|
(defn handler
|
||||||
[{:keys [pool]} {:keys [props] :as task}]
|
[{:keys [pool]} {:keys [props] :as task}]
|
||||||
(us/verify ::props props)
|
(us/verify ::props props)
|
||||||
(db/with-atomic [conn pool]
|
(db/with-atomic [conn pool]
|
||||||
(let [id (:profile-id props)
|
(let [id (:profile-id props)
|
||||||
profile (db/get-by-id conn :profile id {:for-update true})]
|
profile (db/exec-one! conn (sql/select :profile {:id id} {:for-update true}))]
|
||||||
(if (or (:is-demo profile)
|
(if (or (:is-demo profile)
|
||||||
(not (nil? (:deleted-at profile))))
|
(:deleted-at profile))
|
||||||
(delete-profile-data conn (:id profile))
|
(delete-profile-data conn id)
|
||||||
(log/warn "Profile " (:id profile)
|
(log/warnf "Profile %s does not match constraints for deletion" id)))))
|
||||||
"does not match constraints for deletion")))))
|
|
||||||
|
|
||||||
(defn- delete-profile-data
|
;; --- IMPL
|
||||||
[conn profile-id]
|
|
||||||
(log/info "Proceding to delete all data related to profile" profile-id)
|
|
||||||
(delete-teams conn profile-id)
|
|
||||||
(delete-files conn profile-id)
|
|
||||||
(delete-profile conn profile-id))
|
|
||||||
|
|
||||||
(def ^:private sql:remove-owned-teams
|
(def ^:private sql:remove-owned-teams
|
||||||
"with teams as (
|
"delete from team
|
||||||
select distinct
|
where id in (
|
||||||
tpr.team_id as id
|
select tpr.team_id
|
||||||
from team_profile_rel as tpr
|
from team_profile_rel as tpr
|
||||||
where tpr.profile_id = ?
|
where tpr.is_owner is true
|
||||||
and tpr.is_owner is true
|
and tpr.profile_id = ?
|
||||||
), to_delete_teams as (
|
)")
|
||||||
select tpr.team_id as id
|
|
||||||
from team_profile_rel as tpr
|
|
||||||
where tpr.team_id in (select id from teams)
|
|
||||||
group by tpr.team_id
|
|
||||||
having count(tpr.profile_id) = 1
|
|
||||||
)
|
|
||||||
delete from team
|
|
||||||
where id in (select id from to_delete_teams)
|
|
||||||
returning id")
|
|
||||||
|
|
||||||
(defn- delete-teams
|
(defn- delete-teams
|
||||||
[conn profile-id]
|
[conn profile-id]
|
||||||
(db/exec-one! conn [sql:remove-owned-teams profile-id]))
|
(db/exec-one! conn [sql:remove-owned-teams profile-id]))
|
||||||
|
|
||||||
(def ^:private sql:remove-owned-files
|
|
||||||
"with files_to_delete as (
|
|
||||||
select distinct
|
|
||||||
fpr.file_id as id
|
|
||||||
from file_profile_rel as fpr
|
|
||||||
inner join file as f on (fpr.file_id = f.id)
|
|
||||||
where fpr.profile_id = ?
|
|
||||||
and fpr.is_owner is true
|
|
||||||
and f.project_id is null
|
|
||||||
)
|
|
||||||
delete from file
|
|
||||||
where id in (select id from files_to_delete)
|
|
||||||
returning id")
|
|
||||||
|
|
||||||
(defn- delete-files
|
|
||||||
[conn profile-id]
|
|
||||||
(db/exec-one! conn [sql:remove-owned-files profile-id]))
|
|
||||||
|
|
||||||
(defn delete-profile
|
(defn delete-profile
|
||||||
[conn profile-id]
|
[conn profile-id]
|
||||||
(db/delete! conn :profile {:id profile-id}))
|
(db/delete! conn :profile {:id profile-id}))
|
||||||
|
|
||||||
|
(defn- delete-profile-data
|
||||||
|
[conn profile-id]
|
||||||
|
(log/infof "Proceding to delete all data related to profile id = %s" profile-id)
|
||||||
|
(delete-teams conn profile-id)
|
||||||
|
(delete-profile conn profile-id))
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue