-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update Xef DB to incorporate users, projects and organizations (#411)
* initial migration * added missing fields * updated sql and added models and table * updated models * added tests * updated models and added tests * minor changes * refactored
- Loading branch information
Showing
11 changed files
with
502 additions
and
0 deletions.
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
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
32 changes: 32 additions & 0 deletions
32
server/src/main/kotlin/com/xebia/functional/xef/server/db/tables/OrganizationTable.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,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 | ||
} | ||
|
30 changes: 30 additions & 0 deletions
30
server/src/main/kotlin/com/xebia/functional/xef/server/db/tables/ProjectsTable.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,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 | ||
} | ||
|
19 changes: 19 additions & 0 deletions
19
server/src/main/kotlin/com/xebia/functional/xef/server/db/tables/UsersOrgsTable.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,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) | ||
} |
33 changes: 33 additions & 0 deletions
33
server/src/main/kotlin/com/xebia/functional/xef/server/db/tables/UsersTable.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,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 | ||
} |
55 changes: 55 additions & 0 deletions
55
server/src/main/kotlin/com/xebia/functional/xef/server/db/tables/XefTokensTable.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,55 @@ | ||
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.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() { | ||
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] | ||
) | ||
} |
29 changes: 29 additions & 0 deletions
29
server/src/main/kotlin/com/xebia/functional/xef/server/models/ProvidersConfig.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,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? | ||
) |
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,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 | ||
); |
46 changes: 46 additions & 0 deletions
46
server/src/test/kotlin/com/xebia/functional/xef/server/postgresql/DBHelpers.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,46 @@ | ||
package com.xebia.functional.xef.server.postgresql | ||
|
||
import com.xebia.functional.xef.server.db.tables.Organization | ||
import com.xebia.functional.xef.server.db.tables.Project | ||
import com.xebia.functional.xef.server.db.tables.User | ||
import org.jetbrains.exposed.dao.id.EntityID | ||
|
||
object DBHelpers { | ||
|
||
fun testUser( | ||
fName: String = "test", | ||
fEmail: String = "test@test/com", | ||
fPasswordHash: String = "passwordTest", | ||
fSalt: String = "saltTest", | ||
fAuthToken: String = "authTokenTest" | ||
): User { | ||
return User.new { | ||
name = fName | ||
email = fEmail | ||
passwordHash = fPasswordHash | ||
salt = fSalt | ||
authToken = fAuthToken | ||
} | ||
} | ||
|
||
fun testOrganization( | ||
fName: String = "testOrg", | ||
fOwnerId: EntityID<Int> | ||
): Organization { | ||
return Organization.new { | ||
name = fName | ||
ownerId = fOwnerId | ||
} | ||
} | ||
|
||
fun testProject( | ||
fName: String = "testProject", | ||
fOrgId: EntityID<Int> | ||
): Project { | ||
return Project.new { | ||
name = fName | ||
orgId = fOrgId | ||
} | ||
} | ||
|
||
} |
Oops, something went wrong.