Skip to content

Commit

Permalink
refactor: fix db migration method
Browse files Browse the repository at this point in the history
Signed-off-by: Ahmed Moussa <ahmed.moussa@iohk.io>
  • Loading branch information
hamada147 committed Jan 3, 2024
1 parent 628a328 commit e487a8a
Show file tree
Hide file tree
Showing 20 changed files with 130 additions and 69 deletions.
18 changes: 10 additions & 8 deletions atala-prism-sdk/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ val apolloVersion = project.property("apollo_version")
val didpeerVersion = project.property("didpeer_version")

plugins {
id("com.squareup.sqldelight")
id("app.cash.sqldelight") version "2.0.1"
kotlin("multiplatform")
kotlin("plugin.serialization")
id("com.android.library")
Expand Down Expand Up @@ -39,7 +39,7 @@ koverReport {
}

kotlin {
android {
androidTarget {
publishAllLibraryVariants()
}

Expand Down Expand Up @@ -84,7 +84,7 @@ kotlin {
exclude("com.google.protobuf")
}

implementation("com.squareup.sqldelight:coroutines-extensions:1.5.5")
implementation("app.cash.sqldelight:coroutines-extensions:2.0.1")

api("org.lighthousegames:logging:1.1.2")

Expand All @@ -104,15 +104,15 @@ kotlin {
val jvmMain by getting {
dependencies {
implementation("io.ktor:ktor-client-okhttp:2.3.4")
implementation("com.squareup.sqldelight:sqlite-driver:1.5.5")
implementation("app.cash.sqldelight:sqlite-driver:2.0.1")
}
}
val jvmTest by getting
val androidMain by getting {
dependencies {
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-android:1.7.3")
implementation("io.ktor:ktor-client-okhttp:2.3.4")
implementation("com.squareup.sqldelight:android-driver:1.5.5")
implementation("app.cash.sqldelight:android-driver:2.0.1")
}
}
val androidInstrumentedTest by getting {
Expand Down Expand Up @@ -173,9 +173,11 @@ android {
}

sqldelight {
database("PrismPlutoDb") {
packageName = "io.iohk.atala.prism.walletsdk"
sourceFolders = listOf("sqldelight")
databases {
create("PrismPlutoDb") {
packageName.set("io.iohk.atala.prism.walletsdk")
srcDirs.setFrom("src/commonMain/sqldelight")
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package io.iohk.atala.prism.walletsdk.pluto.data

import android.content.Context
import com.squareup.sqldelight.android.AndroidSqliteDriver
import com.squareup.sqldelight.db.SqlDriver
import app.cash.sqldelight.db.SqlDriver
import app.cash.sqldelight.driver.android.AndroidSqliteDriver
import io.iohk.atala.prism.walletsdk.PrismPlutoDb
import io.iohk.atala.prism.walletsdk.domain.models.PlutoError

Expand All @@ -11,11 +11,11 @@ import io.iohk.atala.prism.walletsdk.domain.models.PlutoError
*/
actual class DbConnection actual constructor() {
actual var driver: SqlDriver? = null
actual suspend fun connectDb(context: Any?): PrismPlutoDb {
actual suspend fun connectDb(context: Any?): SqlDriver {
val androidContext: Context = (context as? Context) ?: throw PlutoError.DatabaseContextError()
val driver = AndroidSqliteDriver(PrismPlutoDb.Schema, androidContext, "prism.db")
this.driver = driver
return PrismPlutoDb(driver)
return driver
}
}

Expand All @@ -25,7 +25,8 @@ actual class DbConnection actual constructor() {
actual val SqlDriver.isConnected: Boolean
get() {
try {
return this.executeQuery(null, "SELECT 1", 0).next()
return this.execute(null, "SELECT 1", 0).value == 1L
// return this.executeQuery(null, "SELECT 1", 0).next()
} catch (ex: Exception) {
return false
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -649,7 +649,7 @@ constructor(
*
* @see PlutoError
*/
class DatabaseConnectionError : PlutoError("Database connection error") {
class DatabaseConnectionError(message: String? = "Database connection error") : PlutoError(message) {
override val code: Int
get() = 46
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package io.iohk.atala.prism.walletsdk.pluto

import com.squareup.sqldelight.runtime.coroutines.asFlow
import com.squareup.sqldelight.runtime.coroutines.mapToList
import app.cash.sqldelight.ColumnAdapter
import app.cash.sqldelight.coroutines.asFlow
import app.cash.sqldelight.coroutines.mapToList
import app.cash.sqldelight.db.AfterVersion
import io.iohk.atala.prism.apollo.base64.base64UrlDecodedBytes
import io.iohk.atala.prism.apollo.base64.base64UrlEncoded
import io.iohk.atala.prism.apollo.uuid.UUID
Expand All @@ -24,20 +26,21 @@ import io.iohk.atala.prism.walletsdk.pluto.data.DbConnection
import io.iohk.atala.prism.walletsdk.pluto.data.isConnected
import io.iohk.atala.prism.walletsdk.pollux.models.CredentialRequestMeta
import io.iohk.atala.prism.walletsdk.pollux.models.LinkSecretBlindingData
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.mapLatest
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json
import ioiohkatalaprismwalletsdkpluto.data.AvailableClaims as AvailableClaimsDB
import ioiohkatalaprismwalletsdkpluto.data.DID as DIDDB
import ioiohkatalaprismwalletsdkpluto.data.DIDPair as DIDPairDB
import ioiohkatalaprismwalletsdkpluto.data.LinkSecret as LinkSecretDB
import ioiohkatalaprismwalletsdkpluto.data.Mediator as MediatorDB
import ioiohkatalaprismwalletsdkpluto.data.Message as MessageDB
import ioiohkatalaprismwalletsdkpluto.data.PrivateKey as PrivateKeyDB
import ioiohkatalaprismwalletsdkpluto.data.StorableCredential as StorableCredentialDB
import io.iohk.atala.prism.walletsdk.pluto.data.AvailableClaims as AvailableClaimsDB
import io.iohk.atala.prism.walletsdk.pluto.data.DID as DIDDB
import io.iohk.atala.prism.walletsdk.pluto.data.DIDPair as DIDPairDB
import io.iohk.atala.prism.walletsdk.pluto.data.LinkSecret as LinkSecretDB
import io.iohk.atala.prism.walletsdk.pluto.data.Mediator as MediatorDB
import io.iohk.atala.prism.walletsdk.pluto.data.Message as MessageDB
import io.iohk.atala.prism.walletsdk.pluto.data.PrivateKey as PrivateKeyDB
import io.iohk.atala.prism.walletsdk.pluto.data.StorableCredential as StorableCredentialDB

/**
* `PlutoImpl` is a class that provides an implementation of the Pluto interface for interacting with the database.
Expand All @@ -48,6 +51,23 @@ import ioiohkatalaprismwalletsdkpluto.data.StorableCredential as StorableCredent
class PlutoImpl(private val connection: DbConnection) : Pluto {
private var db: PrismPlutoDb? = null

init {
this.connection.driver?.let { driver ->
PrismPlutoDb.Schema.migrate(
driver,
1,
PrismPlutoDb.Schema.version,
AfterVersion(1) {
it.execute(null, "ALTER TABLE CredentialMetadata DROB COLUMN nonce;", 0)
it.execute(null, "ALTER TABLE CredentialMetadata DROB COLUMN linkSecretBlindingData;", 0)
it.execute(null, "ALTER TABLE CredentialMetadata ADD COLUMN json TEXT;", 0)
}
)
} ?: {
throw PlutoError.DatabaseConnectionError("Database migration failed to: ${PrismPlutoDb.Schema.version}")
}
}

/**
* isConnected indicates whether the connection to the database is currently established or not.
*
Expand All @@ -70,7 +90,42 @@ class PlutoImpl(private val connection: DbConnection) : Pluto {
if (this.db != null) {
throw PlutoError.DatabaseServiceAlreadyRunning()
}
this.db = this.connection.connectDb(context)
this.db = PrismPlutoDb(
this.connection.connectDb(context),
io.iohk.atala.prism.walletsdk.pluto.data.Message.Adapter(
isReceivedAdapter = object : ColumnAdapter<Int, Long> {
override fun decode(databaseValue: Long): Int {
return databaseValue.toInt()
}

override fun encode(value: Int): Long {
return value.toLong()
}
}
),
io.iohk.atala.prism.walletsdk.pluto.data.PrivateKey.Adapter(
keyPathIndexAdapter = object : ColumnAdapter<Int, Long> {
override fun decode(databaseValue: Long): Int {
return databaseValue.toInt()
}

override fun encode(value: Int): Long {
return value.toLong()
}
}
),
io.iohk.atala.prism.walletsdk.pluto.data.StorableCredential.Adapter(
revokedAdapter = object : ColumnAdapter<Int, Long> {
override fun decode(databaseValue: Long): Int {
return databaseValue.toInt()
}

override fun encode(value: Int): Long {
return value.toLong()
}
}
)
)
}

/**
Expand Down Expand Up @@ -313,12 +368,14 @@ class PlutoImpl(private val connection: DbConnection) : Pluto {
* @param metadata The metadata to store. It must be an instance of [CredentialRequestMeta].
*/
override fun storeCredentialMetadata(metadata: CredentialRequestMeta) {
getInstance().credentialMetadataQueries.insert(
id = UUID.randomUUID4().toString(),
nonce = metadata.nonce,
linkSecretName = metadata.linkSecretName,
linkSecretBlindingData = Json.encodeToString(metadata.linkSecretBlindingData)
)
// TODO: Handle before PR
getInstance().credentialMetadataQueries.insert("", "", "")
// getInstance().credentialMetadataQueries.insert(
// id = UUID.randomUUID4().toString(),
// nonce = metadata.nonce,
// linkSecretName = metadata.linkSecretName,
// linkSecretBlindingData = Json.encodeToString(metadata.linkSecretBlindingData)
// )
}

/**
Expand All @@ -330,7 +387,7 @@ class PlutoImpl(private val connection: DbConnection) : Pluto {
return getInstance().dIDQueries
.fetchAllPrismDID()
.asFlow()
.mapToList()
.mapToList(Dispatchers.Default)
.map { list ->
list.map {
PrismDIDInfo(DID(it.did), it.keyPathIndex, it.alias)
Expand Down Expand Up @@ -962,11 +1019,13 @@ class PlutoImpl(private val connection: DbConnection) : Pluto {
.asFlow()
.map {
val metadata = it.executeAsOne()
CredentialRequestMeta(
nonce = metadata.nonce,
linkSecretName = metadata.linkSecretName,
linkSecretBlindingData = LinkSecretBlindingData(metadata.linkSecretBlindingData)
)
// TODO: Handle before PR
CredentialRequestMeta(LinkSecretBlindingData(""), "", "")
// CredentialRequestMeta(
// nonce = metadata.nonce,
// linkSecretName = metadata.linkSecretName,
// linkSecretBlindingData = LinkSecretBlindingData(metadata.linkSecretBlindingData)
// )
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package io.iohk.atala.prism.walletsdk.pluto.data

import com.squareup.sqldelight.db.SqlDriver
import io.iohk.atala.prism.walletsdk.PrismPlutoDb
import app.cash.sqldelight.db.SqlDriver

/**
* DbConnection class represents a connection to the database.
Expand All @@ -16,7 +15,7 @@ expect class DbConnection() {
*
* @return The PrismPlutoDb instance representing the connection to the database.
*/
suspend fun connectDb(context: Any?): PrismPlutoDb
suspend fun connectDb(context: Any?): SqlDriver
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -695,11 +695,13 @@ class PrismAgent {
val plutoMetadata =
pluto.getCredentialMetadata(message.thid).first()
?: throw io.iohk.atala.prism.walletsdk.domain.models.UnknownError.SomethingWentWrongError("Invalid credential metadata")
CredentialRequestMetadata(
linkSecretBlindingData = Json.encodeToString(plutoMetadata.linkSecretBlindingData.toString()),
linkSecretName = plutoMetadata.linkSecretName,
nonce = Nonce.newFromValue(plutoMetadata.nonce)
)
// TODO: Handle this before PR
CredentialRequestMetadata("")
// CredentialRequestMetadata(
// linkSecretBlindingData = Json.encodeToString(plutoMetadata.linkSecretBlindingData.toString()),
// linkSecretName = plutoMetadata.linkSecretName,
// nonce = Nonce.newFromValue(plutoMetadata.nonce)
// )
} else {
null
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
CREATE TABLE CredentialMetadata (
id TEXT NOT NULL PRIMARY KEY,
linkSecretName TEXT NOT NULL,
json TEXT NOT NULL
);

insert:
INSERT INTO CredentialMetadata(id, linkSecretName, json)
VALUES (:id, :linkSecretName, :json);

fetchCredentialMetadata:
SELECT *
FROM CredentialMetadata
WHERE linkSecretName = ?;
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import io.iohk.atala.prism.walletsdk.pollux.models.CredentialRequestMeta
import io.iohk.atala.prism.walletsdk.domain.models.keyManagement.PrivateKey
import io.iohk.atala.prism.walletsdk.domain.models.keyManagement.StorableKey
import io.iohk.atala.prism.walletsdk.pluto.CredentialRecovery
import ioiohkatalaprismwalletsdkpluto.data.AvailableClaims
import io.iohk.atala.prism.walletsdk.pluto.data.AvailableClaims
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flow
/* ktlint-disable import-ordering */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import io.iohk.atala.prism.walletsdk.domain.models.StorableCredential
import io.iohk.atala.prism.walletsdk.domain.models.keyManagement.PrivateKey
import io.iohk.atala.prism.walletsdk.domain.models.keyManagement.StorableKey
import io.iohk.atala.prism.walletsdk.pluto.CredentialRecovery
import io.iohk.atala.prism.walletsdk.pluto.data.AvailableClaims
import io.iohk.atala.prism.walletsdk.pollux.models.CredentialRequestMeta
import ioiohkatalaprismwalletsdkpluto.data.AvailableClaims
import kotlinx.coroutines.flow.Flow

class PlutoMock : Pluto {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
package io.iohk.atala.prism.walletsdk.pluto.data

import com.squareup.sqldelight.db.SqlDriver
import com.squareup.sqldelight.sqlite.driver.JdbcSqliteDriver
import app.cash.sqldelight.db.SqlDriver
import app.cash.sqldelight.driver.jdbc.sqlite.JdbcSqliteDriver
import io.iohk.atala.prism.walletsdk.PrismPlutoDb

actual class DbConnection actual constructor() {
actual var driver: SqlDriver? = null
actual suspend fun connectDb(context: Any?): PrismPlutoDb {
val driver = JdbcSqliteDriver(JdbcSqliteDriver.IN_MEMORY).also { driver ->
PrismPlutoDb.Schema.create(driver)
}
actual suspend fun connectDb(context: Any?): SqlDriver {
val driver = JdbcSqliteDriver(JdbcSqliteDriver.IN_MEMORY)
PrismPlutoDb.Schema.create(driver)
this.driver = driver
return PrismPlutoDb(driver)
return driver
}
}

Expand Down

0 comments on commit e487a8a

Please sign in to comment.