Skip to content

Commit

Permalink
Merge pull request #10 from RADAR-base/feature/graceful-no-table
Browse files Browse the repository at this point in the history
Return no observations when table does not (yet) exist
  • Loading branch information
pvannierop authored Oct 15, 2024
2 parents 342f2e5 + ea16020 commit b35cc33
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 9 deletions.
2 changes: 1 addition & 1 deletion buildSrc/src/main/kotlin/Versions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

@Suppress("ConstPropertyName")
object Versions {
const val project = "0.1.8"
const val project = "0.2.2"

const val java = 17

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ package org.radarbase.datadashboard.api.domain
import jakarta.inject.Provider
import jakarta.persistence.EntityManager
import jakarta.ws.rs.core.Context
import org.hibernate.exception.SQLGrammarException
import org.radarbase.datadashboard.api.domain.model.Observation
import org.radarbase.jersey.hibernate.HibernateRepository
import org.radarbase.jersey.service.AsyncCoroutineService
Expand All @@ -31,22 +32,40 @@ class ObservationRepositoryImpl(
@Context asyncService: AsyncCoroutineService,
) : HibernateRepository(em, asyncService), ObservationRepository {

private val tableExistsRegex = Regex("relation \".*\" does not exist")

override suspend fun getObservations(projectId: String, subjectId: String, topicId: String): List<Observation> {
logger.debug("Get observations in topic {} of subject {} in project {}", topicId, subjectId, projectId)

return transact {
createQuery(
"SELECT o FROM Observation o WHERE o.project = :projectId AND o.subject = :subjectId AND o.topic = :topicId ORDER BY o.observationTime DESC",
Observation::class.java,
).apply {
setParameter("projectId", projectId)
setParameter("subjectId", subjectId)
setParameter("topicId", topicId)
}.resultList
try {
createQuery(
"SELECT o FROM Observation o WHERE o.project = :projectId AND o.subject = :subjectId AND o.topic = :topicId ORDER BY o.observationTime DESC",
Observation::class.java,
).apply {
setParameter("projectId", projectId)
setParameter("subjectId", subjectId)
setParameter("topicId", topicId)
}.resultList
} catch (ex: SQLGrammarException) {
if (tableDoesNotExist(ex)) {
logger.info(
"Observations table has not been created by JDBC connector yet " +
"(will be created upon first data ingestion). Returning empty result...",
)
emptyList()
} else {
throw ex
}
}
}
}

companion object {
private val logger = LoggerFactory.getLogger(ObservationRepositoryImpl::class.java)
}

private fun tableDoesNotExist(ex: SQLGrammarException): Boolean {
return tableExistsRegex.containsMatchIn(ex.message ?: "")
}
}

0 comments on commit b35cc33

Please sign in to comment.