Skip to content

Commit

Permalink
XefToken table with id (#460)
Browse files Browse the repository at this point in the history
* XefToken table with id

* Test fixed
  • Loading branch information
javipacheco authored Sep 27, 2023
1 parent 77213c8 commit 5116b01
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 38 deletions.
Original file line number Diff line number Diff line change
@@ -1,29 +1,19 @@
package com.xebia.functional.xef.server.db.tables

import com.xebia.functional.xef.server.models.ProvidersConfig
import kotlinx.datetime.Instant
import kotlinx.serialization.SerialName
import kotlinx.serialization.json.Json
import org.jetbrains.exposed.dao.IntEntity
import org.jetbrains.exposed.dao.IntEntityClass
import org.jetbrains.exposed.dao.id.EntityID
import org.jetbrains.exposed.dao.id.IntIdTable
import org.jetbrains.exposed.sql.ReferenceOption
import org.jetbrains.exposed.sql.ResultRow
import org.jetbrains.exposed.sql.Table
import org.jetbrains.exposed.sql.json.jsonb
import org.jetbrains.exposed.sql.kotlin.datetime.CurrentTimestamp
import org.jetbrains.exposed.sql.kotlin.datetime.timestamp

val format = Json { prettyPrint = true }

data class XefTokens(
@SerialName("user_id") val userId: Int,
@SerialName("project_id") val projectId: Int,
@SerialName("name") val name: String,
@SerialName("created_at") val createdAt: Instant,
@SerialName("updated_at") val updatedAt: Instant,
@SerialName("token") val token: String,
@SerialName("providers_config") val providersConfig: ProvidersConfig
)

object XefTokensTable : Table("xef_tokens") {
object XefTokensTable : IntIdTable("xef_tokens") {
val userId = reference(
name = "user_id",
foreign = UsersTable,
Expand All @@ -40,17 +30,16 @@ object XefTokensTable : Table("xef_tokens") {
val token = varchar("token", 128).uniqueIndex()
val providersConfig = jsonb<ProvidersConfig>("providers_config", format)

override val primaryKey = PrimaryKey(userId, projectId, name)
}

fun ResultRow.toXefTokens(): XefTokens {
return XefTokens(
userId = this[XefTokensTable.userId].value,
projectId = this[XefTokensTable.projectId].value,
name = this[XefTokensTable.name],
createdAt = this[XefTokensTable.createdAt],
updatedAt = this[XefTokensTable.updatedAt],
token = this[XefTokensTable.token],
providersConfig = this[XefTokensTable.providersConfig]
)
class XefTokens(id: EntityID<Int>) : IntEntity(id) {
companion object : IntEntityClass<XefTokens>(XefTokensTable)

var userId by XefTokensTable.userId
var projectId by XefTokensTable.projectId
var name by XefTokensTable.name
var createdAt by XefTokensTable.createdAt
var updatedAt by XefTokensTable.updatedAt
var token by XefTokensTable.token
var providersConfig by XefTokensTable.providersConfig
}
7 changes: 3 additions & 4 deletions server/src/main/resources/db/migrations/psql/V1__Initial.sql
Original file line number Diff line number Diff line change
Expand Up @@ -53,16 +53,15 @@ CREATE TABLE IF NOT EXISTS users_org(
);

CREATE TABLE IF NOT EXISTS xef_tokens(
user_id INT,
project_id INT,
id SERIAL PRIMARY KEY,
user_id INT NOT NULL,
project_id INT NOT NULL,
name VARCHAR(20) NOT NULL,
created_at TIMESTAMP NOT NULL DEFAULT NOW(),
updated_at TIMESTAMP NOT NULL DEFAULT NOW(),
token VARCHAR(128) UNIQUE,
providers_config JSONB,

PRIMARY KEY (user_id, project_id, name),

CONSTRAINT fk_user_id
FOREIGN KEY (user_id)
REFERENCES users(id) MATCH SIMPLE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,17 +139,17 @@ class XefDatabaseTest {
),
gcp = null
)
XefTokensTable.insert {
it[userId] = user.id.value
it[projectId] = project.id.value
it[name] = "testEnv"
it[token] = "testToken"
it[providersConfig] = config
XefTokens.new {
userId = user.id
projectId = project.id
name = "testEnv"
token = "testToken"
providersConfig = config
}
}
transaction {
val tokens = XefTokensTable.selectAll().map { it.toXefTokens() }
assertEquals("testToken", tokens[0].token)
val token = XefTokens.all().first()
assertEquals("testToken", token.token)
}
}

Expand Down

0 comments on commit 5116b01

Please sign in to comment.