From a7222fa90d7417227886a49ec7c9bbb2cc84aac9 Mon Sep 17 00:00:00 2001 From: belljun3395 <195850@jnu.ac.kr> Date: Mon, 17 Jun 2024 20:14:12 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20api-repo=20=EB=AA=A8=EB=93=88=EA=B3=BC?= =?UTF-8?q?=20Jooq=20=EC=84=A4=EC=A0=95=EC=9D=84=20=EC=B6=94=EA=B0=80?= =?UTF-8?q?=ED=95=A9=EB=8B=88=EB=8B=A4=20(#22)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 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 테스크 정의 및 사전 조건 추가 --- api-repo/build.gradle.kts | 135 ++++++++++++++++++ .../com/few/api/repo/config/ApiRepoConfig.kt | 16 +++ .../api/repo/datasource/DataSourceConfig.kt | 30 ++++ .../com/few/api/repo}/flyway/FlywayConfig.kt | 20 +-- .../resources/application-api-repo-local.yml | 11 ++ .../resources/application-api-repo-prd.yml | 11 ++ buildSrc/src/main/kotlin/DependencyVersion.kt | 3 + data/build.gradle.kts | 7 - .../entity/V1.00.0.0__init_foo_table.sql | 0 .../main/resources/application-data-local.yml | 6 - .../main/resources/application-data-prd.yml | 6 - settings.gradle.kts | 1 + 12 files changed, 219 insertions(+), 27 deletions(-) create mode 100644 api-repo/build.gradle.kts create mode 100644 api-repo/src/main/kotlin/com/few/api/repo/config/ApiRepoConfig.kt create mode 100644 api-repo/src/main/kotlin/com/few/api/repo/datasource/DataSourceConfig.kt rename {data/src/main/kotlin/com/few/data => api-repo/src/main/kotlin/com/few/api/repo}/flyway/FlywayConfig.kt (69%) create mode 100644 api-repo/src/main/resources/application-api-repo-local.yml create mode 100644 api-repo/src/main/resources/application-api-repo-prd.yml rename data/{src/main/resources => }/db/migration/entity/V1.00.0.0__init_foo_table.sql (100%) delete mode 100644 data/src/main/resources/application-data-local.yml delete mode 100644 data/src/main/resources/application-data-prd.yml diff --git a/api-repo/build.gradle.kts b/api-repo/build.gradle.kts new file mode 100644 index 000000000..b978aab7d --- /dev/null +++ b/api-repo/build.gradle.kts @@ -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" + } + } + } +} diff --git a/api-repo/src/main/kotlin/com/few/api/repo/config/ApiRepoConfig.kt b/api-repo/src/main/kotlin/com/few/api/repo/config/ApiRepoConfig.kt new file mode 100644 index 000000000..256fdfda9 --- /dev/null +++ b/api-repo/src/main/kotlin/com/few/api/repo/config/ApiRepoConfig.kt @@ -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 + } +} diff --git a/api-repo/src/main/kotlin/com/few/api/repo/datasource/DataSourceConfig.kt b/api-repo/src/main/kotlin/com/few/api/repo/datasource/DataSourceConfig.kt new file mode 100644 index 000000000..caaf69d1f --- /dev/null +++ b/api-repo/src/main/kotlin/com/few/api/repo/datasource/DataSourceConfig.kt @@ -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()) + } +} diff --git a/data/src/main/kotlin/com/few/data/flyway/FlywayConfig.kt b/api-repo/src/main/kotlin/com/few/api/repo/flyway/FlywayConfig.kt similarity index 69% rename from data/src/main/kotlin/com/few/data/flyway/FlywayConfig.kt rename to api-repo/src/main/kotlin/com/few/api/repo/flyway/FlywayConfig.kt index 130552ef9..9444a50f5 100644 --- a/data/src/main/kotlin/com/few/data/flyway/FlywayConfig.kt +++ b/api-repo/src/main/kotlin/com/few/api/repo/flyway/FlywayConfig.kt @@ -1,20 +1,24 @@ -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 { @@ -22,29 +26,29 @@ class FlywayConfig { } @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) diff --git a/api-repo/src/main/resources/application-api-repo-local.yml b/api-repo/src/main/resources/application-api-repo-local.yml new file mode 100644 index 000000000..7b3ab4d7f --- /dev/null +++ b/api-repo/src/main/resources/application-api-repo-local.yml @@ -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 \ No newline at end of file diff --git a/api-repo/src/main/resources/application-api-repo-prd.yml b/api-repo/src/main/resources/application-api-repo-prd.yml new file mode 100644 index 000000000..863d16476 --- /dev/null +++ b/api-repo/src/main/resources/application-api-repo-prd.yml @@ -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 \ No newline at end of file diff --git a/buildSrc/src/main/kotlin/DependencyVersion.kt b/buildSrc/src/main/kotlin/DependencyVersion.kt index e70502c52..22db90ed3 100644 --- a/buildSrc/src/main/kotlin/DependencyVersion.kt +++ b/buildSrc/src/main/kotlin/DependencyVersion.kt @@ -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" diff --git a/data/build.gradle.kts b/data/build.gradle.kts index 9c884063a..7b4ea0e00 100644 --- a/data/build.gradle.kts +++ b/data/build.gradle.kts @@ -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") } diff --git a/data/src/main/resources/db/migration/entity/V1.00.0.0__init_foo_table.sql b/data/db/migration/entity/V1.00.0.0__init_foo_table.sql similarity index 100% rename from data/src/main/resources/db/migration/entity/V1.00.0.0__init_foo_table.sql rename to data/db/migration/entity/V1.00.0.0__init_foo_table.sql diff --git a/data/src/main/resources/application-data-local.yml b/data/src/main/resources/application-data-local.yml deleted file mode 100644 index a065394e8..000000000 --- a/data/src/main/resources/application-data-local.yml +++ /dev/null @@ -1,6 +0,0 @@ -spring: - flyway: - locations: classpath:db/migration/entity - sql-migration-suffixes: sql - baseline-on-migrate: true - baseline-version: 0 \ No newline at end of file diff --git a/data/src/main/resources/application-data-prd.yml b/data/src/main/resources/application-data-prd.yml deleted file mode 100644 index a065394e8..000000000 --- a/data/src/main/resources/application-data-prd.yml +++ /dev/null @@ -1,6 +0,0 @@ -spring: - flyway: - locations: classpath:db/migration/entity - sql-migration-suffixes: sql - baseline-on-migrate: true - baseline-version: 0 \ No newline at end of file diff --git a/settings.gradle.kts b/settings.gradle.kts index 10c3f614a..63c4ed949 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -2,3 +2,4 @@ rootProject.name = "few" include("api") include("data") +include("api-repo")