feat: 로컬 및 테스트 데이터베이스 mysql 이전 및 애플리케이션 종료시 테이블 truncate 처리 추가

This commit is contained in:
이상진 2025-10-02 21:02:20 +09:00
parent 173467821b
commit 99917df600
6 changed files with 54 additions and 21 deletions

View File

@ -0,0 +1,33 @@
package com.sangdol.roomescape.common.config
import io.github.oshai.kotlinlogging.KLogger
import io.github.oshai.kotlinlogging.KotlinLogging
import jakarta.annotation.PreDestroy
import jakarta.transaction.Transactional
import org.springframework.context.annotation.Profile
import org.springframework.jdbc.core.JdbcTemplate
import org.springframework.stereotype.Component
private val log: KLogger = KotlinLogging.logger {}
@Component
@Profile("local")
class LocalDatabaseCleaner(
private val jdbcTemplate: JdbcTemplate
) {
@PreDestroy
@Transactional
fun clearAll() {
log.info { "[LocalDatabaseCleaner] 데이터베이스 초기화 시작" }
jdbcTemplate.execute("SET FOREIGN_KEY_CHECKS = 0")
jdbcTemplate.query("SHOW TABLES") { rs, _ ->
rs.getString(1).lowercase()
}.forEach {
jdbcTemplate.execute("TRUNCATE TABLE $it")
}
jdbcTemplate.execute("SET FOREIGN_KEY_CHECKS = 1")
log.info { "[LocalDatabaseCleaner] 데이터베이스 초기화 완료" }
}
}

View File

@ -5,20 +5,16 @@ spring:
format_sql: true
hibernate:
ddl-auto: validate
h2:
console:
enabled: true
path: /h2-console
datasource:
hikari:
jdbc-url: jdbc:h2:mem:database
driver-class-name: org.h2.Driver
username: sa
password:
jdbc-url: jdbc:mysql://localhost:23306/roomescape_local
driver-class-name: com.mysql.cj.jdbc.Driver
username: root
password: init
sql:
init:
mode: always
schema-locations: classpath:schema/schema-h2.sql
schema-locations: classpath:schema/schema-mysql.sql
data-locations: classpath:schema/region-data.sql
security:

View File

@ -1,10 +1,10 @@
package com.sangdol.data
import com.sangdol.common.persistence.IDGenerator
import com.sangdol.common.persistence.TransactionExecutionUtil
import com.sangdol.roomescape.admin.infrastructure.persistence.AdminEntity
import com.sangdol.roomescape.admin.infrastructure.persistence.AdminPermissionLevel
import com.sangdol.roomescape.admin.infrastructure.persistence.AdminType
import com.sangdol.common.persistence.TransactionExecutionUtil
import com.sangdol.roomescape.payment.infrastructure.common.*
import com.sangdol.roomescape.reservation.infrastructure.persistence.ReservationStatus
import com.sangdol.roomescape.schedule.infrastructure.persistence.ScheduleStatus
@ -32,7 +32,7 @@ import java.time.LocalDateTime
import java.time.LocalTime
import java.time.OffsetDateTime
@ActiveProfiles("test", "test-mysql")
@ActiveProfiles("test", "data")
abstract class AbstractDataInitializer(
val semaphore: Semaphore = Semaphore(permits = 10),
) : FunSpecSpringbootTest(

View File

@ -23,22 +23,26 @@ class TestDatabaseUtil(
}
fun initializeRegion() {
this::class.java.getResource("/schema/region-data.sql")?.readText()?.let {
jdbcTemplate.execute(it)
jdbcTemplate.queryForObject("SELECT EXISTS (SELECT 1 FROM region LIMIT 1)", Boolean::class.java)!!.also { isRegionTableEmpty ->
if (!isRegionTableEmpty) {
this::class.java.getResource("/schema/region-data.sql")?.readText()?.let { regionInsertSql ->
jdbcTemplate.execute(regionInsertSql)
}
}
}
}
fun clear(mode: CleanerMode) {
entityManager.clear()
jdbcTemplate.execute("SET REFERENTIAL_INTEGRITY FALSE")
jdbcTemplate.execute("SET FOREIGN_KEY_CHECKS = 0")
tables.forEach {
if (mode == CleanerMode.EXCEPT_REGION && it == "region") {
return@forEach
}
jdbcTemplate.execute("TRUNCATE TABLE $it RESTART IDENTITY")
jdbcTemplate.execute("TRUNCATE TABLE $it")
}
jdbcTemplate.execute("SET REFERENTIAL_INTEGRITY TRUE")
jdbcTemplate.execute("SET FOREIGN_KEY_CHECKS = 1")
}
}

View File

@ -10,14 +10,14 @@ spring:
ddl-auto: validate
datasource:
hikari:
jdbc-url: jdbc:h2:mem:test
driver-class-name: org.h2.Driver
username: sa
password:
jdbc-url: jdbc:mysql://localhost:23306/roomescape_local
driver-class-name: com.mysql.cj.jdbc.Driver
username: root
password: init
sql:
init:
mode: always
schema-locations: classpath:schema/schema-h2.sql
schema-locations: classpath:schema/schema-mysql.sql
security:
jwt: