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
7 changes: 7 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
6 changes: 6 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.xebia.functional.xef.server.db.tables

import com.xebia.functional.xef.server.models.Organization
import org.jetbrains.exposed.sql.ReferenceOption
import org.jetbrains.exposed.sql.ResultRow
import org.jetbrains.exposed.sql.Table
import org.jetbrains.exposed.sql.kotlin.datetime.timestamp

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

override val primaryKey = PrimaryKey(id)
}

fun ResultRow.toOrganization(): Organization {
return Organization(
id = this[OrganizationTable.id],
name = this[OrganizationTable.name],
createdAt = this[OrganizationTable.createdAt],
updatedAt = this[OrganizationTable.updatedAt],
ownerId = this[OrganizationTable.ownerId]
)
}

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

import com.xebia.functional.xef.server.models.Projects
import org.jetbrains.exposed.sql.ReferenceOption
import org.jetbrains.exposed.sql.ResultRow
import org.jetbrains.exposed.sql.Table
import org.jetbrains.exposed.sql.kotlin.datetime.timestamp

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

override val primaryKey = PrimaryKey(id)
}

fun ResultRow.toProject(): Projects {
return Projects(
id = this[ProjectsTable.id],
name = this[ProjectsTable.name],
createdAt = this[ProjectsTable.createdAt],
updatedAt = this[ProjectsTable.updatedAt],
orgId = this[ProjectsTable.orgId]
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.xebia.functional.xef.server.db.tables

import com.xebia.functional.xef.server.models.UsersOrgRelation
import org.jetbrains.exposed.sql.ReferenceOption
import org.jetbrains.exposed.sql.ResultRow
import org.jetbrains.exposed.sql.Table

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

override val primaryKey = PrimaryKey(userId, orgId)
}

fun ResultRow.toUsersOrgs(): UsersOrgRelation {
return UsersOrgRelation(
userId = this[UsersOrgsTable.userId],
orgId = this[UsersOrgsTable.orgId]
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.xebia.functional.xef.server.db.tables

import com.xebia.functional.xef.server.models.Users
import org.jetbrains.exposed.sql.ResultRow
import org.jetbrains.exposed.sql.Table
import org.jetbrains.exposed.sql.kotlin.datetime.timestamp


object UsersTable : Table() {
val id = integer("id").autoIncrement()
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")
val updatedAt = timestamp("updated_at")
val authToken = varchar("auth_token", 128)

override val primaryKey = PrimaryKey(id)
}

fun ResultRow.toUser(): Users {
return Users(
id = this[UsersTable.id],
name = this[UsersTable.name],
email = this[UsersTable.email],
password = this[UsersTable.passwordHash],
salt = this[UsersTable.salt],
createdAt = this[UsersTable.createdAt],
updatedAt = this[UsersTable.updatedAt],
authToken = this[UsersTable.authToken]
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.xebia.functional.xef.server.db.tables

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.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.timestamp

val format = Json { prettyPrint = true }

object XefTokensTable : Table() {
val userId = reference(
name = "user_id",
refColumn = UsersTable.id,
onDelete = ReferenceOption.CASCADE)
val projectId = reference(
name = "project_id",
refColumn = ProjectsTable.id,
onDelete = ReferenceOption.CASCADE
)
val name = varchar("name", 20)
val createdAt = timestamp("created_at")
val updatedAt = timestamp("updated_at")
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],
projectId = this[XefTokensTable.projectId],
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,52 @@
package com.xebia.functional.xef.server.models

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

@Serializable
data class Users(
@SerialName("id") val id: Int,
@SerialName("name") val name: String,
@SerialName("email") val email: String,
@SerialName("password_hash") val password: String,
@SerialName("salt") val salt: String,
@SerialName("created_at") val createdAt: Instant,
@SerialName("updated_at") val updatedAt: Instant,
@SerialName("auth_token") val authToken: String
)

@Serializable
data class Organization(
@SerialName("id") val id: Int,
@SerialName("name") val name: String,
@SerialName("created_at") val createdAt: Instant,
@SerialName("updated_at") val updatedAt: Instant,
@SerialName("owner_id") val ownerId: Int
)

@Serializable
data class Projects(
@SerialName("id") val id: Int,
@SerialName("name") val name: String,
@SerialName("created_at") val createdAt: Instant,
@SerialName("updated_at") val updatedAt: Instant,
@SerialName("org_id") val orgId: Int
)

@Serializable
data class UsersOrgRelation(
@SerialName("user_id") val userId: Int,
@SerialName("org_id") val orgId: Int
)

@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),
created_at TIMESTAMP NOT NULL DEFAULT NOW(),
updated_at TIMESTAMP NOT NULL DEFAULT NOW(),
org_id INT,

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
);