diff --git a/apps/api/src/main/resources/db/migration/V1__init.sql b/apps/api/src/main/resources/db/migration/V1__init.sql index a25e17e9..f201bc83 100644 --- a/apps/api/src/main/resources/db/migration/V1__init.sql +++ b/apps/api/src/main/resources/db/migration/V1__init.sql @@ -43,9 +43,9 @@ CREATE TABLE posts ( author_id UUID NOT NULL, text VARCHAR(280) NOT NULL, created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, - CONSTRAINT fk_parent_id FOREIGN KEY (parent_id) REFERENCES posts(id), - CONSTRAINT fk_conversation_id FOREIGN KEY (conversation_id) REFERENCES posts(id), - CONSTRAINT fk_author_id FOREIGN KEY (author_id) REFERENCES profiles(id) + CONSTRAINT fk_parent_id FOREIGN KEY (parent_id) REFERENCES posts(id) ON DELETE CASCADE, + CONSTRAINT fk_conversation_id FOREIGN KEY (conversation_id) REFERENCES posts(id) ON DELETE CASCADE, + CONSTRAINT fk_author_id FOREIGN KEY (author_id) REFERENCES profiles(id) ON DELETE CASCADE ); CREATE TABLE post_likes ( diff --git a/apps/api/src/test/java/com/example/echo_api/modules/post/repository/PostRepositoryIT.java b/apps/api/src/test/java/com/example/echo_api/modules/post/repository/PostRepositoryIT.java index 952d265f..553cf7b1 100644 --- a/apps/api/src/test/java/com/example/echo_api/modules/post/repository/PostRepositoryIT.java +++ b/apps/api/src/test/java/com/example/echo_api/modules/post/repository/PostRepositoryIT.java @@ -555,4 +555,17 @@ void PostRepository_FindPostsMentioningProfileId_ContainsOnlyPostsMentioningUser } } + @Test + void PostRepository_Delete_CascadeDeletesRelatedPosts() { + Post root = createPost(null, self.getId(), "root."); + Post replyToRoot = createPost(root.getId(), self.getId(), "reply to root."); + Post replyToReply = createPost(replyToRoot.getId(), self.getId(), "reply to reply."); + + postRepository.delete(root); + + assertFalse(postRepository.existsById(root.getId())); + assertFalse(postRepository.existsById(replyToRoot.getId())); + assertFalse(postRepository.existsById(replyToReply.getId())); + } + } \ No newline at end of file