-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: keys migration (document deserialization error)
- Loading branch information
1 parent
9361956
commit 04f0a50
Showing
3 changed files
with
54 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,4 +25,4 @@ dataSource { | |
fullColumnNames = false | ||
} | ||
} | ||
recreateDatabaseOnStart = true | ||
recreateDatabaseOnStart = false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
waltid-services/waltid-wallet-api/src/main/kotlin/id/walt/webwallet/db/Migration.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package id.walt.webwallet.db | ||
|
||
import id.walt.webwallet.db.models.WalletKey | ||
import id.walt.webwallet.db.models.WalletKeys | ||
import kotlinx.datetime.toJavaInstant | ||
import kotlinx.serialization.encodeToString | ||
import kotlinx.serialization.json.* | ||
import org.jetbrains.exposed.sql.batchUpsert | ||
import org.jetbrains.exposed.sql.selectAll | ||
import org.jetbrains.exposed.sql.transactions.transaction | ||
|
||
object Migration { | ||
/** | ||
* Migrate from jwk key string representation to json object | ||
*/ | ||
object Keys { | ||
fun execute() = transaction { | ||
val conversion = WalletKeys.selectAll().mapNotNull { row -> | ||
val wallet = row[WalletKeys.wallet].value | ||
val key = WalletKey(row) | ||
convertDocument(key.document)?.let { | ||
Pair( | ||
wallet, key.copy( | ||
document = it | ||
) | ||
) | ||
} | ||
} | ||
WalletKeys.batchUpsert(conversion) { | ||
this[WalletKeys.document] = it.second.document | ||
this[WalletKeys.keyId] = it.second.keyId | ||
this[WalletKeys.wallet] = it.first | ||
this[WalletKeys.createdOn] = it.second.createdOn.toJavaInstant() | ||
} | ||
} | ||
|
||
private fun convertDocument(document: String): String? = let { | ||
val json = Json.decodeFromString<JsonObject>(document) | ||
json["jwk"].takeIf { it is JsonPrimitive }?.let { | ||
json["jwk"]?.jsonPrimitive?.content?.replace("\\", "")?.let { | ||
buildJsonObject { | ||
put("type", json["type"]!!) | ||
put("jwk", Json.decodeFromString<JsonObject>(it)) | ||
} | ||
}?.let { | ||
Json.encodeToString(it) | ||
} | ||
} | ||
} | ||
} | ||
} |