Skip to content

Commit

Permalink
Merge pull request #1364 from AugustNagro/magnum-style
Browse files Browse the repository at this point in the history
Update Magnum to 1.3.1 and use SqlLogger.logSlowQueries
  • Loading branch information
adamw authored Dec 6, 2024
2 parents 6dce6cc + 42cdb44 commit d96b1f8
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import org.flywaydb.core.Flyway

import scala.concurrent.duration.*
import Magnum.*
import com.augustnagro.magnum.connect
import com.augustnagro.magnum.{SqlLogger, Transactor, connect}
import com.softwaremill.bootzooka.config.Sensitive
import com.softwaremill.bootzooka.infrastructure.DB.LeftException
import com.softwaremill.bootzooka.logging.Logging
Expand All @@ -19,16 +19,21 @@ import scala.util.NotGiven
import scala.util.control.{NoStackTrace, NonFatal}

class DB(dataSource: DataSource & Closeable) extends Logging with AutoCloseable:
private val transactor = Transactor(
dataSource = dataSource,
sqlLogger = SqlLogger.logSlowQueries(200.millis)
)

/** Runs `f` in a transaction. The transaction is commited if the result is a [[Right]], and rolled back otherwise. */
def transactEither[E, T](f: DbTx ?=> Either[E, T])(using IO): Either[E, T] =
try com.augustnagro.magnum.transact(dataSource)(Right(f.fold(e => throw LeftException(e), identity)))
catch case e: LeftException[E] => Left(e.left)
try com.augustnagro.magnum.transact(transactor)(Right(f.fold(e => throw LeftException(e), identity)))
catch case e: LeftException[?] => Left(e.asInstanceOf[LeftException[E]].left)

/** Runs `f` in a transaction. The result cannot be an `Either`, as then [[transactEither]] should be used. The transaction is commited if
* no exception is thrown.
*/
def transact[T](f: DbTx ?=> T)(using NotGiven[T <:< Either[_, _]], IO): T =
com.augustnagro.magnum.transact(dataSource)(f)
com.augustnagro.magnum.transact(transactor)(f)

override def close(): Unit = IO.unsafe(dataSource.close())

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,4 @@ object Magnum extends Logging:
given DbCodec[LowerCased] = DbCodec.StringCodec.biMap(_.toLowerCased, _.toString)

// proxies to the magnum functions/types, so that we can have only one import
export com.augustnagro.magnum.sql
type DbTx = com.augustnagro.magnum.DbTx
type DbCon = com.augustnagro.magnum.DbCon
type DbCodec[E] = com.augustnagro.magnum.DbCodec[E]

/** Logs the SQL queries which are slow or end up in an exception. */
// TODO: https://github.com/AugustNagro/magnum/issues/32
// private val SlowThreshold = 200.millis
// implicit def doobieLogHandler[M[_]: Sync]: LogHandler[M] = new LogHandler[M] {
// override def run(logEvent: LogEvent): M[Unit] = Sync[M].delay(
// logEvent match {
// case Success(sql, _, _, exec, processing) =>
// if (exec > SlowThreshold || processing > SlowThreshold) {
// logger.warn(s"Slow query (execution: $exec, processing: $processing): $sql")
// }
//
// case ProcessingFailure(sql, args, _, exec, processing, failure) =>
// logger.error(s"Processing failure (execution: $exec, processing: $processing): $sql | args: $args", failure)
//
// case ExecFailure(sql, args, _, exec, failure) =>
// logger.error(s"Execution failure (execution: $exec): $sql | args: $args", failure)
// }
// )
// }
export com.augustnagro.magnum.{sql, DbTx, DbCon, DbCodec}
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,3 @@ class ApiKeyService(apiKeyModel: ApiKeyModel, idGenerator: IdGenerator, clock: C
def invalidateAllForUser(userId: Id[User])(using DbTx): Unit =
logger.debug(s"Invalidating all api keys for user $userId")
apiKeyModel.deleteAllForUser(userId)

Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import com.softwaremill.bootzooka.logging.Logging
import com.softwaremill.bootzooka.user.User
import com.softwaremill.bootzooka.util.*
import com.softwaremill.bootzooka.util.Strings.Id
import ox.{IO, either, sleep}
import ox.either.{fail, ok}
import ox.{IO, sleep}

import java.security.SecureRandom
import java.time.Instant
Expand All @@ -20,7 +20,7 @@ class Auth[T](authTokenOps: AuthTokenOps[T], db: DB, clock: Clock) extends Loggi
private val random = SecureRandom.getInstance("NativePRNGNonBlocking")

/** Authenticates using the given authentication token. If the token is invalid, a [[Fail.Unauthorized]] error is returned. Otherwise,
* returns the id of the authenticated user .
* returns the id of the authenticated user .
*/
def apply(id: Id[T])(using IO): Either[Fail.Unauthorized, Id[User]] =
db.transact(authTokenOps.findById(id)) match {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ class UserModel:
private val userRepo = Repo[User, User, Id[User]]
private val u = TableInfo[User, User, Id[User]]

def insert(user: User)(using DbTx): Unit = userRepo.insert(user)
def findById(id: Id[User])(using DbTx): Option[User] = userRepo.findById(id)
export userRepo.{insert, findById}

def findByEmail(email: LowerCased)(using DbTx): Option[User] = findBy(
Spec[User].where(sql"${u.emailLowerCase} = $email")
)
Expand Down
3 changes: 2 additions & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ val tapirVersion = "1.11.4"
val oxVersion = "0.3.9"

val dbDependencies = Seq(
"com.augustnagro" %% "magnum" % "1.3.0",
"com.augustnagro" %% "magnum" % "1.3.1",
"org.postgresql" % "postgresql" % "42.7.4",
"com.zaxxer" % "HikariCP" % "6.0.0",
"org.flywaydb" % "flyway-database-postgresql" % "10.20.0"
Expand Down Expand Up @@ -45,6 +45,7 @@ val jsonDependencies = Seq(
val loggingDependencies = Seq(
"ch.qos.logback" % "logback-classic" % "1.5.11",
"com.softwaremill.ox" %% "mdc-logback" % oxVersion,
"org.slf4j" % "slf4j-jdk-platform-logging" % "2.0.7" % Runtime,
"org.codehaus.janino" % "janino" % "3.1.12" % Runtime,
"net.logstash.logback" % "logstash-logback-encoder" % "8.0" % Runtime
)
Expand Down

0 comments on commit d96b1f8

Please sign in to comment.