Skip to content
This repository has been archived by the owner on Jan 9, 2024. It is now read-only.

Commit

Permalink
feat: sqlite db-migration
Browse files Browse the repository at this point in the history
refactor: db related services
  • Loading branch information
mikeplotean committed Oct 17, 2023
1 parent d7f78be commit a980a97
Show file tree
Hide file tree
Showing 38 changed files with 945 additions and 261 deletions.
13 changes: 10 additions & 3 deletions k8s/deployment-portal.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ spec:
imagePullPolicy: Always
env:
- name: NUXT_PUBLIC_ISSUER_URL
value: "https://issuer.walt.id"
value: "https://portal.walt.id"
ports:
- containerPort: 3000
name: http-api
Expand Down Expand Up @@ -244,11 +244,18 @@ spec:
service:
name: wallet-backend
port:
number: 80
name: http
- path: /swagger
pathType: Prefix
backend:
service:
name: wallet-backend
port:
name: http
- path: /
pathType: Prefix
backend:
service:
name: wallet-frontend
port:
number: 80
name: http
1 change: 0 additions & 1 deletion src/main/kotlin/id/walt/Application.kt
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ suspend fun main(args: Array<String>) {
ConfigManager.loadConfigs(args)

Db.start()
Db.init()

// val webConfig = ConfigManager.getConfig<WebConfig>()
// log.info { "Starting web server (binding to ${webConfig.webHost}, listening on port ${webConfig.webPort})..." }
Expand Down
3 changes: 1 addition & 2 deletions src/main/kotlin/id/walt/config/DatabaseConfiguration.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ package id.walt.config
import com.zaxxer.hikari.HikariDataSource

data class DatabaseConfiguration(
val database: String,
val recreate_schema: Boolean = false
val database: String
) : WalletConfig

data class DatasourceConfiguration(
Expand Down
91 changes: 51 additions & 40 deletions src/main/kotlin/id/walt/db/Db.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,18 @@ import id.walt.config.ConfigManager
import id.walt.config.DatabaseConfiguration
import id.walt.config.DatasourceConfiguration
import id.walt.db.models.*
import id.walt.db.repositories.DbCredential
import id.walt.db.repositories.DbDid
import id.walt.db.repositories.DbKey
import id.walt.service.Did
import id.walt.service.account.AccountsService
import id.walt.service.credentials.CredentialsService
import id.walt.service.dids.DidInsertDataObject
import id.walt.service.dids.DidsService
import id.walt.service.keys.KeysService
import id.walt.web.model.EmailLoginRequest
import io.github.oshai.kotlinlogging.KotlinLogging
import org.flywaydb.core.Flyway
import org.jetbrains.exposed.sql.Database
import org.jetbrains.exposed.sql.SchemaUtils
import org.jetbrains.exposed.sql.StdOutSqlLogger
Expand All @@ -15,6 +24,7 @@ import org.jetbrains.exposed.sql.transactions.TransactionManager
import org.jetbrains.exposed.sql.transactions.transaction
import org.slf4j.bridge.SLF4JBridgeHandler
import java.sql.Connection
import java.util.*

object Db {

Expand All @@ -25,11 +35,11 @@ object Db {
val databaseConfig = ConfigManager.getConfig<DatabaseConfiguration>()

//migrate
/*Flyway.configure()
Flyway.configure()
.locations(databaseConfig.database.replace(".", "/"))
.dataSource(datasourceConfig.hikariDataSource)
.load()
.migrate()*/
.migrate()

// connect
log.info { "Connecting to database at \"${datasourceConfig.hikariDataSource.jdbcUrl}\"..." }
Expand Down Expand Up @@ -57,42 +67,43 @@ object Db {
"TRANSACTION_SERIALIZABLE" -> Connection.TRANSACTION_SERIALIZABLE
else -> Connection.TRANSACTION_SERIALIZABLE
}

suspend fun init() {
val databaseConfig = ConfigManager.getConfig<DatabaseConfiguration>()
transaction {
if (databaseConfig.recreate_schema) {
println("DROP SCHEMA")
SchemaUtils.drop(
WalletOperationHistories,
WalletKeys,
WalletDids,
WalletCredentials,
AccountWallets,
Accounts,
Emails,
Wallets
)
}
println("CREATE SCHEMA IF NOT EXISTING")
SchemaUtils.create(
Wallets,
Emails,
Accounts,
AccountWallets,
WalletCredentials,
WalletDids,
WalletKeys,
WalletOperationHistories
)
}

if (databaseConfig.recreate_schema) {
val accountId = AccountsService.register(EmailLoginRequest("[email protected]", "password")).getOrThrow().id
println("CREATED ACCOUNT: $accountId")
}
/** moved to [AccountsService.register] **/
// val did = WalletServiceManager.getWalletService(accountId).createDid("key")
// println("CREATED DID: $did")
}
}
fun main(){
ConfigManager.loadConfigs(emptyArray())
Db.start()
// val account = AccountsService.register(EmailLoginRequest("username", "password")).getOrThrow().id
val account = UUID.fromString("04e595ac-7c48-4482-9ea6-8ae0981251c8")
println(account)
val key = KeysService.add(account, DbKey(keyId = "keyId", document = "document"))
println(key)
val did = DidsService.add(
account, DidInsertDataObject(
key = key,
did = Did(did = "did", document = "document")
)
)
println(did)
val cid = CredentialsService.add(account, DbCredential(
credentialId = "credentialId",
document = "document"
))
println(cid)
// ConfigManager.loadConfigs(emptyArray())
// val datasourceConfig = ConfigManager.getConfig<DatasourceConfiguration>()
// Database.connect(datasourceConfig.hikariDataSource)
// transaction {
// SchemaUtils.create(
// Accounts,
// Emails,
// Wallets,
// AccountWallets,
// WalletOperationHistories,
// Keys,
// Dids,
// Credentials,
// AccountKeys,
// AccountDids,
// AccountCredentials,
// )
// }
}
8 changes: 8 additions & 0 deletions src/main/kotlin/id/walt/db/models/AccountCredentials.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package id.walt.db.models

import org.jetbrains.exposed.dao.id.UUIDTable

object AccountCredentials : UUIDTable("account_credentials") {
val account = reference("account", Accounts)
val credential = reference("credential", Credentials)
}
10 changes: 10 additions & 0 deletions src/main/kotlin/id/walt/db/models/AccountDids.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package id.walt.db.models

import org.jetbrains.exposed.dao.id.UUIDTable

object AccountDids: UUIDTable("account_dids") {
val account = reference("account", Accounts)
val did = reference("did", Dids)
val alias = varchar("alias", 1024)
val default = bool("default").default(false)
}
8 changes: 8 additions & 0 deletions src/main/kotlin/id/walt/db/models/AccountKeys.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package id.walt.db.models

import org.jetbrains.exposed.dao.id.UUIDTable

object AccountKeys : UUIDTable("account_keys") {
val account = reference("account", Accounts)
val key = reference("key", Keys)
}
8 changes: 8 additions & 0 deletions src/main/kotlin/id/walt/db/models/Credentials.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package id.walt.db.models

import org.jetbrains.exposed.dao.id.UUIDTable

object Credentials : UUIDTable() {
val credentialId = varchar("cid", 256).uniqueIndex()
val document = text("document")
}
9 changes: 9 additions & 0 deletions src/main/kotlin/id/walt/db/models/Dids.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package id.walt.db.models

import org.jetbrains.exposed.dao.id.UUIDTable

object Dids : UUIDTable() {
val did = varchar("did", 1024).uniqueIndex()
val document = text("document")
val key = reference("key", Keys)
}
8 changes: 8 additions & 0 deletions src/main/kotlin/id/walt/db/models/Keys.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package id.walt.db.models

import org.jetbrains.exposed.dao.id.UUIDTable

object Keys : UUIDTable() {
val keyId = varchar("kid", 512).uniqueIndex()
val document = text("document")
}
11 changes: 0 additions & 11 deletions src/main/kotlin/id/walt/db/models/WalletCredentials.kt

This file was deleted.

14 changes: 0 additions & 14 deletions src/main/kotlin/id/walt/db/models/WalletDids.kt

This file was deleted.

11 changes: 0 additions & 11 deletions src/main/kotlin/id/walt/db/models/WalletKeys.kt

This file was deleted.

24 changes: 2 additions & 22 deletions src/main/kotlin/id/walt/db/models/WalletOperationHistories.kt
Original file line number Diff line number Diff line change
@@ -1,31 +1,11 @@
package id.walt.db.models

import id.walt.service.WalletService
import id.walt.utils.JsonUtils.toJsonPrimitives
import kotlinx.datetime.Clock
import kotlinx.datetime.Instant
import kotlinx.serialization.Serializable
import kotlinx.serialization.json.JsonElement
import org.jetbrains.exposed.dao.id.UUIDTable
import org.jetbrains.exposed.sql.javatime.timestamp

object WalletOperationHistories : UUIDTable() {
val account = reference("account", Accounts.id)
val account = reference("account", Accounts)
val timestamp = timestamp("timestamp")
val operation = varchar("operation", 48)
val data = text("data")
}

@Serializable
data class WalletOperationHistory(
val accountId: String,
//val walletId: String,
val timestamp: Instant,
val operation: String,
val data: Map<String, JsonElement>
) {
companion object {
fun new(wallet: WalletService, operation: String, data: Map<String, Any?>) =
WalletOperationHistory(wallet.accountId.toString(), Clock.System.now(), operation, data.toJsonPrimitives())
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package id.walt.db.repositories

import id.walt.db.models.AccountCredentials
import org.jetbrains.exposed.dao.id.EntityID
import org.jetbrains.exposed.sql.ResultRow
import org.jetbrains.exposed.sql.statements.InsertStatement
import java.util.*

object AccountCredentialsRepository : RepositoryBase<DbAccountCredentials>(AccountCredentials) {
override fun ResultRow.fromRow(): DbAccountCredentials = DbAccountCredentials(
id = this[AccountCredentials.id].value,
account = this[AccountCredentials.account].value,
credential = this[AccountCredentials.credential].value,
)

override fun DbAccountCredentials.toRow(insertStatement: InsertStatement<EntityID<UUID>>): InsertStatement<EntityID<UUID>> =
let {
insertStatement[AccountCredentials.account] = it.account
insertStatement[AccountCredentials.credential] = it.credential
insertStatement
}
}

data class DbAccountCredentials(
override val id: UUID? = null,
val account: UUID,
val credential: UUID,
) : DbEntity()
37 changes: 37 additions & 0 deletions src/main/kotlin/id/walt/db/repositories/AccountDidsRepository.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package id.walt.db.repositories

import id.walt.db.models.AccountDids
import org.jetbrains.exposed.dao.id.EntityID
import org.jetbrains.exposed.sql.Column
import org.jetbrains.exposed.sql.ResultRow
import org.jetbrains.exposed.sql.select
import org.jetbrains.exposed.sql.statements.InsertStatement
import org.jetbrains.exposed.sql.transactions.transaction
import java.util.*

object AccountDidsRepository : RepositoryBase<DbAccountDids>(AccountDids) {
override fun ResultRow.fromRow(): DbAccountDids = DbAccountDids(
id = this[AccountDids.id].value,
account = this[AccountDids.account].value,
did = this[AccountDids.did].value,
alias = this[AccountDids.alias],
default = this[AccountDids.default],
)

override fun DbAccountDids.toRow(insertStatement: InsertStatement<EntityID<UUID>>): InsertStatement<EntityID<UUID>> =
let {
insertStatement[AccountDids.account] = it.account
insertStatement[AccountDids.did] = it.did
insertStatement[AccountDids.alias] = it.alias
insertStatement[AccountDids.default] = it.default
insertStatement
}
}

data class DbAccountDids(
override val id: UUID? = null,
val account: UUID,
val did: UUID,
val alias: String,
val default: Boolean,
) : DbEntity()
Loading

0 comments on commit a980a97

Please sign in to comment.