-
Notifications
You must be signed in to change notification settings - Fork 6
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
[BE] Truncate 를 위한 DatabaseCleanup 구현 #118
Merged
Merged
Changes from 10 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
8646b1a
[Docs] GitHub Issue 및 PR Template 설정 (#37)
junpakPark 5b153cc
[Docs] GitHub Issue Template 파일명 오류 수정 (#39)
junpakPark c54a142
Merge branch 'develop' of https://github.com/woowacourse-teams/2023-m…
kpeel5839 1397964
Merge branch 'develop' of https://github.com/woowacourse-teams/2023-m…
kpeel5839 d178bc5
Merge branch 'develop' of https://github.com/woowacourse-teams/2023-m…
kpeel5839 fd27fe8
test : Database Truncate 위한 DatabaseCleanup 구현
kpeel5839 6d03390
style : 코드 컨벤션으로 인한 개행 추가
kpeel5839 eb513f1
refactor : @Service -> @Component 및 guava 의존성 제거
kpeel5839 33ab588
fix: guava import 문 제거
kpeel5839 0542ed0
refactor: 메서드, 변수 분리
kpeel5839 5ac2fa8
refactor: disable/enable Referential query 상수로 변환
kpeel5839 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
92 changes: 92 additions & 0 deletions
92
backend/src/test/java/com/mapbefine/mapbefine/DatabaseCleanup.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
package com.mapbefine.mapbefine; | ||
|
||
import jakarta.persistence.Entity; | ||
import jakarta.persistence.EntityManager; | ||
import jakarta.persistence.PersistenceContext; | ||
import jakarta.persistence.metamodel.EntityType; | ||
import java.util.List; | ||
import java.util.Set; | ||
import org.springframework.beans.factory.InitializingBean; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
@Component | ||
public class DatabaseCleanup implements InitializingBean { | ||
|
||
private static final String SET_REFERENTIAL_INTEGRITY_SQL_MESSAGE = "SET REFERENTIAL_INTEGRITY %s"; | ||
private static final String TRUNCATE_SQL_MESSAGE = "TRUNCATE TABLE %s"; | ||
private static final String ID_RESET_SQL_MESSAGE = "ALTER TABLE %s ALTER COLUMN ID RESTART WITH 1"; | ||
private static final String UNDERSCORE = "_"; | ||
|
||
@PersistenceContext | ||
private EntityManager entityManager; | ||
|
||
private List<String> tableNames; | ||
|
||
@Override | ||
public void afterPropertiesSet() { | ||
Set<EntityType<?>> entities = entityManager.getMetamodel() | ||
.getEntities(); | ||
|
||
tableNames = entities.stream() | ||
.filter(this::isEntity) | ||
.map(this::convertTableNameFromCamelCaseToSnakeCase) | ||
.toList(); | ||
} | ||
|
||
private boolean isEntity(final EntityType<?> entityType) { | ||
return entityType.getJavaType() | ||
.getAnnotation(Entity.class) != null; | ||
} | ||
|
||
private String convertTableNameFromCamelCaseToSnakeCase(EntityType<?> entityType) { | ||
StringBuilder tableNameSnake = new StringBuilder(); | ||
String classNameOfEntity = entityType.getName(); | ||
|
||
for (char letter : classNameOfEntity.toCharArray()) { | ||
addUnderScoreForCapitalLetter(tableNameSnake, letter); | ||
tableNameSnake.append(letter); | ||
} | ||
|
||
return tableNameSnake.substring(1).toLowerCase(); | ||
} | ||
|
||
private void addUnderScoreForCapitalLetter(StringBuilder tableNameSnake, char letter) { | ||
if (Character.isUpperCase(letter)) { | ||
tableNameSnake.append(UNDERSCORE); | ||
} | ||
} | ||
|
||
@Transactional | ||
public void execute() { | ||
executeSqlWithReferentialIntegrityDisabled(this::executeTruncate); | ||
} | ||
|
||
private void executeSqlWithReferentialIntegrityDisabled(Runnable sqlExecutor) { | ||
disableReferentialIntegrity(); | ||
sqlExecutor.run(); | ||
enableReferentialIntegrity(); | ||
} | ||
|
||
private void disableReferentialIntegrity() { | ||
entityManager.flush(); | ||
|
||
entityManager.createNativeQuery(String.format(SET_REFERENTIAL_INTEGRITY_SQL_MESSAGE, false)) | ||
.executeUpdate(); | ||
} | ||
|
||
private void enableReferentialIntegrity() { | ||
entityManager.createNativeQuery(String.format(SET_REFERENTIAL_INTEGRITY_SQL_MESSAGE, true)) | ||
.executeUpdate(); | ||
} | ||
|
||
private void executeTruncate() { | ||
for (String tableName : tableNames) { | ||
entityManager.createNativeQuery(String.format(TRUNCATE_SQL_MESSAGE, tableName)) | ||
.executeUpdate(); | ||
entityManager.createNativeQuery(String.format(ID_RESET_SQL_MESSAGE, tableName)) | ||
.executeUpdate(); | ||
} | ||
} | ||
|
||
} |
16 changes: 13 additions & 3 deletions
16
backend/src/test/java/com/mapbefine/mapbefine/integration/IntegrationTest.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,30 @@ | ||
package com.mapbefine.mapbefine.integration; | ||
|
||
import com.mapbefine.mapbefine.DatabaseCleanup; | ||
import io.restassured.RestAssured; | ||
import org.junit.jupiter.api.AfterEach; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.boot.test.web.server.LocalServerPort; | ||
import org.springframework.test.context.jdbc.Sql; | ||
|
||
@Sql("/initialization.sql") | ||
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) | ||
public class IntegrationTest { | ||
|
||
@LocalServerPort | ||
int port; | ||
private int port; | ||
|
||
@Autowired | ||
private DatabaseCleanup databaseCleanup; | ||
|
||
@BeforeEach | ||
public void setUp() { | ||
RestAssured.port = port; | ||
} | ||
|
||
@AfterEach | ||
public void tearDown() { | ||
databaseCleanup.execute(); | ||
} | ||
|
||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
String.format도 변수 처리해주면 어떨까요