Skip to content

Commit

Permalink
Merge pull request #2355 from eikek/update-doobie
Browse files Browse the repository at this point in the history
Update doobie to 1.0.0-RC4
  • Loading branch information
mergify[bot] authored Nov 6, 2023
2 parents 3cb0172 + 4874739 commit e3f2fcb
Show file tree
Hide file tree
Showing 9 changed files with 71 additions and 35 deletions.
12 changes: 0 additions & 12 deletions modules/fts-psql/src/main/scala/docspell/ftspsql/DoobieMeta.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,9 @@ package docspell.ftspsql
import docspell.common._

import doobie._
import doobie.util.log.Success

trait DoobieMeta {

implicit val sqlLogging: LogHandler = LogHandler {
case e @ Success(_, _, _, _) =>
DoobieMeta.logger.debug("SQL " + e)
case e =>
DoobieMeta.logger.error(s"SQL Failure: $e")
}

implicit val metaIdent: Meta[Ident] =
Meta[String].timap(Ident.unsafe)(_.id)

Expand All @@ -29,7 +21,3 @@ trait DoobieMeta {
implicit val metaCollectiveId: Meta[CollectiveId] =
Meta[Long].timap(CollectiveId(_))(_.value)
}

object DoobieMeta {
private val logger = org.log4s.getLogger
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import fs2.Stream
import docspell.common._
import docspell.ftsclient._
import docspell.logging.Logger
import docspell.store.impl.DoobieLogging

import com.zaxxer.hikari.HikariDataSource
import doobie._
Expand Down Expand Up @@ -157,7 +158,8 @@ object PsqlFtsClient {
ds.setPassword(cfg.password.pass)
ds.setDriverClassName("org.postgresql.Driver")
}
xa = HikariTransactor[F](ds, connectEC)
logh = DoobieLogging[F](docspell.logging.getLogger[F])
xa = HikariTransactor[F](ds, connectEC, Some(logh))

pc = new PsqlFtsClient[F](cfg, xa)
} yield pc
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ package db.migration.common
import cats.effect.IO

import docspell.logging.Logger
import docspell.store.impl.DoobieLogging

import doobie.util.transactor.{Strategy, Transactor}
import org.flywaydb.core.api.migration.Context
Expand All @@ -18,7 +19,8 @@ trait TransactorSupport {
def logger: Logger[IO]

def mkTransactor(ctx: Context): Transactor[IO] = {
val xa = Transactor.fromConnection[IO](ctx.getConnection)
val logHandler = DoobieLogging[IO](logger)
val xa = Transactor.fromConnection[IO](ctx.getConnection, Some(logHandler))
logger.asUnsafe.info(s"Creating transactor for db migrations from connection: $xa")
Transactor.strategy.set(xa, Strategy.void) // transactions are handled by flyway
}
Expand Down
7 changes: 4 additions & 3 deletions modules/store/src/main/scala/docspell/store/Store.scala
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import fs2._
import fs2.io.file.Files

import docspell.store.file.{FileRepository, FileRepositoryConfig}
import docspell.store.impl.StoreImpl
import docspell.store.impl.{DoobieLogging, StoreImpl}

import com.zaxxer.hikari.HikariDataSource
import doobie._
Expand Down Expand Up @@ -60,8 +60,9 @@ object Store {
ds.setPassword(jdbc.password)
ds.setDriverClassName(jdbc.dbms.driverClass)
}
xa = HikariTransactor(ds, connectEC)
fr = FileRepository(xa, ds, fileRepoConfig, true)
logh = DoobieLogging[F](docspell.logging.getLogger[F])
xa = HikariTransactor[F](ds, connectEC, Some(logh))
fr = FileRepository(xa, ds, fileRepoConfig, withAttributeStore = true)
st = new StoreImpl[F](fr, jdbc, schemaCfg, ds, xa)
_ <- Resource.eval(st.migrate)
} yield st
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Copyright 2020 Eike K. & Contributors
*
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

package docspell.store.impl

import cats.Applicative
import cats.syntax.all._

import docspell.logging.Logger
import docspell.store.impl.DoobieLogging.LogLabel

import doobie.util.log
import doobie.util.log.LogHandler

final class DoobieLogging[F[_]: Applicative](logger: Logger[F]) extends LogHandler[F] {
override def run(logEvent: log.LogEvent): F[Unit] =
if (LogLabel.fromEvent(logEvent).contains(LogLabel.Silent)) ().pure[F]
else
logEvent match {
case log.Success(sql, args, _, exec, _) =>
logger.trace(s"SQL: $sql ($args) executed in $exec")

case log.ProcessingFailure(sql, args, _, _, _, failure) =>
logger.error(failure)(s"SQL processing failed: $sql ($args)")

case log.ExecFailure(sql, args, _, _, failure) =>
logger.error(failure)(s"SQL exec failed: $sql ($args)")
}
}

object DoobieLogging {

def apply[F[_]: Applicative](logger: Logger[F]): DoobieLogging[F] =
new DoobieLogging[F](logger)

sealed trait LogLabel extends Product {
lazy val name: String = productPrefix.toLowerCase
}
object LogLabel {
case object Silent extends LogLabel

val all: List[LogLabel] = List(Silent)

def fromString(str: String): Option[LogLabel] =
all.find(_.name.equalsIgnoreCase(str))

def fromEvent(e: log.LogEvent): Option[LogLabel] =
fromString(e.label)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,12 @@ import binny.BinaryId
import com.github.eikek.calev.CalEvent
import doobie._
import doobie.implicits.legacy.instant._
import doobie.util.log.Success
import emil.doobie.EmilDoobieMeta
import io.circe.Json
import io.circe.{Decoder, Encoder}
import io.circe.{Decoder, Encoder, Json}
import scodec.bits.ByteVector

trait DoobieMeta extends EmilDoobieMeta {

implicit val sqlLogging: LogHandler = LogHandler {
case e @ Success(_, _, _, _) =>
DoobieMeta.logger.trace(s"SQL: $e")
case e =>
DoobieMeta.logger.warn(s"SQL Failure: $e")
}

def jsonMeta[A](implicit d: Decoder[A], e: Encoder[A]): Meta[A] =
Meta[String].imap(str => str.parseJsonAs[A].fold(ex => throw ex, identity))(a =>
e.apply(a).noSpaces
Expand Down Expand Up @@ -180,12 +171,9 @@ trait DoobieMeta extends EmilDoobieMeta {
}

object DoobieMeta extends DoobieMeta {
import org.log4s._
private val logger = getLogger

private def parseJsonUnsafe(str: String): Json =
io.circe.parser
.parse(str)
.fold(throw _, identity)

}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ final class StoreImpl[F[_]: Async: Files](
) extends Store[F] {
private[this] val xa = transactor

val dbms = jdbc.dbms
val dbms: Db = jdbc.dbms

def createFileRepository(
cfg: FileRepositoryConfig,
Expand Down
6 changes: 4 additions & 2 deletions modules/store/src/main/scala/docspell/store/qb/DML.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ package docspell.store.qb

import cats.data.{NonEmptyList => Nel}

import docspell.store.impl.DoobieMeta
import docspell.store.impl.{DoobieLogging, DoobieMeta}
import docspell.store.qb.impl._

import doobie._
Expand All @@ -34,7 +34,9 @@ object DML extends DoobieMeta {
cols: Nel[Column[_]],
values: Fragment
): ConnectionIO[Int] =
insertFragment(table, cols, List(values)).update(LogHandler.nop).run
insertFragment(table, cols, List(values))
.updateWithLabel(DoobieLogging.LogLabel.Silent.name)
.run

def insertMany(
table: TableDef,
Expand Down
4 changes: 2 additions & 2 deletions project/Dependencies.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ object Dependencies {
val CirceGenericExtrasVersion = "0.14.3"
val CirceYamlVersion = "0.15.1"
val ClipboardJsVersion = "2.0.11"
val DoobieVersion = "1.0.0-RC2"
val EmilVersion = "0.13.0"
val DoobieVersion = "1.0.0-RC4"
val EmilVersion = "0.15.0"
val FlexmarkVersion = "0.64.8"
val FlywayVersion = "10.0.0"
val Fs2Version = "3.9.2"
Expand Down

0 comments on commit e3f2fcb

Please sign in to comment.