mirror of
https://github.com/TheAnachronism/docspell.git
synced 2025-06-04 14:15:59 +00:00
Remove fts_migration table
It is now stored it SOLR instead.
This commit is contained in:
parent
5205ee0623
commit
3ee0846e19
@ -1,6 +1,6 @@
|
||||
package docspell.joex.fts
|
||||
|
||||
import cats.data.{Kleisli, OptionT}
|
||||
import cats.data.Kleisli
|
||||
import cats.effect._
|
||||
import cats.implicits._
|
||||
import cats.{Applicative, FlatMap, Traverse}
|
||||
@ -8,13 +8,13 @@ import cats.{Applicative, FlatMap, Traverse}
|
||||
import docspell.common._
|
||||
import docspell.ftsclient._
|
||||
import docspell.joex.Config
|
||||
import docspell.store.records.RFtsMigration
|
||||
import docspell.store.{AddResult, Store}
|
||||
import docspell.store.Store
|
||||
|
||||
/** Migrating the index from the previous version to this version.
|
||||
*
|
||||
* The sql database stores the outcome of a migration task. If this
|
||||
* task has already been applied, it is skipped.
|
||||
* The migration asks the fulltext search client for a list of
|
||||
* migration tasks to run. It may be empty when there is no migration
|
||||
* required.
|
||||
*/
|
||||
case class Migration[F[_]](
|
||||
version: Int,
|
||||
@ -35,41 +35,16 @@ object Migration {
|
||||
logger: Logger[F]
|
||||
): Kleisli[F, List[Migration[F]], Unit] = {
|
||||
val ctx = FtsContext(cfg, store, fts, logger)
|
||||
Kleisli(migs => Traverse[List].sequence(migs.map(applySingle[F](ctx))).map(_ => ()))
|
||||
Kleisli { migs =>
|
||||
if (migs.isEmpty) logger.info("No fulltext search migrations to run.")
|
||||
else Traverse[List].sequence(migs.map(applySingle[F](ctx))).map(_ => ())
|
||||
}
|
||||
}
|
||||
|
||||
def applySingle[F[_]: Effect](ctx: FtsContext[F])(m: Migration[F]): F[Unit] = {
|
||||
val insertRecord: F[Option[RFtsMigration]] =
|
||||
for {
|
||||
rec <- RFtsMigration.create(m.version, m.engine, m.description)
|
||||
res <- ctx.store.add(
|
||||
RFtsMigration.insert(rec),
|
||||
RFtsMigration.exists(m.version, m.engine)
|
||||
)
|
||||
ret <- res match {
|
||||
case AddResult.Success => rec.some.pure[F]
|
||||
case AddResult.EntityExists(_) => None.pure[F]
|
||||
case AddResult.Failure(ex) => Effect[F].raiseError(ex)
|
||||
}
|
||||
} yield ret
|
||||
|
||||
(for {
|
||||
_ <- OptionT.liftF(ctx.logger.info(s"Apply ${m.version}/${m.description}"))
|
||||
rec <- OptionT(insertRecord)
|
||||
res <- OptionT.liftF(m.task.run(ctx).attempt)
|
||||
ret <- OptionT.liftF(res match {
|
||||
case Right(()) => ().pure[F]
|
||||
case Left(ex) =>
|
||||
ctx.logger.error(ex)(
|
||||
s"Applying index migration ${m.version}/${m.description} failed"
|
||||
) *>
|
||||
ctx.store.transact(RFtsMigration.deleteById(rec.id)) *> Effect[F]
|
||||
.raiseError[Unit](
|
||||
ex
|
||||
)
|
||||
})
|
||||
} yield ret).getOrElseF(
|
||||
ctx.logger.info(s"Migration ${m.version}/${m.description} already applied.")
|
||||
)
|
||||
for {
|
||||
_ <- ctx.logger.info(s"Apply ${m.version}/${m.description}")
|
||||
_ <- m.task.run(ctx)
|
||||
} yield ()
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1 @@
|
||||
DROP TABLE "fts_migration";
|
@ -0,0 +1 @@
|
||||
DROP TABLE `fts_migration`;
|
@ -0,0 +1 @@
|
||||
DROP TABLE "fts_migration";
|
@ -1,68 +0,0 @@
|
||||
package docspell.store.records
|
||||
|
||||
import cats.data.NonEmptyList
|
||||
import cats.effect._
|
||||
import cats.implicits._
|
||||
|
||||
import docspell.common._
|
||||
import docspell.store.qb.DSL._
|
||||
import docspell.store.qb._
|
||||
|
||||
import doobie._
|
||||
import doobie.implicits._
|
||||
|
||||
final case class RFtsMigration(
|
||||
id: Ident,
|
||||
version: Int,
|
||||
ftsEngine: Ident,
|
||||
description: String,
|
||||
created: Timestamp
|
||||
)
|
||||
|
||||
object RFtsMigration {
|
||||
|
||||
def create[F[_]: Sync](
|
||||
version: Int,
|
||||
ftsEngine: Ident,
|
||||
description: String
|
||||
): F[RFtsMigration] =
|
||||
for {
|
||||
newId <- Ident.randomId[F]
|
||||
now <- Timestamp.current[F]
|
||||
} yield RFtsMigration(newId, version, ftsEngine, description, now)
|
||||
|
||||
final case class Table(alias: Option[String]) extends TableDef {
|
||||
val tableName = "fts_migration"
|
||||
|
||||
val id = Column[Ident]("id", this)
|
||||
val version = Column[Int]("version", this)
|
||||
val ftsEngine = Column[Ident]("fts_engine", this)
|
||||
val description = Column[String]("description", this)
|
||||
val created = Column[Timestamp]("created", this)
|
||||
|
||||
val all = NonEmptyList.of[Column[_]](id, version, ftsEngine, description, created)
|
||||
}
|
||||
|
||||
val T = Table(None)
|
||||
def as(alias: String): Table =
|
||||
Table(Some(alias))
|
||||
|
||||
def insert(v: RFtsMigration): ConnectionIO[Int] =
|
||||
DML
|
||||
.insertFragment(
|
||||
T,
|
||||
T.all,
|
||||
Seq(fr"${v.id},${v.version},${v.ftsEngine},${v.description},${v.created}")
|
||||
)
|
||||
.updateWithLogHandler(LogHandler.nop)
|
||||
.run
|
||||
|
||||
def exists(vers: Int, engine: Ident): ConnectionIO[Boolean] =
|
||||
run(select(count(T.id)), from(T), T.version === vers && T.ftsEngine === engine)
|
||||
.query[Int]
|
||||
.unique
|
||||
.map(_ > 0)
|
||||
|
||||
def deleteById(rId: Ident): ConnectionIO[Int] =
|
||||
DML.delete(T, T.id === rId)
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user