Skip to content

Commit

Permalink
Merge branch 'generate-openapi-spec' of github.com:softwaremill/bootz…
Browse files Browse the repository at this point in the history
…ooka into generate-openapi-spec
  • Loading branch information
Katarzyna Kozlowska committed Jan 10, 2025
2 parents 2d9a7e8 + d93c602 commit 6f8728d
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 38 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,6 @@ work.txt
.vscode
metals.sbt
.bsp

metals.sbt
.vscode
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]): Either[E, T] =
try com.augustnagro.magnum.transact(dataSource)(Right(f.fold(e => throw LeftException(e), identity)))
try com.augustnagro.magnum.transact(transactor)(Right(f.fold(e => throw LeftException(e), identity)))
catch case e: LeftException[E] @unchecked => Left(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[?, ?]]): T =
com.augustnagro.magnum.transact(dataSource)(f)
com.augustnagro.magnum.transact(transactor)(f)

override def close(): Unit = 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 @@ -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
15 changes: 8 additions & 7 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,17 @@ import scala.sys.process.Process
import complete.DefaultParsers._

val password4jVersion = "1.8.2"
val sttpVersion = "3.9.8"
val sttpVersion = "3.10.1"
val tapirVersion = "1.11.6"
val oxVersion = "0.5.1"
val otelVersion = "1.42.1"
val otelVersion = "1.43.0"
val otelInstrumentationVersion = "2.8.0-alpha"

val dbDependencies = Seq(
"com.augustnagro" %% "magnum" % "1.2.1",
"com.augustnagro" %% "magnum" % "1.3.1",
"org.postgresql" % "postgresql" % "42.7.4",
"com.zaxxer" % "HikariCP" % "5.1.0",
"org.flywaydb" % "flyway-database-postgresql" % "10.18.0"
"com.zaxxer" % "HikariCP" % "6.0.0",
"org.flywaydb" % "flyway-database-postgresql" % "10.20.0"
)

val httpDependencies = Seq(
Expand All @@ -41,15 +41,16 @@ val observabilityDependencies = Seq(
)

val jsonDependencies = Seq(
"com.github.plokhotnyuk.jsoniter-scala" %% "jsoniter-scala-macros" % "2.30.11",
"com.github.plokhotnyuk.jsoniter-scala" %% "jsoniter-scala-macros" % "2.30.15",
"com.softwaremill.sttp.tapir" %% "tapir-jsoniter-scala" % tapirVersion,
"com.softwaremill.sttp.client3" %% "jsoniter" % sttpVersion
)

val loggingDependencies = Seq(
"ch.qos.logback" % "logback-classic" % "1.5.8",
"ch.qos.logback" % "logback-classic" % "1.5.11",
"org.slf4j" % "jul-to-slf4j" % "1.7.36", // forward e.g. otel logs which use JUL to SLF4J
"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
4 changes: 2 additions & 2 deletions project/plugins.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ addSbtPlugin("com.eed3si9n" % "sbt-buildinfo" % "0.12.0")

addSbtPlugin("io.spray" % "sbt-revolver" % "0.10.0")

addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "2.2.0")
addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "2.3.0")

addSbtPlugin("com.github.sbt" % "sbt-native-packager" % "1.10.4")

addSbtPlugin("com.github.sbt" % "sbt-git" % "2.0.1")
addSbtPlugin("com.github.sbt" % "sbt-git" % "2.1.0")

addSbtPlugin("org.scalameta" % "sbt-scalafmt" % "2.5.2")

0 comments on commit 6f8728d

Please sign in to comment.