Skip to content

Commit

Permalink
h2 -> mysql
Browse files Browse the repository at this point in the history
  • Loading branch information
jinia91 committed Aug 19, 2024
1 parent 80dcd1b commit c8afca1
Show file tree
Hide file tree
Showing 21 changed files with 72 additions and 37 deletions.
4 changes: 2 additions & 2 deletions buildSrc/src/main/kotlin/Modules.kt
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ object Modules {
InWebsocket(":service:blog:adapter:blog-in-websocket"),
OutUser(":service:blog:adapter:blog-out-user"),
OutMemo(":service:blog:adapter:blog-out-memo"),
OutPersistence(":service:blog:adapter:blog-out-persistence")
OutMySql(":service:blog:adapter:blog-out-mysql")
}
}

Expand All @@ -48,7 +48,7 @@ object Modules {
InAcl(":service:user-auth:adapter:user-auth-in-acl"),
InHttp(":service:user-auth:adapter:user-auth-in-http"),
OutGoogle(":service:user-auth:adapter:user-auth-out-google"),
OutH2(":service:user-auth:adapter:user-auth-out-h2"),
OutMySql(":service:user-auth:adapter:user-auth-out-mysql"),
OutCache(":service:user-auth:adapter:user-auth-out-cache")
}
}
Expand Down
6 changes: 5 additions & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ spring-dependency-management = "1.1.5"

# db
h2 = "2.2.224"
mysql-connector-j = "8.3.0"

