From 2844f1c997fa699e3d3544278d15f8f58e986748 Mon Sep 17 00:00:00 2001 From: eikek Date: Sun, 14 Aug 2022 00:30:05 +0200 Subject: [PATCH] Fix selectNextGroup query for an empty job table --- .../main/scala/docspell/scheduler/impl/QJob.scala | 2 +- .../scala/docspell/scheduler/impl/QJobTest.scala | 14 ++++++++++++++ .../src/main/scala/docspell/store/qb/DML.scala | 8 +++++--- 3 files changed, 20 insertions(+), 4 deletions(-) diff --git a/modules/scheduler/impl/src/main/scala/docspell/scheduler/impl/QJob.scala b/modules/scheduler/impl/src/main/scala/docspell/scheduler/impl/QJob.scala index c965fb48..7b7fd45a 100644 --- a/modules/scheduler/impl/src/main/scala/docspell/scheduler/impl/QJob.scala +++ b/modules/scheduler/impl/src/main/scala/docspell/scheduler/impl/QJob.scala @@ -135,7 +135,7 @@ object QJob { .trace( s"nextGroupQuery: $frag (now=${now.toMillis}, pause=${initialPause.millis})" ) *> - groups.build.query[Ident].option + frag.query[Option[Ident]].unique } private def stuckTriggerValue(t: RJob.Table, initialPause: Duration, now: Timestamp) = diff --git a/modules/scheduler/impl/src/test/scala/docspell/scheduler/impl/QJobTest.scala b/modules/scheduler/impl/src/test/scala/docspell/scheduler/impl/QJobTest.scala index ff6151d8..1f953b63 100644 --- a/modules/scheduler/impl/src/test/scala/docspell/scheduler/impl/QJobTest.scala +++ b/modules/scheduler/impl/src/test/scala/docspell/scheduler/impl/QJobTest.scala @@ -12,6 +12,7 @@ import java.util.concurrent.atomic.AtomicLong import cats.syntax.all._ import docspell.common._ +import docspell.store.qb.{Condition, DML} import docspell.store.records.{RJob, RJobGroupUse} import docspell.store.{DatabaseTest, Db} @@ -42,6 +43,19 @@ class QJobTest extends DatabaseTest { None ) + Db.all.toList.foreach { db => + test(s"selectNextGroup on empty table ($db)") { + val store = createStore(db) + val nextGroup = for { + _ <- store.transact(RJobGroupUse.deleteAll) + _ <- store.transact(DML.delete(RJob.T, Condition.unit)) + next <- store.transact(QJob.selectNextGroup(worker, nowTs, initialPause)) + } yield next + + nextGroup.assertEquals(None) + } + } + Db.all.toList.foreach { db => test(s"set group must insert or update ($db)") { val store = createStore(db) diff --git a/modules/store/src/main/scala/docspell/store/qb/DML.scala b/modules/store/src/main/scala/docspell/store/qb/DML.scala index e9b3c790..f53e3dc7 100644 --- a/modules/store/src/main/scala/docspell/store/qb/DML.scala +++ b/modules/store/src/main/scala/docspell/store/qb/DML.scala @@ -20,9 +20,11 @@ object DML extends DoobieMeta { def delete(table: TableDef, cond: Condition): ConnectionIO[Int] = deleteFragment(table, cond).update.run - def deleteFragment(table: TableDef, cond: Condition): Fragment = - fr"DELETE FROM" ++ FromExprBuilder.buildTable(table) ++ fr" WHERE" ++ ConditionBuilder - .build(cond) + def deleteFragment(table: TableDef, cond: Condition): Fragment = { + val delete = fr"DELETE FROM" ++ FromExprBuilder.buildTable(table) + if (cond == Condition.unit) delete + else delete ++ fr"WHERE" ++ ConditionBuilder.build(cond) + } def insert(table: TableDef, cols: Nel[Column[_]], values: Fragment): ConnectionIO[Int] = insertFragment(table, cols, List(values)).update.run