refactor migration

This commit is contained in:
2025-08-17 15:40:26 +02:00
parent dcb490ae31
commit 132a8913aa
3 changed files with 52 additions and 54 deletions

View File

@@ -6,7 +6,7 @@ postgres = "42.7.5"
koin = "4.0.3"
random = "1.0.0"
otp = "1.1.1"
kdf = "1.0.1"
kdf = "1.0.3"
hash = "1.2.2"
encoding = "1.1.0"
logback = "1.5.18"

View File

@@ -5,7 +5,6 @@ import com.infendro.account.model.repository.AccountRepository
import com.infendro.account.model.repository.MigrationRepository
import com.infendro.account.util.SecureHasher
import io.ktor.server.application.*
import org.intellij.lang.annotations.Language
import org.jetbrains.exposed.sql.SchemaUtils
import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq
import org.jetbrains.exposed.sql.Transaction
@@ -17,8 +16,10 @@ fun Application.migrate() {
SchemaUtils.create(MigrationTable)
}
migration(
"initial",
val accounts by inject<AccountRepository>()
migration("initial") {
exec(
"""
CREATE TABLE access
(
@@ -57,8 +58,6 @@ fun Application.migrate() {
);
""".trimIndent()
)
migration("owner") {
val accounts by inject<AccountRepository>()
accounts.insert {
val salt = SecureHasher.generateSalt()
@@ -66,7 +65,7 @@ fun Application.migrate() {
this.username = "infendro"
this.passwordHash = SecureHasher.hash("password", salt)
this.passwordSalt = salt
this.secret = "deadbeef"
this.secret = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA===="
this.role = Role.OWNER
}
}
@@ -83,17 +82,16 @@ private fun Application.migration(
?.run { return }
transaction {
try {
block()
log.info("""Migration "$name" successful""")
} catch (e: Exception) {
log.error("""Migration "$name" failed""")
throw e
}
}
migrations.insert {
this.name = name
}
}
private fun Application.migration(
name: String,
@Language("sql") sql: String,
) = migration(name) {
exec(sql)
}

View File

@@ -24,6 +24,6 @@ object OTP {
fun generateSecret(): String {
val bytes = SecretGenerator.generate(totp.hotp.function)
return Base32.encode(bytes).toString()
return Base32.encode(bytes).decodeToString()
}
}