Skip to content

Commit

Permalink
feat: Data 모듈을 추가합니다 (#21)
Browse files Browse the repository at this point in the history
* feat: data 모듈 추가

* feat: data 모듈 spring starter 추가

- data-jdbc
- mysql-connector-j

* feat: FLYWAY 버전 변수 추가

* feat: data 모듈 flyway 의존성 추가

* feat: DataSource 빈 추가

* feat: TransactionManager 빈 추가

* feat: FlywayConfig 추가

* feat: data 모듈 구성 설정 추가

* feat: foo 테이블 추가

* remove: 데이터 소스 관련 코드 삭제
  • Loading branch information
belljun3395 authored Jun 8, 2024
1 parent f9aa4bd commit e38e3d4
Show file tree
Hide file tree
Showing 9 changed files with 118 additions and 1 deletion.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ subprojects {
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-reactor")
implementation("io.projectreactor.kotlin:reactor-kotlin-extensions")
implementation("com.fasterxml.jackson.module:jackson-module-kotlin")

/** test **/
testImplementation("org.springframework.boot:spring-boot-starter-test")
testImplementation("io.mockk:mockk:${DependencyVersion.MOCKK}")
Expand Down
3 changes: 3 additions & 0 deletions buildSrc/src/main/kotlin/DependencyVersion.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ object DependencyVersion {
/** springDependencyManagementVersion */
const val SPRING_DEPENDENCY_MANAGEMENT = "1.1.5"

/** flyway */
const val FLYWAY = "9.16.0"

/** test */
const val MOCKK = "1.13.9"
const val KOTEST = "5.8.0"
Expand Down
18 changes: 18 additions & 0 deletions data/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
tasks.getByName("bootJar") {
enabled = false
}

tasks.getByName("jar") {
enabled = true
}

dependencies {
/** spring starter */
implementation("org.springframework.boot:spring-boot-starter-data-jdbc")
implementation("com.mysql:mysql-connector-j")

/** flyway */
implementation("org.flywaydb:flyway-core:${DependencyVersion.FLYWAY}")
implementation("org.flywaydb:flyway-mysql")
}

16 changes: 16 additions & 0 deletions data/src/main/kotlin/com/few/data/config/DataConfig.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.few.data.config

import org.springframework.context.annotation.ComponentScan
import org.springframework.context.annotation.Configuration

@Configuration
@ComponentScan(basePackages = [DataConfig.BASE_PACKAGE])
class DataConfig {
companion object {
const val BASE_PACKAGE = "com.few.data"
const val SERVICE_NAME = "few"
const val MODULE_NAME = "few-data"
const val BEAN_NAME_PREFIX = "fewData"
const val PROPERTY_PREFIX = SERVICE_NAME + "." + MODULE_NAME
}
}
60 changes: 60 additions & 0 deletions data/src/main/kotlin/com/few/data/flyway/FlywayConfig.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package com.few.data.flyway

import com.few.data.config.DataConfig
import org.flywaydb.core.Flyway
import org.flywaydb.core.api.configuration.FluentConfiguration
import org.springframework.boot.autoconfigure.flyway.FlywayMigrationInitializer
import org.springframework.boot.autoconfigure.flyway.FlywayProperties
import org.springframework.boot.context.properties.ConfigurationProperties
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.context.annotation.Profile
import java.util.function.Consumer
import javax.sql.DataSource

@Configuration
class FlywayConfig {
@Bean(name = [DataConfig.BEAN_NAME_PREFIX + "Flyway"])
fun flyway(
configuration: org.flywaydb.core.api.configuration.Configuration?
): Flyway {
return Flyway(configuration)
}

@Profile("!new")
@Bean(name = [DataConfig.BEAN_NAME_PREFIX + "FlywayValidateInitializer"])
fun flywayValidateInitializer(
flyway: Flyway?
): FlywayMigrationInitializer {
return FlywayMigrationInitializer(flyway) { obj: Flyway -> obj.validate() }
}

@Bean(name = [DataConfig.BEAN_NAME_PREFIX + "FlywayMigrationInitializer"])
fun flywayMigrationInitializer(
flyway: Flyway?
): FlywayMigrationInitializer {
return FlywayMigrationInitializer(flyway) { obj: Flyway -> obj.migrate() }
}

@Bean(name = [DataConfig.BEAN_NAME_PREFIX + "FlywayProperties"])
@ConfigurationProperties(prefix = "spring.flyway")
fun flywayProperties(): FlywayProperties {
return FlywayProperties()
}

@Bean(name = [DataConfig.BEAN_NAME_PREFIX + "FlywayConfiguration"])
fun configuration(
dataSource: DataSource?
): org.flywaydb.core.api.configuration.Configuration {
val configuration = FluentConfiguration()
configuration.dataSource(dataSource)
flywayProperties().locations.forEach(
Consumer { locations: String? ->
configuration.locations(
locations
)
}
)
return configuration
}
}
6 changes: 6 additions & 0 deletions data/src/main/resources/application-data-local.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
spring:
flyway:
locations: classpath:db/migration/entity
sql-migration-suffixes: sql
baseline-on-migrate: true
baseline-version: 0
6 changes: 6 additions & 0 deletions data/src/main/resources/application-data-prd.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
spring:
flyway:
locations: classpath:db/migration/entity
sql-migration-suffixes: sql
baseline-on-migrate: true
baseline-version: 0
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
CREATE TABLE foo_tb
(
id BIGINT NOT NULL AUTO_INCREMENT,
name VARCHAR(255) NOT NULL,
deleted BIT NOT NULL,
primary key (id)
);
1 change: 1 addition & 0 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
rootProject.name = "few"

include("api")
include("data")

0 comments on commit e38e3d4

Please sign in to comment.