Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#99] soft delete 적용 #103

Merged
merged 2 commits into from
Feb 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
import javax.persistence.ManyToOne;
import javax.validation.constraints.Size;

import org.hibernate.annotations.SQLDelete;
import org.hibernate.annotations.Where;

import com.prgrms.prolog.domain.post.model.Post;
import com.prgrms.prolog.domain.user.model.User;
import com.prgrms.prolog.global.common.BaseEntity;
Expand All @@ -25,6 +28,8 @@
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Entity
@SQLDelete(sql = "UPDATE comment SET deleted = true where id=?")
@Where(clause = "deleted=false")
public class Comment extends BaseEntity {

private static final String CONTENT_OVER_LENGTH_MESSAGE = "exception.comment.content.overLength";
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/com/prgrms/prolog/domain/post/model/Post.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import javax.persistence.OneToMany;

import org.hibernate.annotations.ColumnDefault;
import org.hibernate.annotations.SQLDelete;
import org.hibernate.annotations.Where;

import com.prgrms.prolog.domain.comment.model.Comment;
import com.prgrms.prolog.domain.post.dto.PostDto.UpdatePostRequest;
Expand All @@ -36,6 +38,8 @@
@Getter
@NoArgsConstructor(access = PROTECTED)
@Entity
@SQLDelete(sql = "UPDATE post SET deleted = true where id=?")
@Where(clause = "deleted=false")
public class Post extends BaseEntity {

private static final int TITLE_MAX_SIZE = 50;
Expand Down
13 changes: 8 additions & 5 deletions src/main/java/com/prgrms/prolog/domain/series/model/Series.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,13 @@
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;

import org.hibernate.annotations.SQLDelete;
import org.hibernate.annotations.Where;
import org.springframework.util.Assert;

import com.prgrms.prolog.domain.post.model.Post;
import com.prgrms.prolog.domain.user.model.User;
import com.prgrms.prolog.global.common.BaseEntity;

import lombok.Builder;
import lombok.Getter;
Expand All @@ -28,9 +31,9 @@
@Getter
@NoArgsConstructor(access = PROTECTED)
@Entity
// @SQLDelete(sql = "UPDATE series SET deleted = true where id=?")
// @Where(clause = "deleted=false")
public class Series {
@SQLDelete(sql = "UPDATE series SET deleted = true where id=?")
@Where(clause = "deleted=false")
public class Series extends BaseEntity {

private static final int TITLE_MAX_SIZE = 50;

Expand All @@ -40,15 +43,15 @@ public class Series {

private String title;

private boolean deleted;

@OneToMany(mappedBy = "series")
private final List<Post> posts = new ArrayList<>();

@ManyToOne(fetch = LAZY)
@JoinColumn(name = "user_id")
private User user;

private boolean deleted;

@Builder
public Series(String title, User user, Post post) {
this.title = validateTitle(title);
Expand Down
10 changes: 4 additions & 6 deletions src/main/java/com/prgrms/prolog/domain/user/model/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,6 @@
import static javax.persistence.GenerationType.*;
import static lombok.AccessLevel.*;

import static com.prgrms.prolog.domain.user.dto.UserDto.*;
import static com.prgrms.prolog.global.util.ValidateUtil.*;
import static javax.persistence.GenerationType.*;
import static lombok.AccessLevel.*;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
Expand All @@ -25,12 +20,13 @@
import javax.persistence.Table;
import javax.validation.constraints.Size;

import org.hibernate.annotations.SQLDelete;
import org.hibernate.annotations.Where;
import org.springframework.util.Assert;

import com.prgrms.prolog.domain.comment.model.Comment;
import com.prgrms.prolog.domain.post.model.Post;
import com.prgrms.prolog.domain.series.model.Series;
import com.prgrms.prolog.domain.user.dto.UserDto;
import com.prgrms.prolog.domain.usertag.model.UserTag;
import com.prgrms.prolog.global.common.BaseEntity;

Expand All @@ -42,6 +38,8 @@
@NoArgsConstructor(access = PROTECTED)
@Entity
@Table(name = "users")
@SQLDelete(sql = "UPDATE users SET deleted = true where id=?")
@Where(clause = "deleted=false")
public class User extends BaseEntity {

private static final Pattern emailPattern
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/com/prgrms/prolog/global/common/BaseEntity.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,6 @@ public abstract class BaseEntity {
@CreatedBy
@Column(name = "created_by")
private String createdBy;

private boolean deleted;
}
10 changes: 10 additions & 0 deletions src/main/resources/db/migration/V3__add_soft_deleted_field.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
ALTER TABLE users
change deleted_at deleted tinyint(1) DEFAULT false;
ALTER TABLE post
change deleted_at deleted boolean DEFAULT false;
ALTER TABLE series
change deleted_at deleted boolean DEFAULT false;
ALTER TABLE comment
change deleted_at deleted boolean DEFAULT false;
ALTER TABLE series
ADD created_by varchar(100) NULL;
34 changes: 16 additions & 18 deletions src/test/resources/schema.sql
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
-- init.sql
# create database if not exists prolog;
# use prolog;
-- create database if not exists prolog;
-- use prolog;
DROP TABLE IF EXISTS likes;
DROP TABLE IF EXISTS user_tag;
DROP TABLE IF EXISTS post_tag;
Expand All @@ -25,17 +25,17 @@ CREATE TABLE users
created_by varchar(100) NULL,
created_at datetime NOT NULL DEFAULT now(),
updated_at datetime NOT NULL DEFAULT now(),
deleted_at datetime
deleted boolean NOT NULL DEFAULT false
);

CREATE TABLE series
(
id bigint NOT NULL PRIMARY KEY AUTO_INCREMENT,
title varchar(200) NOT NULL,
created_at datetime NOT NULL DEFAULT now(),
created_by varchar(100) NULL,
updated_at datetime NOT NULL DEFAULT now(),
deleted_at datetime,
deleted boolean NOT NULL DEFAULT false,
deleted boolean NOT NULL DEFAULT false,
user_id bigint NOT NULL,
FOREIGN KEY fk_series_user_id (user_id) REFERENCES users (id)
);
Expand All @@ -45,30 +45,31 @@ CREATE TABLE post
id bigint NOT NULL PRIMARY KEY AUTO_INCREMENT,
title varchar(200) NOT NULL,
content text NOT NULL,
open_status tinyint(1) NOT NULL DEFAULT 0,
open_status tinyint(1) NOT NULL DEFAULT 0,
created_by varchar(100) NULL,
created_at datetime NOT NULL DEFAULT now(),
updated_at datetime NOT NULL DEFAULT now(),
deleted_at datetime,
deleted boolean NOT NULL DEFAULT false,
user_id bigint NOT NULL,
series_id bigint NULL,
series_id bigint NULL,
like_count INT DEFAULT 0,
FOREIGN KEY fk_post_user_id (user_id) REFERENCES users (id),
FOREIGN KEY fk_post_series_id (series_id) REFERENCES series (id)
);

CREATE TABLE social_account
(
id bigint NOT NULL PRIMARY KEY AUTO_INCREMENT,
id bigint NOT NULL PRIMARY KEY AUTO_INCREMENT,
email varchar(100),
facebook_id varchar(100),
github_id varchar(100),
twitter_id varchar(100),
blog_url varchar(100),
created_by varchar(100) NULL,
created_at datetime NOT NULL DEFAULT now(),
updated_at datetime NOT NULL DEFAULT now(),
deleted_at datetime,
user_id bigint NOT NULL,
created_at datetime NOT NULL DEFAULT now(),
updated_at datetime NOT NULL DEFAULT now(),
deleted boolean NOT NULL DEFAULT false,
user_id bigint NOT NULL,
FOREIGN KEY fk_social_account_user_id (user_id) REFERENCES users (id)
);

Expand All @@ -79,7 +80,7 @@ CREATE TABLE comment
created_by varchar(100) NULL,
created_at datetime NOT NULL DEFAULT now(),
updated_at datetime NOT NULL DEFAULT now(),
deleted_at datetime,
deleted boolean NOT NULL DEFAULT false,
post_id bigint NOT NULL,
user_id bigint NOT NULL,
FOREIGN KEY fk_comment_post_id (post_id) REFERENCES post (id),
Expand Down Expand Up @@ -118,7 +119,4 @@ CREATE TABLE likes
post_id bigint NOT NULL,
FOREIGN KEY fk_likes_user_id (user_id) REFERENCES users (id),
FOREIGN KEY fk_likes_post_id (post_id) REFERENCES post (id)
);

ALTER TABLE post
ADD like_count INT DEFAULT 0;
);