Created
January 6, 2022 12:46
-
-
Save chrishowejones/65dabb628a710968a27d483e45c42d14 to your computer and use it in GitHub Desktop.
Recursive deletion experiment (as far as I got)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; Functions added to clash.api.postgres/video-comment | |
(defn get-all-by-user | |
[postgres user-id] | |
(postgres/execute! postgres | |
{:select [:*] | |
:from :video-comments | |
:where [:and | |
[:= :deleted-at nil] | |
[:= :creator-id user-id]]})) | |
(defn get-comments-by-parent | |
[postgres {::video-comment/keys [id]}] | |
(postgres/execute! postgres | |
{:select [:*] | |
:from :video-comments | |
:where [:and | |
[:= :deleted-at nil] | |
[:= :parent-id id]]})) | |
(defn delete-child-comment | |
[postgres {::video-comment/keys [id]}] | |
(postgres/execute-one! postgres | |
{:insert-into [[:video-comments-deleted | |
[:created_at | |
:creator_id | |
:deleted_at | |
:id | |
:parent_id | |
:text | |
:updated_at | |
:video_id | |
:is_liked_by_video_creator | |
:deleted_reason]] | |
{:select [:video-comments.created_at | |
:video-comments.creator_id | |
:video-comments.deleted_at | |
:video-comments.id | |
:video-comments.parent_id | |
:video-comments.text | |
:video-comments.updated_at | |
:video-comments.video_id | |
:video-comments.is_liked_by_video_creator | |
:video-comments.deleted_reason] | |
:from :video-comments | |
:where [:and | |
[:= :parent-id id] | |
[:= :deleted-at nil]]}]}) | |
(postgres/execute-one! postgres | |
{:delete-from :videos-comments | |
:where [:and | |
[:= :parent-id id] | |
[:= :deleted-at nil]]})) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; added to bottom of clash.api.postgres/video-comment-test | |
(defn- active-child-comment [video-id user-id parent-id created-at] | |
(-> {::video-comment/id (uuid/uuid) | |
::video-comment/text "some comment" | |
::video-comment/created-at created-at | |
::video-comment/is-liked-by-video-creator false | |
::video-comment/updated-at nil | |
::video-comment/deleted-reason nil} | |
(assoc ::video-comment/deleted-at nil | |
::video-comment/parent-id parent-id | |
::video-comment/creator-id user-id | |
::video-comment/video-id video-id))) | |
(defn child-comments [num-comments parent-id video-id user-id] | |
(let [created-at (timestamp/now)] | |
(->> #(active-child-comment video-id user-id parent-id created-at) | |
(repeatedly num-comments) | |
vec))) | |
(defn- setup-child-comments | |
[postgres num-children {user-id ::user/id} {::video-comment/keys [id video-id]}] | |
(let [child-comments (child-comments num-children id video-id user-id)] | |
(fixtures/insert-multi! postgres :video_comments child-comments))) | |
(def system | |
(atom nil)) | |
(defn start-system | |
[] | |
(reset! system (component/start-system (t/system)))) | |
(defn stop-system | |
[] | |
(swap! system #(component/stop-system %))) | |
(defn setup-recursive-comments | |
[] | |
(let [{:keys [postgres] :as system-map} @system | |
[user child grandchild] (user.fixtures/setup-active-user! postgres {} 3) | |
[video] (video.fixtures/setup-videos-for-user! postgres user 1) | |
top-level-comments (video-comment.fixtures/setup-comments-for-video! postgres | |
100 | |
user | |
video) | |
child-comments (mapcat (partial setup-child-comments postgres 100 child) top-level-comments)] | |
(run! (partial setup-child-comments postgres 100 grandchild) child-comments) | |
[user child grandchild])) | |
(defn recursively-fetch-comments | |
[postgres result parent] | |
(let [children (sut/get-comments-by-parent postgres parent)] | |
(if (zero? (count children)) | |
result | |
(mapv (partial recursively-fetch-comments postgres (cons children result)) children)))) | |
(comment | |
(start-system) | |
(def users | |
(setup-recursive-comments)) | |
(count users) | |
(let [{:keys [postgres]} @system] | |
(postgres/execute! postgres | |
{:select [[[:count :*]]] | |
:from :video-comments})) | |
(let [{:keys [postgres]} @system] | |
(postgres/execute! postgres | |
{:select [[[:count :*]]] | |
:from :video-comments | |
:where [:= :creator-id (::user/id (nth users 2))]})) | |
(let [{:keys [postgres]} @system | |
user-id (-> users first ::user/id) | |
top-level-comments (sut/get-all-by-user postgres user-id) | |
child-comments (mapcat (partial sut/get-comments-by-parent postgres) top-level-comments) | |
grandchild-comments (mapcat (partial sut/get-comments-by-parent postgres) child-comments)] | |
(count grandchild-comments)) | |
(let [{:keys [postgres]} @system | |
user-id (-> users first ::user/id) | |
top-level-comments (sut/get-all-by-user postgres user-id) | |
low-level-comments (reduce (partial recursively-fetch-comments postgres) [] top-level-comments) | |
] | |
(reduce #(+ %1 (count %2)) 0 low-level-comments)) | |
(let [{:keys [postgres]} @system | |
user-id (-> users first ::user/id) | |
top-level-comments (sut/get-all-by-user postgres user-id) | |
low-level-comments (reduce (partial recursively-fetch-comments postgres) [top-level-comments] top-level-comments)] | |
(count low-level-comments)) | |
(stop-system) | |
;; | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment