-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* test: truncate sql로 실행하는 버전 * feat: jdbc-url에 cacheDefaultTimeZone 설정 추가 * refactor: 클리너 불필요한 메서드 제거, 리팩터링
- Loading branch information
Showing
3 changed files
with
49 additions
and
4 deletions.
There are no files selected for viewing
39 changes: 39 additions & 0 deletions
39
backend/src/test/java/com/woowacourse/zzimkkong/DatabaseCleaner.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,39 @@ | ||
package com.woowacourse.zzimkkong; | ||
|
||
import org.springframework.beans.factory.InitializingBean; | ||
import org.springframework.context.annotation.Profile; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
import javax.persistence.EntityManager; | ||
import javax.persistence.PersistenceContext; | ||
import java.util.List; | ||
import java.util.Locale; | ||
import java.util.stream.Collectors; | ||
|
||
@Component | ||
@Profile("test") | ||
public class DatabaseCleaner implements InitializingBean { | ||
@PersistenceContext | ||
private EntityManager entityManager; | ||
|
||
private List<String> tableNames; | ||
|
||
@Override | ||
public void afterPropertiesSet() { | ||
tableNames = entityManager.getMetamodel().getEntities().stream() | ||
.map(entry -> entry.getName().toLowerCase(Locale.ROOT)) | ||
.collect(Collectors.toList()); | ||
} | ||
|
||
@Transactional | ||
public void execute() { | ||
entityManager.flush(); | ||
entityManager.createNativeQuery("SET REFERENTIAL_INTEGRITY FALSE").executeUpdate(); | ||
for (String tableName : tableNames) { | ||
entityManager.createNativeQuery("TRUNCATE TABLE " + tableName).executeUpdate(); | ||
entityManager.createNativeQuery("ALTER TABLE " + tableName + " ALTER COLUMN id RESTART WITH 1").executeUpdate(); | ||
} | ||
entityManager.createNativeQuery("SET REFERENTIAL_INTEGRITY TRUE").executeUpdate(); | ||
} | ||
} |
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
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