Skip to content

Commit

Permalink
feat: api-repo 모듈과 Jooq 설정을 추가합니다 (#22)
Browse files Browse the repository at this point in the history
* remove: application-data-*.yml 삭제

* remove: Data 모듈 의존성 삭제

* move: *.sql 위치 변경

* feat: api-repo 모듈 추가

* feat: api-repo 모듈 jdbc, flyway 의존성 추가

* feat: data 모듈의 DataMigration 복사 및 실행 태스크 추가

* feat: api-repo 모듈 구성 설정 추가

* feat: DataSourceConfig 추가

* feat: FlywayConfig 추가

* feat: JOOQ 버전 추가

* feat: api-repo 모듈 jooq 의존성 추가

* feat: api-repo 모듈 jooqCodegen 테스크 정의 및 사전 조건 추가
  • Loading branch information
belljun3395 authored Jun 17, 2024
1 parent cc8b1ff commit a7222fa
Show file tree
Hide file tree
Showing 12 changed files with 219 additions and 27 deletions.
135 changes: 135 additions & 0 deletions api-repo/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
tasks.getByName("bootJar") {
enabled = false
}

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

plugins {
/** jooq */
id("org.jooq.jooq-codegen-gradle") version DependencyVersion.JOOQ
}

sourceSets {
main {
java {
val mainDir = "src/main/kotlin"
val jooqDir = "src/generated"
srcDirs(mainDir, jooqDir)
}
}
}

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")

/** jooq */
implementation("org.springframework.boot:spring-boot-starter-jooq")
implementation("org.jooq:jooq:${DependencyVersion.JOOQ}")
implementation("org.jooq:jooq-meta:${DependencyVersion.JOOQ}")
implementation("org.jooq:jooq-codegen:${DependencyVersion.JOOQ}")
jooqCodegen("org.jooq:jooq-meta-extensions:${DependencyVersion.JOOQ}")
}

/** copy data migration */
tasks.create("copyDataMigration") {
doLast {
val root = rootDir
val flyWayResourceDir = "/db/migration/entity"
val dataMigrationDir = "$root/data/$flyWayResourceDir"
File(dataMigrationDir).walkTopDown().forEach {
if (it.isFile) {
it.copyTo(
File("${project.projectDir}/src/main/resources$flyWayResourceDir/${it.name}"),
true
)
}
}
}
}

/** copy data migration before compile kotlin */
tasks.getByName("compileKotlin") {
dependsOn("copyDataMigration")
}

/** jooq codegen after copy data migration */
tasks.getByName("jooqCodegen") {
dependsOn("copyDataMigration")
}

jooq {
configuration {
generator {
database {
name = "org.jooq.meta.extensions.ddl.DDLDatabase"
properties {
// Specify the location of your SQL script.
// You may use ant-style file matching, e.g. /path/**/to/*.sql
//
// Where:
// - ** matches any directory subtree
// - * matches any number of characters in a directory / file name
// - ? matches a single character in a directory / file name
property {
key = "scripts"
value = "src/main/resources/db/migration/**/*.sql"
}

// The sort order of the scripts within a directory, where:
//
// - semantic: sorts versions, e.g. v-3.10.0 is after v-3.9.0 (default)
// - alphanumeric: sorts strings, e.g. v-3.10.0 is before v-3.9.0
// - flyway: sorts files the same way as flyway does
// - none: doesn't sort directory contents after fetching them from the directory
property {
key = "sort"
value = "flyway"
}

// The default schema for unqualified objects:
//
// - public: all unqualified objects are located in the PUBLIC (upper case) schema
// - none: all unqualified objects are located in the default schema (default)
//
// This configuration can be overridden with the schema mapping feature
property {
key = "unqualifiedSchema"
value = "none"
}

// The default name case for unquoted objects:
//
// - as_is: unquoted object names are kept unquoted
// - upper: unquoted object names are turned into upper case (most databases)
// - lower: unquoted object names are turned into lower case (e.g. PostgreSQL)
property {
key = "defaultNameCase"
value = "as_is"
}
}
}

generate {
isDeprecated = false
isRecords = true
isImmutablePojos = true
isFluentSetters = true
isJavaTimeTypes = true
}

target {
packageName = "jooq.jooq_dsl"
directory = "src/generated"
encoding = "UTF-8"
}
}
}
}
16 changes: 16 additions & 0 deletions api-repo/src/main/kotlin/com/few/api/repo/config/ApiRepoConfig.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.few.api.repo.config

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

