Adopt backend to collective-id

This commit is contained in:
eikek
2022-08-04 11:03:27 +02:00
parent 26d7c91266
commit 53d92c4a26
94 changed files with 1468 additions and 833 deletions

View File

@ -0,0 +1,33 @@
drop index "ftspsql_search_ftsidx";
drop index "ftpsql_search_item_idx";
drop index "ftpsql_search_attach_idx";
drop index "ftpsql_search_folder_idx";
drop table "ftspsql_search" cascade;
create table "ftspsql_search"(
"id" varchar(254) not null primary key,
"item_id" varchar(254) not null,
"collective" bigint not null,
"lang" varchar(254) not null,
"attach_id" varchar(254),
"folder_id" varchar(254),
"updated_at" timestamptz not null default current_timestamp,
--- content columns
"attach_name" text,
"attach_content" text,
"item_name" text,
"item_notes" text,
--- index column
"fts_config" regconfig not null,
"text_index" tsvector
generated always as (
setweight(to_tsvector("fts_config", coalesce("attach_name", '')), 'B') ||
setweight(to_tsvector("fts_config", coalesce("item_name", '')), 'B') ||
setweight(to_tsvector("fts_config", coalesce("attach_content", '')), 'C') ||
setweight(to_tsvector("fts_config", coalesce("item_notes", '')), 'C')) stored
);
create index "ftspsql_search_ftsidx" on "ftspsql_search" using GIN ("text_index");
create index "ftpsql_search_item_idx" on "ftspsql_search"("item_id");
create index "ftpsql_search_attach_idx" on "ftspsql_search"("attach_id");
create index "ftpsql_search_folder_idx" on "ftspsql_search"("folder_id");

View File

@ -26,6 +26,8 @@ trait DoobieMeta {
implicit val metaLanguage: Meta[Language] =
Meta[String].timap(Language.unsafe)(_.iso3)
implicit val metaCollectiveId: Meta[CollectiveId] =
Meta[Long].timap(CollectiveId(_))(_.value)
}
object DoobieMeta {

View File

@ -7,14 +7,13 @@
package docspell.ftspsql
import cats.syntax.all._
import docspell.common.{Ident, Language}
import docspell.common.{CollectiveId, Ident, Language}
import docspell.ftsclient.TextData
final case class FtsRecord(
id: Ident,
itemId: Ident,
collective: Ident,
collective: CollectiveId,
language: Language,
attachId: Option[Ident],
folderId: Option[Ident],

View File

@ -18,6 +18,12 @@ import doobie.implicits._
object FtsRepository extends DoobieMeta {
val table = fr"ftspsql_search"
def containsData: ConnectionIO[Boolean] =
sql"select id from $table limit 1".query[String].option.map(_.isDefined)
def containsNoData: ConnectionIO[Boolean] =
containsData.map(!_)
def searchSummary(pq: PgQueryParser, rn: RankNormalization)(
q: FtsQuery
): ConnectionIO[SearchSummary] = {
@ -139,7 +145,7 @@ object FtsRepository extends DoobieMeta {
def updateFolder(
itemId: Ident,
collective: Ident,
collective: CollectiveId,
folder: Option[Ident]
): ConnectionIO[Int] =
(sql"UPDATE $table" ++
@ -155,7 +161,7 @@ object FtsRepository extends DoobieMeta {
def deleteAll: ConnectionIO[Int] =
sql"DELETE FROM $table".update.run
def delete(collective: Ident): ConnectionIO[Int] =
def delete(collective: CollectiveId): ConnectionIO[Int] =
sql"DELETE FROM $table WHERE collective = $collective".update.run
def resetAll: ConnectionIO[Int] = {

View File

@ -46,6 +46,16 @@ final class PsqlFtsClient[F[_]: Sync](cfg: PsqlConfig, xa: Transactor[F])
engine,
"initialize",
DbMigration[F](cfg).run.as(FtsMigration.Result.WorkDone)
),
FtsMigration(
1,
engine,
"Re-Index if empty",
FtsRepository.containsNoData
.transact(xa)
.map(empty =>
if (empty) FtsMigration.Result.IndexAll else FtsMigration.Result.WorkDone
)
)
)
)
@ -104,11 +114,11 @@ final class PsqlFtsClient[F[_]: Sync](cfg: PsqlConfig, xa: Transactor[F])
def updateFolder(
logger: Logger[F],
itemId: Ident,
collective: Ident,
collective: CollectiveId,
folder: Option[Ident]
): F[Unit] =
logger.debug(s"Update folder '${folder
.map(_.id)}' in fts for collective ${collective.id} and item ${itemId.id}") *>
.map(_.id)}' in fts for collective ${collective.value} and item ${itemId.id}") *>
FtsRepository.updateFolder(itemId, collective, folder).transact(xa).void
def removeItem(logger: Logger[F], itemId: Ident): F[Unit] =
@ -123,8 +133,8 @@ final class PsqlFtsClient[F[_]: Sync](cfg: PsqlConfig, xa: Transactor[F])
logger.info(s"Deleting complete FTS index") *>
FtsRepository.deleteAll.transact(xa).void
def clear(logger: Logger[F], collective: Ident): F[Unit] =
logger.info(s"Deleting index for collective ${collective.id}") *>
def clear(logger: Logger[F], collective: CollectiveId): F[Unit] =
logger.info(s"Deleting index for collective ${collective.value}") *>
FtsRepository.delete(collective).transact(xa).void
}

View File

@ -51,8 +51,8 @@ trait PgFixtures {
self.transact(client.transactor)
}
val collective1 = ident("coll1")
val collective2 = ident("coll2")
val collective1 = CollectiveId(1)
val collective2 = CollectiveId(2)
val itemData: TextData.Item =
TextData.Item(