Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Xef DB to incorporate users, projects and organizations #411

Merged
merged 10 commits into from
Sep 12, 2023
Merged
8 changes: 8 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
[versions]
arrow = "1.2.0"
arrowGradle = "0.12.0-rc.5"
exposed = "0.43.0"
kotlin = "1.9.10"
kotlinx-json = "1.6.0"
kotlinx-datetime = "0.4.0"
Expand Down Expand Up @@ -49,6 +50,12 @@ detekt = "1.23.1"
arrow-core = { module = "io.arrow-kt:arrow-core", version.ref = "arrow" }
arrow-continuations = { module = "io.arrow-kt:arrow-continuations", version.ref = "arrow" }
arrow-fx-coroutines = { module = "io.arrow-kt:arrow-fx-coroutines", version.ref = "arrow" }
exposed-core = { module = "org.jetbrains.exposed:exposed-core", version.ref = "exposed" }
exposed-dao = { module = "org.jetbrains.exposed:exposed-dao", version.ref = "exposed" }
exposed-jdbc = { module = "org.jetbrains.exposed:exposed-jdbc", version.ref = "exposed" }
exposed-java-time = { module = "org.jetbrains.exposed:exposed-java-time", version.ref = "exposed" }
exposed-kotlin-datetime = { module = "org.jetbrains.exposed:exposed-kotlin-datetime", version.ref = "exposed" }
exposed-json = { module = "org.jetbrains.exposed:exposed-json", version.ref = "exposed" }
flyway-core = { module = "org.flywaydb:flyway-core", version.ref = "flyway" }
suspendApp-core = { module = "io.arrow-kt:suspendapp", version.ref = "suspendApp" }
suspendApp-ktor = { module = "io.arrow-kt:suspendapp-ktor", version.ref = "suspendApp" }
Expand Down Expand Up @@ -87,6 +94,7 @@ kotest-testcontainers = { module = "io.kotest.extensions:kotest-extensions-testc
kotest-assertions-arrow = { module = "io.kotest.extensions:kotest-assertions-arrow", version.ref = "kotest-arrow" }
ktor-serialization-json = { module = "io.ktor:ktor-serialization-kotlinx-json", version.ref = "ktor" }
junit-jupiter-api = { module = "org.junit.jupiter:junit-jupiter-api", version.ref = "junit" }
junit-jupiter-engine = { module = "org.junit.jupiter:junit-jupiter-engine", version.ref = "junit" }
uuid = { module = "app.softwork:kotlinx-uuid-core", version.ref = "uuid" }
klogging = { module = "io.github.oshai:kotlin-logging", version.ref = "klogging" }
hikari = { module = "com.zaxxer:HikariCP", version.ref = "hikari" }
Expand Down
19 changes: 19 additions & 0 deletions server/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ node {
}

dependencies {
implementation(libs.exposed.core)
implementation(libs.exposed.dao)
implementation(libs.exposed.kotlin.datetime)
implementation(libs.exposed.java.time)
implementation(libs.exposed.jdbc)
implementation(libs.exposed.json)
implementation(libs.flyway.core)
implementation(libs.hikari)
implementation(libs.klogging)
Expand Down Expand Up @@ -48,6 +54,15 @@ dependencies {
implementation(projects.xefCore)
implementation(projects.xefLucene)
implementation(projects.xefPostgresql)

testImplementation(libs.junit.jupiter.api)
testImplementation(libs.junit.jupiter.engine)
testImplementation(libs.kotest.property)
testImplementation(libs.kotest.framework)
testImplementation(libs.kotest.assertions)
testImplementation(libs.kotest.testcontainers)
testImplementation(libs.testcontainers.postgresql)
testRuntimeOnly(libs.kotest.junit5)
}

tasks.getByName<Copy>("processResources") {
Expand All @@ -71,3 +86,7 @@ task<JavaExec>("server") {
classpath = sourceSets.main.get().runtimeClasspath
mainClass.set("com.xebia.functional.xef.server.Server")
}

tasks.named<Test>("test") {
useJUnitPlatform()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.xebia.functional.xef.server.db.tables

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.kotlin.datetime.CurrentTimestamp
import org.jetbrains.exposed.sql.kotlin.datetime.timestamp

object OrganizationTable : IntIdTable() {
val name = varchar("name", 20)
val createdAt = timestamp("created_at").defaultExpression(CurrentTimestamp())
val updatedAt = timestamp("updated_at").defaultExpression(CurrentTimestamp())
val ownerId = reference(
name = "owner_id",
refColumn = UsersTable.id,
onDelete = ReferenceOption.CASCADE
)
}

class Organization(id: EntityID<Int>) : IntEntity(id) {
companion object : IntEntityClass<Organization>(OrganizationTable)

var name by OrganizationTable.name
var createdAt by OrganizationTable.createdAt
var updatedAt by OrganizationTable.updatedAt
var ownerId by OrganizationTable.ownerId

var users by User via UsersOrgsTable
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.xebia.functional.xef.server.db.tables

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.kotlin.datetime.CurrentTimestamp
import org.jetbrains.exposed.sql.kotlin.datetime.timestamp

object ProjectsTable: IntIdTable() {
val name = varchar("name", 20)
val createdAt = timestamp("created_at").defaultExpression(CurrentTimestamp())
val updatedAt = timestamp("updated_at").defaultExpression(CurrentTimestamp())
val orgId = reference(
name = "org_id",
refColumn = OrganizationTable.id,
onDelete = ReferenceOption.CASCADE
)
}

class Project(id: EntityID<Int>) : IntEntity(id) {
companion object : IntEntityClass<Project>(ProjectsTable)

var name by ProjectsTable.name
var createdAt by ProjectsTable.createdAt
var updatedAt by ProjectsTable.updatedAt
var orgId by ProjectsTable.orgId
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.xebia.functional.xef.server.db.tables

import org.jetbrains.exposed.sql.ReferenceOption
import org.jetbrains.exposed.sql.Table

object UsersOrgsTable : Table() {
val userId = reference(
name = "user_id",
foreign = UsersTable,
onDelete = ReferenceOption.CASCADE
)
val orgId = reference(
name = "org_id",
foreign = OrganizationTable,
onDelete = ReferenceOption.CASCADE
)

override val primaryKey = PrimaryKey(userId, orgId)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.xebia.functional.xef.server.db.tables

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.kotlin.datetime.CurrentTimestamp
import org.jetbrains.exposed.sql.kotlin.datetime.timestamp


object UsersTable : IntIdTable() {
val name = varchar("name", 20)
val email = varchar("email", 50)
val passwordHash = varchar("password_hash", 50)
val salt = varchar("salt", 20)
val createdAt = timestamp("created_at").defaultExpression(CurrentTimestamp())
val updatedAt = timestamp("updated_at").defaultExpression(CurrentTimestamp())
val authToken = varchar("auth_token", 128)
}

class User(id: EntityID<Int>) : IntEntity(id) {
companion object : IntEntityClass<User>(UsersTable)

var name by UsersTable.name
var email by UsersTable.email
var passwordHash by UsersTable.passwordHash
var salt by UsersTable.salt
var createdAt by UsersTable.createdAt
var updatedAt by UsersTable.updatedAt
var authToken by UsersTable.authToken

var organizations by Organization via UsersOrgsTable
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.xebia.functional.xef.server.db.tables

import com.xebia.functional.xef.server.db.tables.OrganizationTable.defaultExpression
import com.xebia.functional.xef.server.models.ProvidersConfig
import com.xebia.functional.xef.server.models.XefTokens
import kotlinx.serialization.json.Json
import org.jetbrains.exposed.dao.Entity
import org.jetbrains.exposed.dao.EntityClass
import org.jetbrains.exposed.dao.IntEntity
import org.jetbrains.exposed.dao.IntEntityClass
import org.jetbrains.exposed.dao.id.EntityID
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 }

object XefTokensTable : Table() {
val userId = reference(
name = "user_id",
foreign = UsersTable,
onDelete = ReferenceOption.CASCADE)
val projectId = reference(
name = "project_id",
foreign = ProjectsTable,
onDelete = ReferenceOption.CASCADE
)
val name = varchar("name", 20)
val createdAt = timestamp("created_at").defaultExpression(CurrentTimestamp())
val updatedAt = timestamp("updated_at").defaultExpression(CurrentTimestamp())
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]
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.xebia.functional.xef.server.models

import kotlinx.datetime.Instant
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable

@Serializable
data class XefTokens(
Montagon marked this conversation as resolved.
Show resolved Hide resolved
@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
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.xebia.functional.xef.server.models

import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable

@Serializable
@SerialName("open_ai")
data class OpenAIConf(
val name: String,
val token: String,
val url: String
)

@Serializable
@SerialName("gcp")
data class GCPConf(
val name: String,
val token: String,
val projectId: String,
val location: String
)

@Serializable
data class ProvidersConfig(
@SerialName("open_ai")
val openAI: OpenAIConf?,
@SerialName("gcp")
val gcp: GCPConf?
)
75 changes: 75 additions & 0 deletions server/src/main/resources/db/migrations/psql/V1__Initial.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
CREATE TABLE IF NOT EXISTS users(
id SERIAL PRIMARY KEY,
name VARCHAR(20) NOT NULL,
email VARCHAR(50) UNIQUE NOT NULL,
password_hash VARCHAR(50) NOT NULL,
salt VARCHAR(20) NOT NULL,
created_at TIMESTAMP NOT NULL DEFAULT NOW(),
updated_at TIMESTAMP NOT NULL DEFAULT NOW(),
auth_token VARCHAR(128) UNIQUE NOT NULL
);

CREATE TABLE IF NOT EXISTS organizations(
id SERIAL PRIMARY KEY,
name VARCHAR(20) NOT NULL,
created_at TIMESTAMP NOT NULL DEFAULT NOW(),
updated_at TIMESTAMP NOT NULL DEFAULT NOW(),
owner_id INT NOT NULL,

CONSTRAINT fk_user_id
FOREIGN KEY (owner_id)
REFERENCES users(id) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE CASCADE
);

CREATE TABLE IF NOT EXISTS projects(
id SERIAL PRIMARY KEY,
name VARCHAR(20) UNIQUE,
created_at TIMESTAMP NOT NULL DEFAULT NOW(),
updated_at TIMESTAMP NOT NULL DEFAULT NOW(),
org_id INT NOT NULL,

CONSTRAINT fk_org_id
FOREIGN KEY (org_id)
REFERENCES organizations(id) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE CASCADE
);

CREATE TABLE IF NOT EXISTS users_org(
user_id INT,
org_id INT,

PRIMARY KEY (user_id, org_id),

CONSTRAINT fk_user_id
FOREIGN KEY (user_id)
REFERENCES users(id) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE CASCADE,

CONSTRAINT fk_org_id
FOREIGN KEY (org_id)
REFERENCES organizations(id) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE CASCADE
);

CREATE TABLE IF NOT EXISTS xef_tokens(
user_id INT,
project_id INT,
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
ON UPDATE NO ACTION ON DELETE CASCADE,

CONSTRAINT fk_project_id
FOREIGN KEY (project_id)
REFERENCES projects(id) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE CASCADE
);
Loading