Fix job query for H2

Unfortunately, the new h2 version has some regressions related to
CTEs. The query selecting the next group failed only for H2 after the
update. The query has been rewritten to not use union on CTE tables.
The weird thing was that the error only occured using bind values and
was not reproducible with "just string" SQL in the h2 console.

The QJobTest are now running on all databases.
This commit is contained in:
eikek
2022-08-12 16:30:32 +02:00
parent 0a3ac9f121
commit 5bbe073bf3
6 changed files with 109 additions and 88 deletions

View File

@ -300,11 +300,6 @@ object RJob {
def setProgress(jobId: Ident, perc: Int): ConnectionIO[Int] =
DML.update(T, T.id === jobId, DML.set(T.progress.setTo(perc)))
def selectWaiting: ConnectionIO[Option[RJob]] = {
val sql = run(select(T.all), from(T), T.state === JobState.waiting)
sql.query[RJob].to[Vector].map(_.headOption)
}
def selectGroupInState(states: NonEmptyList[JobState]): ConnectionIO[Vector[Ident]] = {
val sql =
Select(select(T.group), from(T), T.state.in(states)).orderBy(T.group)

View File

@ -7,12 +7,10 @@
package docspell.store
import java.util.UUID
import cats.effect._
import cats.syntax.option._
import docspell.common._
import docspell.logging.TestLoggingConfig
import com.dimafeng.testcontainers.munit.fixtures.TestContainersFixtures
import com.dimafeng.testcontainers.{
JdbcDatabaseContainer,
@ -20,6 +18,7 @@ import com.dimafeng.testcontainers.{
PostgreSQLContainer
}
import doobie._
import fs2.io.file.{Files, Path}
import munit.CatsEffectSuite
import org.testcontainers.utility.DockerImageName
@ -55,6 +54,15 @@ trait DatabaseTest
}
)
lazy val h2FileDataSource = ResourceSuiteLocalFixture(
"h2FileDataSource",
for {
file <- Files[IO].tempFile(Path("target").some, "h2-test-", ".db", None)
jdbc = StoreFixture.fileDB(file)
res <- StoreFixture.dataSource(jdbc).map(ds => (jdbc, ds))
} yield res
)
lazy val newH2DataSource = ResourceFixture(for {
jdbc <- Resource.eval(IO(StoreFixture.memoryDB(UUID.randomUUID().toString)))
ds <- StoreFixture.dataSource(jdbc)
@ -84,9 +92,18 @@ trait DatabaseTest
} yield store
)
lazy val h2FileStore = ResourceSuiteLocalFixture(
"h2FileStore",
for {
t <- Resource.eval(IO(h2FileDataSource()))
store <- StoreFixture.store(t._2, t._1)
} yield store
)
def postgresAll = List(postgresCnt, pgDataSource, pgStore)
def mariaDbAll = List(mariadbCnt, mariaDataSource, mariaStore)
def h2All = List(h2DataSource, h2Store)
def h2Memory = List(h2DataSource, h2Store)
def h2File = List(h2FileDataSource, h2FileStore)
}
object DatabaseTest {

View File

@ -7,15 +7,13 @@
package docspell.store
import javax.sql.DataSource
import cats.effect._
import docspell.common.LenientUri
import docspell.store.file.{FileRepository, FileRepositoryConfig}
import docspell.store.impl.StoreImpl
import docspell.store.migrate.FlywayMigrate
import doobie._
import fs2.io.file.Path
import munit._
import org.h2.jdbcx.{JdbcConnectionPool, JdbcDataSource}
import org.mariadb.jdbc.MariaDbDataSource
@ -55,6 +53,15 @@ object StoreFixture {
""
)
def fileDB(file: Path): JdbcConfig =
JdbcConfig(
LenientUri.unsafe(
s"jdbc:h2:file://${file.absolute.toString};MODE=PostgreSQL;DATABASE_TO_LOWER=TRUE"
),
"sa",
""
)
def dataSource(jdbc: JdbcConfig): Resource[IO, JdbcConnectionPool] = {
def jdbcConnPool =
jdbc.dbms match {
@ -115,5 +122,4 @@ object StoreFixture {
case None =>
IO.raiseError(new Exception(s"Resource not found: $resourceName"))
}
}

View File

@ -27,7 +27,7 @@ import doobie._
class TempFtsOpsTest extends DatabaseTest {
private[this] val logger = docspell.logging.getLogger[IO]
override def munitFixtures = postgresAll ++ mariaDbAll ++ h2All
override def munitFixtures = postgresAll ++ mariaDbAll ++ h2Memory
def id(str: String): Ident = Ident.unsafe(str)