# testcontainers
testcontainers = "1.19.8"
Expand Down Expand Up @@ -48,8 +49,10 @@ springdoc-openapi-ui = { module = "org.springdoc:springdoc-openapi-starter-webmv
# spring spy
spring-boot-starter-spy = { module = "com.github.gavlyukovskiy:p6spy-spring-boot-starter", version = "1.9.0" }

# h2 의존성
# db 의존성
h2 = { module = "com.h2database:h2", version.ref = "h2" }
mysql-connector-j = { module = "com.mysql:mysql-connector-j", version.ref = "mysql-connector-j" }


# logback
logback-classic = { module = "ch.qos.logback:logback-classic", version = "1.4.12" }
Expand All @@ -63,6 +66,7 @@ mockk = { module = "io.mockk:mockk", version.ref = "mockk" }
testcontainers = { module = "org.testcontainers:testcontainers", version.ref = "testcontainers" }
testcontainers-junit5 = { module = "org.testcontainers:junit-jupiter", version.ref = "testcontainers"}
testcontainers-neo4j = { module = "org.testcontainers:neo4j", version.ref = "testcontainers" }
testcontainers-mysql = { module = "org.testcontainers:mysql", version.ref = "testcontainers" }
restassured = {module = "io.rest-assured:spring-mock-mvc" , version = "5.4.0"}
wiremock = {module = "org.springframework.cloud:spring-cloud-contract-wiremock", version = "4.1.0"}
mockkbean = {module = "com.ninja-squad:springmockk", version = "4.0.2"}
Expand Down
1 change: 1 addition & 0 deletions libs/rdb-kernel/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ dependencies {
api(project(Modules.Libs.CoreKernel.path))
api(libs.spring.boot.starter.data.jpa)
implementation(libs.spring.boot.starter.spy)
implementation(libs.mysql.connector.j)
implementation("com.querydsl:querydsl-jpa:5.1.0:jakarta")
kapt("com.querydsl:querydsl-apt:5.1.0:jakarta")
}
8 changes: 5 additions & 3 deletions mains/monolith-main/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ val blogService = mutableListOf(
project(Modules.Service.Blog.Adaptors.InWebsocket.path),
project(Modules.Service.Blog.Adaptors.OutUser.path),
project(Modules.Service.Blog.Adaptors.OutMemo.path),
project(Modules.Service.Blog.Adaptors.OutPersistence.path),
project(Modules.Service.Blog.Adaptors.OutMySql.path),
)

val memoService = mutableListOf(
Expand All @@ -44,7 +44,7 @@ val authUserService = mutableListOf(
project(Modules.Service.AuthUser.Adaptors.OutGoogle.path),
project(Modules.Service.AuthUser.Adaptors.InHttp.path),
project(Modules.Service.AuthUser.Adaptors.OutCache.path),
project(Modules.Service.AuthUser.Adaptors.OutH2.path),
project(Modules.Service.AuthUser.Adaptors.OutMySql.path),
)

val admin = mutableListOf(
Expand All @@ -67,13 +67,15 @@ val bootLib = mutableListOf(
libs.spring.boot.starter.web,
libs.spring.boot.starter.data.neo4j,
libs.spring.boot.starter.actuator,
libs.h2,
libs.mysql.connector.j,
libs.h2
)

var integrationTestLib = mutableListOf(
libs.testcontainers,
libs.testcontainers.junit5,
libs.testcontainers.neo4j,
libs.testcontainers.mysql,
libs.restassured,
libs.wiremock,
libs.mockkbean,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package kr.co.jiniaslog
import io.mockk.mockk
import io.restassured.RestAssured
import kr.co.jiniaslog.media.outbound.ImageUploader
import kr.co.jiniaslog.utils.H2RdbCleaner
import kr.co.jiniaslog.utils.MySqlRdbCleaner
import kr.co.jiniaslog.utils.Neo4jDbCleaner
import org.junit.jupiter.api.AfterEach
import org.junit.jupiter.api.BeforeEach
Expand All @@ -18,6 +18,7 @@ import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Primary
import org.springframework.test.context.DynamicPropertyRegistry
import org.springframework.test.context.DynamicPropertySource
import org.testcontainers.containers.MySQLContainer
import org.testcontainers.containers.Neo4jContainer
import org.testcontainers.junit.jupiter.Testcontainers

Expand Down Expand Up @@ -55,7 +56,7 @@ abstract class TestContainerAbstractSkeleton {
protected lateinit var neo4jDbCleaner: Neo4jDbCleaner

@Autowired
protected lateinit var h2RdbCleaner: H2RdbCleaner
protected lateinit var mySqlRdbCleaner: MySqlRdbCleaner

@BeforeEach
fun setUp() {
Expand All @@ -65,19 +66,38 @@ abstract class TestContainerAbstractSkeleton {
@AfterEach
fun tearDown() {
neo4jDbCleaner.tearDownAll()
h2RdbCleaner.tearDownAll()
mySqlRdbCleaner.tearDownAll()
}

@Test
fun contextLoads() {
}

companion object {
private const val RDB_CHARSET = "--character-set-server=utf8mb4"
private const val RDB_COLLATION = "--collation-server=utf8mb4_unicode_ci"
private const val RDB_INIT_SQL = "SET NAMES utf8mb4 COLLATE utf8mb4_unicode_ci"

@JvmStatic
val neo4j: Neo4jContainer<*> = CustomNeo4jContainer()

@JvmStatic
val userDb: MySQLContainer<*> = MySQLContainer("mysql:8.0")
.withCommand(RDB_CHARSET, RDB_COLLATION)
.withDatabaseName("jiniaslog_user")
.withReuse(true)

@JvmStatic
val blogDb: MySQLContainer<*> =
MySQLContainer("mysql:8.0")
.withCommand(RDB_CHARSET, RDB_COLLATION)
.withDatabaseName("jiniaslog_blog")
.withReuse(true)

init {
neo4j.start()
userDb.start()
blogDb.start()
}

@DynamicPropertySource
Expand All @@ -87,6 +107,20 @@ abstract class TestContainerAbstractSkeleton {
registry.add("spring.neo4j.uri") { neo4j.boltUrl }
registry.add("spring.neo4j.authentication.username") { "neo4j" }
registry.add("spring.neo4j.authentication.password") { "password" }

// user db
registry.add("spring.datasource.user.jdbc-url") { userDb.jdbcUrl }
registry.add("spring.datasource.user.username") { userDb.username }
registry.add("spring.datasource.user.password") { userDb.password }
registry.add("spring.datasource.user.driver-class-name") { userDb.driverClassName }
registry.add("spring.datasource.user.connection-init-sql") { RDB_INIT_SQL }

// blog db
registry.add("spring.datasource.blog.jdbc-url") { blogDb.jdbcUrl }
registry.add("spring.datasource.blog.username") { blogDb.username }
registry.add("spring.datasource.blog.password") { blogDb.password }
registry.add("spring.datasource.blog.driver-class-name") { blogDb.driverClassName }
registry.add("spring.datasource.blog.connection-init-sql") { RDB_INIT_SQL }
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import java.sql.Connection
import javax.sql.DataSource

@Component
class H2RdbCleaner(private val dataSources: List<DataSource>) : DbCleaner {
class MySqlRdbCleaner(private val dataSources: List<DataSource>) : DbCleaner {
override fun tearDownAll() {
dataSources.forEach { dataSource ->
clean(dataSource)
Expand All @@ -19,34 +19,30 @@ class H2RdbCleaner(private val dataSources: List<DataSource>) : DbCleaner {
var connection: Connection? = null
try {
connection = DataSourceUtils.getConnection(jdbcTemplate.dataSource!!)

jdbcTemplate.execute(NO_LOG + FOREIGN_KEY_FALSE)
val schema: String = connection.catalog

val tableNames: List<String> =
jdbcTemplate.query(
NO_LOG + QUERY_TABLE_NAMES,
arrayOf(H2_SCHEMA),
arrayOf(schema),
) { resultSet, _ ->
resultSet.getString(TABLE_NAME)
}
jdbcTemplate.execute(NO_LOG + "SET FOREIGN_KEY_CHECKS=0;")
tableNames.forEach { tableName ->
jdbcTemplate.execute(String.format(NO_LOG + DELETE_ALL_TABLES, tableName))
jdbcTemplate.execute(NO_LOG + "TRUNCATE TABLE $tableName")
}
jdbcTemplate.execute(NO_LOG + "SET FOREIGN_KEY_CHECKS=1;")
} finally {
if (connection != null) {
jdbcTemplate.execute(NO_LOG + FOREIGN_KEY_TRUE)
DataSourceUtils.releaseConnection(connection, jdbcTemplate.dataSource!!)
}
}
}

companion object {
private const val NO_LOG = "-- NO_LOG \n"
private const val FOREIGN_KEY_FALSE = "SET REFERENTIAL_INTEGRITY FALSE;"
private const val FOREIGN_KEY_TRUE = "SET REFERENTIAL_INTEGRITY TRUE;"
private const val QUERY_TABLE_NAMES = "SELECT table_name FROM information_schema.tables WHERE table_schema = ?"
private const val H2_SCHEMA = "PUBLIC"
private const val TABLE_NAME = "table_name"
private const val DELETE_ALL_TABLES = "DELETE FROM %s"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ dependencies {
implementation(project(Modules.Libs.RdbKernel.path))
implementation(project(Modules.Service.Blog.Core.path))
implementation(libs.spring.boot.starter.data.jpa)
implementation(libs.h2)
implementation(libs.spring.boot.starter.validation)
implementation("com.querydsl:querydsl-jpa:5.1.0:jakarta")
kapt("com.querydsl:querydsl-apt:5.1.0:jakarta")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package kr.co.jiniaslog.blog.adapter.outbound.persistence
package kr.co.jiniaslog.blog.adapter.outbound.mysql

import kr.co.jiniaslog.blog.domain.article.Article
import kr.co.jiniaslog.blog.domain.article.ArticleId
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package kr.co.jiniaslog.blog.adapter.outbound.persistence
package kr.co.jiniaslog.blog.adapter.outbound.mysql

import kr.co.jiniaslog.blog.outbound.BlogTransactionHandler
import org.springframework.stereotype.Component
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package kr.co.jiniaslog.blog.adapter.outbound.persistence
package kr.co.jiniaslog.blog.adapter.outbound.mysql

import kr.co.jiniaslog.blog.domain.category.Category
import kr.co.jiniaslog.blog.domain.category.CategoryId
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package kr.co.jiniaslog.blog.adapter.outbound.persistence
package kr.co.jiniaslog.blog.adapter.outbound.mysql

import com.querydsl.jpa.JPAExpressions
import com.querydsl.jpa.impl.JPAQueryFactory
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package kr.co.jiniaslog.blog.adapter.outbound.persistence.config
package kr.co.jiniaslog.blog.adapter.outbound.mysql.config

import jakarta.persistence.EntityManagerFactory
import kr.co.jiniaslog.shared.adapter.out.rdb.JpaAutoDdlProperty
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package kr.co.jiniaslog.blog.adapter.outbound.persistence.config
package kr.co.jiniaslog.blog.adapter.outbound.mysql.config

import com.querydsl.jpa.impl.JPAQueryFactory
import jakarta.persistence.EntityManager
Expand Down
1 change: 0 additions & 1 deletion service/blog/blog-core/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ dependencies {
implementation(project(Modules.Libs.RdbKernel.path))
implementation(libs.spring.boot.starter.data.jpa)
implementation(libs.spring.boot.starter.core)
implementation(libs.h2)
implementation(libs.spring.boot.starter.validation)

implementation("com.querydsl:querydsl-jpa:5.1.0:jakarta")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ dependencies {
implementation(project(Modules.Service.AuthUser.Application.path))
implementation(project(Modules.Libs.CoreKernel.path))
implementation(project(Modules.Libs.RdbKernel.path))
api(libs.spring.boot.starter.data.jpa)
implementation(libs.h2)
implementation(libs.spring.boot.starter.spy)
implementation(libs.mysql.connector.j)
api(libs.spring.boot.starter.data.jpa)
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package kr.co.jiniaslog.user.adapter.out.h2
package kr.co.jiniaslog.user.adapter.out.mysql

import jakarta.persistence.EntityManagerFactory
import kr.co.jiniaslog.shared.adapter.out.rdb.JpaAutoDdlProperty
Expand All @@ -16,7 +16,7 @@ import org.springframework.transaction.annotation.EnableTransactionManagement
import javax.sql.DataSource

object UserDb {
const val BASE_PACKAGE = "kr.co.jiniaslog.user.adapter.out.h2"
const val BASE_PACKAGE = "kr.co.jiniaslog.user.adapter.out.mysql"
const val DATASOURCE_PREFIX = "spring.datasource.user"
const val ENTITY_MANAGER_FACTORY = "userEntityManagerFactory"
const val DATASOURCE = "userDatasource"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package kr.co.jiniaslog.user.adapter.out.h2
package kr.co.jiniaslog.user.adapter.out.mysql

import kr.co.jiniaslog.user.application.infra.UserAuthTransactionHandler
import org.springframework.stereotype.Component
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package kr.co.jiniaslog.user.adapter.out.h2.user
package kr.co.jiniaslog.user.adapter.out.mysql.user

import jakarta.persistence.Column
import jakarta.persistence.Entity
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package kr.co.jiniaslog.user.adapter.out.h2.user
package kr.co.jiniaslog.user.adapter.out.mysql.user

import org.springframework.data.jpa.repository.JpaRepository

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package kr.co.jiniaslog.user.adapter.out.h2.user
package kr.co.jiniaslog.user.adapter.out.mysql.user

import kr.co.jiniaslog.shared.core.annotation.PersistenceAdapter
import kr.co.jiniaslog.user.application.infra.UserRepository
Expand Down
4 changes: 2 additions & 2 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ include(
"service:blog:adapter:blog-in-websocket",
"service:blog:adapter:blog-out-user",
"service:blog:adapter:blog-out-memo",
"service:blog:adapter:blog-out-persistence",
"service:blog:adapter:blog-out-mysql",

// memo
"service:memo:memo-domain",
Expand All @@ -52,7 +52,7 @@ include(
"service:user-auth:adapter:user-auth-in-http",
"service:user-auth:adapter:user-auth-in-acl",
"service:user-auth:adapter:user-auth-out-google",
"service:user-auth:adapter:user-auth-out-h2",
"service:user-auth:adapter:user-auth-out-mysql",
"service:user-auth:adapter:user-auth-out-cache",

// admin
Expand Down

0 comments on commit c8afca1

Please sign in to comment.