@Configuration
@ComponentScan(basePackages = [ApiRepoConfig.BASE_PACKAGE])
class ApiRepoConfig {
companion object {
const val BASE_PACKAGE = "com.few.api.repo"
const val SERVICE_NAME = "few"
const val MODULE_NAME = "few-api-repo"
const val BEAN_NAME_PREFIX = "fewApiRepo"
const val PROPERTY_PREFIX = SERVICE_NAME + "." + MODULE_NAME
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.few.api.repo.datasource

import com.few.api.repo.config.ApiRepoConfig
import org.springframework.boot.context.properties.ConfigurationProperties
import org.springframework.boot.jdbc.DataSourceBuilder
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.jdbc.datasource.DataSourceTransactionManager
import org.springframework.transaction.PlatformTransactionManager
import javax.sql.DataSource

@Configuration
class DataSourceConfig {

companion object {
const val API_DATASOURCE = ApiRepoConfig.BEAN_NAME_PREFIX + "DataSource"
const val API_TX = ApiRepoConfig.BEAN_NAME_PREFIX + "TransactionManager"
}

@Bean(name = [API_DATASOURCE])
@ConfigurationProperties(prefix = "spring.datasource")
fun apiDataSource(): DataSource {
return DataSourceBuilder.create().build()
}

@Bean(name = [API_TX])
fun apiTransactionManager(): PlatformTransactionManager {
return DataSourceTransactionManager(apiDataSource())
}
}
Original file line number Diff line number Diff line change
@@ -1,50 +1,54 @@
package com.few.data.flyway
package com.few.api.repo.flyway

import com.few.data.config.DataConfig
import com.few.api.repo.config.ApiRepoConfig
import com.few.api.repo.datasource.DataSourceConfig
import org.flywaydb.core.Flyway
import org.flywaydb.core.api.configuration.FluentConfiguration
import org.springframework.beans.factory.annotation.Qualifier
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.Import
import org.springframework.context.annotation.Profile
import java.util.function.Consumer
import javax.sql.DataSource

@Configuration
@Import(DataSourceConfig::class)
class FlywayConfig {
@Bean(name = [DataConfig.BEAN_NAME_PREFIX + "Flyway"])
@Bean(name = [ApiRepoConfig.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"])
@Bean(name = [ApiRepoConfig.BEAN_NAME_PREFIX + "FlywayValidateInitializer"])
fun flywayValidateInitializer(
flyway: Flyway?
): FlywayMigrationInitializer {
return FlywayMigrationInitializer(flyway) { obj: Flyway -> obj.validate() }
}

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

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

@Bean(name = [DataConfig.BEAN_NAME_PREFIX + "FlywayConfiguration"])
@Bean(name = [ApiRepoConfig.BEAN_NAME_PREFIX + "FlywayConfiguration"])
fun configuration(
dataSource: DataSource?
@Qualifier(DataSourceConfig.API_DATASOURCE) dataSource: DataSource?
): org.flywaydb.core.api.configuration.Configuration {
val configuration = FluentConfiguration()
configuration.dataSource(dataSource)
Expand Down
11 changes: 11 additions & 0 deletions api-repo/src/main/resources/application-api-repo-local.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
spring:
datasource:
jdbcUrl: jdbc:mysql://localhost:13306/api?allowPublicKeyRetrieval=true&rewriteBatchedStatements=true
username: root
password: root
driver-class-name: com.mysql.cj.jdbc.Driver
flyway:
locations: classpath:db/migration/entity
sql-migration-suffixes: sql
baseline-on-migrate: true
baseline-version: 0
11 changes: 11 additions & 0 deletions api-repo/src/main/resources/application-api-repo-prd.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
spring:
datasource:
jdbcUrl: ${DB_HOSTNAME}/api?allowPublicKeyRetrieval=true&rewriteBatchedStatements=true
username: ${DB_USERNAME}
password: ${DB_PASSWORD}
driver-class-name: com.mysql.cj.jdbc.Driver
flyway:
locations: classpath:db/migration/entity
sql-migration-suffixes: sql
baseline-on-migrate: true
baseline-version: 0
3 changes: 3 additions & 0 deletions buildSrc/src/main/kotlin/DependencyVersion.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ object DependencyVersion {
/** flyway */
const val FLYWAY = "9.16.0"

/** jooq */
const val JOOQ = "3.19.9"

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

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")
}
6 changes: 0 additions & 6 deletions data/src/main/resources/application-data-local.yml

This file was deleted.

6 changes: 0 additions & 6 deletions data/src/main/resources/application-data-prd.yml

This file was deleted.

1 change: 1 addition & 0 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ rootProject.name = "few"

include("api")
include("data")
include("api-repo")

0 comments on commit a7222fa

Please sign in to comment.