Skip to content

Commit

Permalink
refactor: DataBaseCleanup JdbcTemplate ์ ์šฉ (#371)
Browse files Browse the repository at this point in the history
  • Loading branch information
cpot5620 authored Sep 1, 2023
1 parent 731f0fa commit 6c887a6
Showing 1 changed file with 23 additions and 58 deletions.
81 changes: 23 additions & 58 deletions backend/src/test/java/com/mapbefine/mapbefine/DatabaseCleanup.java
Original file line number Diff line number Diff line change
@@ -1,98 +1,63 @@
package com.mapbefine.mapbefine;

import jakarta.persistence.Entity;
import jakarta.persistence.EntityManager;
import jakarta.persistence.PersistenceContext;
import jakarta.persistence.metamodel.EntityType;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

@Component
public class DatabaseCleanup implements InitializingBean {

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 SET_REFERENTIAL_INTEGRITY_SQL_MESSAGE = "SET REFERENTIAL_INTEGRITY %s";
private static final String UNDERSCORE = "_";
private static final String DISABLE_REFERENTIAL_QUERY = String.format(SET_REFERENTIAL_INTEGRITY_SQL_MESSAGE, false);
private static final String ENABLE_REFERENTIAL_QUERY = String.format(SET_REFERENTIAL_INTEGRITY_SQL_MESSAGE, true);

@PersistenceContext
private EntityManager entityManager;
@Autowired
private JdbcTemplate jdbcTemplate;

private List<String> tableNames;
private List<String> tableNames = new ArrayList<>();

@Override
public void afterPropertiesSet() {
Set<EntityType<?>> entities = entityManager.getMetamodel()
.getEntities();
public void afterPropertiesSet() throws SQLException {
ResultSet rs = jdbcTemplate.getDataSource()
.getConnection()
.getMetaData()
.getTables(null, "PUBLIC", null, new String[]{"TABLE"});

tableNames = entities.stream()
.filter(this::isEntity)
.map(this::convertTableNameFromCamelCaseToSnakeCase)
.toList();
}

private boolean isEntity(final EntityType<?> entityType) {
return entityType.getJavaType()
.getAnnotation(Entity.class) != null;
}
while (rs.next()) {
String tableName = rs.getString("TABLE_NAME");

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);
this.tableNames.add(tableName);
}
}

@Transactional
public void execute() {
executeSqlWithReferentialIntegrityDisabled(this::executeTruncate);
}

private void executeSqlWithReferentialIntegrityDisabled(Runnable sqlExecutor) {
disableReferentialIntegrity();
sqlExecutor.run();
executeTruncate();
enableReferentialIntegrity();
}

private void disableReferentialIntegrity() {
entityManager.flush();

entityManager.createNativeQuery(DISABLE_REFERENTIAL_QUERY)
.executeUpdate();
}

private void enableReferentialIntegrity() {

entityManager.createNativeQuery(ENABLE_REFERENTIAL_QUERY)
.executeUpdate();
jdbcTemplate.execute(DISABLE_REFERENTIAL_QUERY);
}

private void executeTruncate() {
for (String tableName : tableNames) {
String TRUNCATE_QUERY = String.format(TRUNCATE_SQL_MESSAGE, tableName);
String ID_RESET_QUERY = String.format(ID_RESET_SQL_MESSAGE, tableName);

entityManager.createNativeQuery(TRUNCATE_QUERY)
.executeUpdate();
entityManager.createNativeQuery(ID_RESET_QUERY)
.executeUpdate();
jdbcTemplate.execute(TRUNCATE_QUERY);
}
}

private void enableReferentialIntegrity() {
jdbcTemplate.execute(ENABLE_REFERENTIAL_QUERY);
}

}

0 comments on commit 6c887a6

Please sign in to comment.