feat: Exposedで初期化を自動でするように

This commit is contained in:
usbharu 2023-09-30 13:41:42 +09:00
parent 36ad81ddd1
commit 0e5d7c92f6
9 changed files with 10 additions and 77 deletions

View File

@ -1,9 +1,7 @@
package dev.usbharu.hideout.config
import org.jetbrains.exposed.sql.Database
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.context.properties.ConfigurationProperties
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import java.net.URL
@ -15,16 +13,6 @@ class SpringConfig {
@Autowired
lateinit var config: ApplicationConfig
@Bean
fun database(): Database {
return Database.connect(
url = dbConfig.url,
driver = dbConfig.driver,
user = dbConfig.user,
password = dbConfig.password
)
}
}
@ConfigurationProperties("hideout")

View File

@ -1,20 +1,15 @@
package dev.usbharu.hideout.repository
import dev.usbharu.hideout.domain.model.hideout.entity.Jwt
import org.jetbrains.exposed.sql.*
import org.jetbrains.exposed.sql.transactions.transaction
import org.jetbrains.exposed.sql.Table
import org.jetbrains.exposed.sql.insert
import org.jetbrains.exposed.sql.select
import org.jetbrains.exposed.sql.update
import org.springframework.stereotype.Repository
import java.util.*
@Repository
class MetaRepositoryImpl(private val database: Database) : MetaRepository {
init {
transaction(database) {
SchemaUtils.create(Meta)
SchemaUtils.createMissingTablesAndColumns(Meta)
}
}
class MetaRepositoryImpl : MetaRepository {
override suspend fun save(meta: dev.usbharu.hideout.domain.model.hideout.entity.Meta) {
if (Meta.select { Meta.id eq 1 }.empty()) {

View File

@ -6,18 +6,10 @@ import dev.usbharu.hideout.exception.FailedToGetResourcesException
import dev.usbharu.hideout.service.core.IdGenerateService
import org.jetbrains.exposed.sql.*
import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq
import org.jetbrains.exposed.sql.transactions.transaction
import org.springframework.stereotype.Repository
@Repository
class PostRepositoryImpl(database: Database, private val idGenerateService: IdGenerateService) : PostRepository {
init {
transaction(database) {
SchemaUtils.create(Posts)
SchemaUtils.createMissingTablesAndColumns(Posts)
}
}
class PostRepositoryImpl(private val idGenerateService: IdGenerateService) : PostRepository {
override suspend fun generateId(): Long = idGenerateService.generateId()

View File

@ -5,21 +5,13 @@ import dev.usbharu.hideout.service.core.IdGenerateService
import org.jetbrains.exposed.dao.id.LongIdTable
import org.jetbrains.exposed.sql.*
import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq
import org.jetbrains.exposed.sql.transactions.transaction
import org.springframework.stereotype.Repository
@Repository
class ReactionRepositoryImpl(
private val database: Database,
private val idGenerateService: IdGenerateService
) : ReactionRepository {
init {
transaction(database) {
SchemaUtils.create(Reactions)
SchemaUtils.createMissingTablesAndColumns(Reactions)
}
}
override suspend fun generateId(): Long = idGenerateService.generateId()

View File

@ -10,7 +10,6 @@ import dev.usbharu.hideout.service.auth.ExposedOAuth2AuthorizationService
import org.jetbrains.exposed.sql.*
import org.jetbrains.exposed.sql.javatime.CurrentTimestamp
import org.jetbrains.exposed.sql.javatime.timestamp
import org.jetbrains.exposed.sql.transactions.transaction
import org.slf4j.LoggerFactory
import org.springframework.security.jackson2.SecurityJackson2Modules
import org.springframework.security.oauth2.core.AuthorizationGrantType
@ -27,14 +26,8 @@ import java.time.Instant
import org.springframework.security.oauth2.server.authorization.client.RegisteredClient as SpringRegisteredClient
@Repository
class RegisteredClientRepositoryImpl(private val database: Database) : RegisteredClientRepository {
class RegisteredClientRepositoryImpl : RegisteredClientRepository {
init {
transaction(database) {
SchemaUtils.create(RegisteredClient)
SchemaUtils.createMissingTablesAndColumns(RegisteredClient)
}
}
override fun save(registeredClient: SpringRegisteredClient?) {
requireNotNull(registeredClient)

View File

@ -6,23 +6,12 @@ import dev.usbharu.hideout.service.core.IdGenerateService
import org.jetbrains.exposed.dao.id.LongIdTable
import org.jetbrains.exposed.sql.*
import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq
import org.jetbrains.exposed.sql.transactions.transaction
import org.springframework.stereotype.Repository
import java.time.Instant
@Repository
class UserRepositoryImpl(private val database: Database, private val idGenerateService: IdGenerateService) :
class UserRepositoryImpl(private val idGenerateService: IdGenerateService) :
UserRepository {
init {
transaction(database) {
SchemaUtils.create(Users)
SchemaUtils.create(UsersFollowers)
SchemaUtils.createMissingTablesAndColumns(Users)
SchemaUtils.createMissingTablesAndColumns(UsersFollowers)
SchemaUtils.create(FollowRequests)
SchemaUtils.createMissingTablesAndColumns(FollowRequests)
}
}
override suspend fun save(user: User): User {
val singleOrNull = Users.select { Users.id eq user.id }.singleOrNull()

View File

@ -4,7 +4,6 @@ import dev.usbharu.hideout.service.core.Transaction
import kotlinx.coroutines.runBlocking
import org.jetbrains.exposed.sql.*
import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq
import org.jetbrains.exposed.sql.transactions.transaction
import org.springframework.security.core.authority.SimpleGrantedAuthority
import org.springframework.security.oauth2.server.authorization.OAuth2AuthorizationConsentService
import org.springframework.security.oauth2.server.authorization.client.RegisteredClientRepository
@ -15,17 +14,9 @@ import org.springframework.security.oauth2.server.authorization.OAuth2Authorizat
class ExposedOAuth2AuthorizationConsentService(
private val registeredClientRepository: RegisteredClientRepository,
private val transaction: Transaction,
private val database: Database
) :
OAuth2AuthorizationConsentService {
init {
transaction(database) {
SchemaUtils.create(OAuth2AuthorizationConsent)
SchemaUtils.createMissingTablesAndColumns(OAuth2AuthorizationConsent)
}
}
override fun save(authorizationConsent: AuthorizationConsent?) = runBlocking {
requireNotNull(authorizationConsent)
transaction.transaction {

View File

@ -10,7 +10,6 @@ import kotlinx.coroutines.runBlocking
import org.jetbrains.exposed.sql.*
import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq
import org.jetbrains.exposed.sql.javatime.timestamp
import org.jetbrains.exposed.sql.transactions.transaction
import org.springframework.security.jackson2.CoreJackson2Module
import org.springframework.security.jackson2.SecurityJackson2Modules
import org.springframework.security.oauth2.core.*
@ -29,16 +28,9 @@ import org.springframework.stereotype.Service
class ExposedOAuth2AuthorizationService(
private val registeredClientRepository: RegisteredClientRepository,
private val transaction: Transaction,
private val database: Database
) :
OAuth2AuthorizationService {
init {
transaction(database) {
SchemaUtils.create(Authorization)
SchemaUtils.createMissingTablesAndColumns(Authorization)
}
}
@Suppress("LongMethod", "CyclomaticComplexMethod")
override fun save(authorization: OAuth2Authorization?): Unit = runBlocking {

View File

@ -33,8 +33,9 @@ spring:
h2:
console:
enabled: true
exposed:
generate-ddl: true
server:
tomcat:
basedir: tomcat
accesslog: