Models and schemas for users and auth tokens

This commit is contained in:
2026-07-09 16:12:09 +03:00
parent ef0d281dcb
commit e06c53a9ff
9 changed files with 105 additions and 1 deletions
+1
View File
@@ -0,0 +1 @@
{"name":"sbt","version":"1.12.13","bspVersion":"2.1.0-M1","languages":["scala"],"argv":["/usr/lib/jvm/java-21-openjdk-amd64/bin/java","-Xms100m","-Xmx100m","-classpath","/home/artemiy/.local/share/JetBrains/IntelliJIdea2026.1/Scala/launcher/sbt-launch.jar","-Dsbt.script=/home/artemiy/.local/bin/sbt","xsbt.boot.Boot","-bsp"]}
+3
View File
@@ -5,3 +5,6 @@
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*
/project/project/target/
/project/target
/target
+7
View File
@@ -0,0 +1,7 @@
package models
import java.time.Instant
case class User(id: Option[Int], username: String, gid: String, passwordDigest: Option[String])
case class AuthToken(id: Option[Int], userId: Int, token: String, createdAt: Instant, expiresAt: Instant)
+34
View File
@@ -0,0 +1,34 @@
package schemas
import slick.jdbc.PostgresProfile.api._
import models._
import slick.lifted.ProvenShape
import slick.model.ForeignKeyAction.Cascade
import java.time.Instant
class UsersTable(tag: Tag) extends Table[User](tag, "users") {
def id = column[Int]("id", O.PrimaryKey, O.AutoInc)
def gid = column[String]("gid", O.Unique)
def username = column[String]("username", O.Unique)
def passwordDigest = column[Option[String]]("password_digest")
override def * : ProvenShape[User] = (id.?, username, gid, passwordDigest).mapTo[User]
}
lazy val Users = TableQuery[UsersTable]
class AuthTokensTable(tag: Tag) extends Table[AuthToken](tag, "auth_tokens") {
def id = column[Int]("id", O.PrimaryKey, O.AutoInc)
def userId = column[Int]("user_id")
def token = column[String]("token")
def createdAt = column[Instant]("created_at")
def expiresAt = column[Instant]("expires_at")
override def * : ProvenShape[AuthToken] = (id.?, userId, token, createdAt, expiresAt).mapTo[AuthToken]
def user = foreignKey("users", userId, Users)(_.id, onDelete = Cascade)
}
lazy val AuthTokens = TableQuery[AuthTokensTable]
+9 -1
View File
@@ -5,11 +5,19 @@ version := "1.0-SNAPSHOT"
lazy val root = (project in file(".")).enablePlugins(PlayScala)
scalaVersion := "2.13.18"
scalaVersion := "3.8.4"
libraryDependencies += guice
libraryDependencies += "org.scalatestplus.play" %% "scalatestplus-play" % "7.0.2" % Test
// Source: https://mvnrepository.com/artifact/org.postgresql/postgresql
libraryDependencies += "org.postgresql" % "postgresql" % "42.7.13"
// Source: https://mvnrepository.com/artifact/org.playframework/play-slick
libraryDependencies += "org.playframework" %% "play-slick" % "6.2.0"
// Source: https://mvnrepository.com/artifact/org.playframework/play-slick-evolutions
libraryDependencies += "org.playframework" %% "play-slick-evolutions" % "6.2.0"
// Adds additional packages into Twirl
//TwirlKeys.templateImports += "me.artemis.controllers._"
+8
View File
@@ -0,0 +1,8 @@
name: 'neon-vertigo-api'
services:
db:
image: "postgres:18"
env_file:
- db.env
ports:
- "5432:5432"
+14
View File
@@ -1 +1,15 @@
# https://www.playframework.com/documentation/latest/Configuration
slick.dbs.default = {
profile = "slick.jdbc.PostgresProfile$"
db = {
dataSourceClass = "org.postgresql.ds.PGSimpleDataSource"
properties = {
serverName = "localhost"
portNumber = "5432"
databaseName = ${POSTGRES_DB}
user = ${POSTGRES_USER}
password = ${POSTGRES_PASSWORD}
}
}
}
+26
View File
@@ -0,0 +1,26 @@
-- Users and tokens
-- !Ups
CREATE TABLE users (
id BIGINT PRIMARY KEY GENERATED BY DEFAULT AS IDENTITY,
username VARCHAR UNIQUE NOT NULL,
gid VARCHAR UNIQUE NOT NULL,
password_digest VARCHAR
);
CREATE TABLE auth_tokens (
id BIGINT PRIMARY KEY GENERATED BY DEFAULT AS IDENTITY,
user_id BIGINT NOT NULL REFERENCES users(id) ON DELETE CASCADE,
token VARCHAR NOT NULL,
created_at TIMESTAMP NOT NULL,
expires_at TIMESTAMP NOT NULL
);
CREATE INDEX idx_auth_tokens_user_id_expires_at ON auth_tokens (user_id, expires_at);
-- !Downs
DROP INDEX idx_auth_tokens_user_id_expires_at;
DROP TABLE auth_tokens;
DROP TABLE users;
+3
View File
@@ -0,0 +1,3 @@
POSTGRES_DB=neon-vertigo-api
POSTGRES_USER=neon-vertigo
POSTGRES_PASSWORD=nvertigo