mirror of
https://github.com/TheAnachronism/docspell.git
synced 2025-06-22 02:18:26 +00:00
Update doobie to 1.0.0-RC4
This commit is contained in:
@ -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
|
||||
@ -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
|
||||
}
|
||||
|
@ -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._
|
||||
@ -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
|
||||
|
@ -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)
|
||||
}
|
||||
}
|
@ -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
|
||||
@ -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)
|
||||
|
||||
}
|
||||
|
@ -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,
|
||||
|
@ -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._
|
||||
@ -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,
|
||||
|
Reference in New Issue
Block a user