Skip to content

Commit

Permalink
EVA-PIMP use generic dml query for deletes and no returning inserts (#…
Browse files Browse the repository at this point in the history
…110)

* EVA-PIMP use generic dml query for deletes and no returning inserts

* cosmetics

* fix patching executor
  • Loading branch information
Rattenkrieg committed Nov 18, 2023
1 parent 3d032a2 commit b67e040
Show file tree
Hide file tree
Showing 11 changed files with 44 additions and 62 deletions.
2 changes: 1 addition & 1 deletion buildSrc/src/main/kotlin/CI.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import java.lang.Boolean.parseBoolean
object Ci {

private const val SNAPSHOT_BASE = "0.5.0"
private const val RELEASE_VERSION = "0.3.7"
private const val RELEASE_VERSION = "0.4.1"
private val githubSha = System.getenv("GITHUB_SHA") ?: "latest"

val publishRelease = System.getProperty("release", "true").let(::parseBoolean)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package com.razz.eva.persistence.jdbc.executor
import com.razz.eva.persistence.ConnectionMode.REQUIRE_EXISTING
import com.razz.eva.persistence.executor.QueryExecutor
import com.razz.eva.persistence.jdbc.JdbcTransactionManager
import org.jooq.DMLQuery
import org.jooq.DSLContext
import org.jooq.DeleteQuery
import org.jooq.Param
import org.jooq.Query
import org.jooq.Record
Expand Down Expand Up @@ -42,30 +42,33 @@ class JdbcQueryExecutor(
}
}

override suspend fun <R : Record> executeDelete(
override suspend fun <R : Record> executeQuery(
dslContext: DSLContext,
jooqQuery: DeleteQuery<R>,
table: Table<R>,
jooqQuery: DMLQuery<R>,
): Int {
return transactionManager.inTransaction(REQUIRE_EXISTING) { connection ->
DSL.using(connection, dslContext.settings()).run {
preparedQuery(jooqQuery).execute()
execute(
render(jooqQuery),
*extractParams(jooqQuery)
.values
.filterNot(Param<*>::isInline)
.toTypedArray()
)
}
}
}

private fun <R : Record> DSLContext.preparedQuery(
jooqQuery: Query,
table: Table<R>,
): List<R> = preparedQuery(jooqQuery).coerce(table).fetch()

private fun DSLContext.preparedQuery(jooqQuery: Query) = resultQuery(
): List<R> = resultQuery(
render(jooqQuery),
*extractParams(jooqQuery)
.values
.filterNot(Param<*>::isInline)
.toTypedArray()
)
).coerce(table).fetch()

override fun getConstraintName(e: DataAccessException): String? {
return e.getCause(PSQLException::class.java)?.serverErrorMessage?.constraint
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,9 @@ class JdbcQueryExecutorSpec : BehaviorSpec({
When("Principal calls execute delete without context") {

val storeRun = suspend {
jdbcExecutor.executeDelete(
jdbcExecutor.executeQuery(
dslContext,
delete,
DSL.table("cool_table"),
)
}

Expand Down Expand Up @@ -166,10 +165,9 @@ class JdbcQueryExecutorSpec : BehaviorSpec({
When("Principal calls execute delete with context") {

withContext(Dispatchers.IO + JdbcConnectionElement(connection)) {
jdbcExecutor.executeDelete(
jdbcExecutor.executeQuery(
dslContext,
delete,
DSL.table("cool_table"),
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import io.vertx.sqlclient.RowSet
import io.vertx.sqlclient.SqlResult
import io.vertx.sqlclient.impl.ArrayTuple
import org.jooq.Converter
import org.jooq.DMLQuery
import org.jooq.DSLContext
import org.jooq.DeleteQuery
import org.jooq.JSON
import org.jooq.JSONB
import org.jooq.Query
Expand Down Expand Up @@ -55,10 +55,9 @@ class VertxQueryExecutor(
}
}

override suspend fun <R : Record> executeDelete(
override suspend fun <R : Record> executeQuery(
dslContext: DSLContext,
jooqQuery: DeleteQuery<R>,
table: Table<R>,
jooqQuery: DMLQuery<R>,
): Int {
return transactionManager.inTransaction(REQUIRE_EXISTING) { connection ->
connection.preparedQuery(dslContext.renderNamedParams(jooqQuery))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,10 +128,9 @@ class VertxQueryExecutorSpec : BehaviorSpec({
When("Principal calls execute store without context") {

val storeRun = suspend {
vertxExecutor.executeDelete(
vertxExecutor.executeQuery(
dslContext,
delete,
DSL.table("cool_table"),
)
}

Expand Down Expand Up @@ -204,10 +203,9 @@ class VertxQueryExecutorSpec : BehaviorSpec({
When("Principal calls execute delete with context") {

withContext(Dispatchers.IO + VertxConnectionElement(connection)) {
vertxExecutor.executeDelete(
vertxExecutor.executeQuery(
dslContext,
delete,
DSL.table("cool_table"),
)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.razz.eva.persistence.executor

import org.jooq.DMLQuery
import org.jooq.DSLContext
import org.jooq.DeleteQuery
import org.jooq.Record
import org.jooq.Select
import org.jooq.StoreQuery
Expand All @@ -22,10 +22,9 @@ interface QueryExecutor {
table: Table<ROUT>,
): List<ROUT>

suspend fun <R : Record> executeDelete(
suspend fun <R : Record> executeQuery(
dslContext: DSLContext,
jooqQuery: DeleteQuery<R>,
table: Table<R>,
jooqQuery: DMLQuery<R>,
): Int

fun getConstraintName(e: DataAccessException): String?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import kotlinx.serialization.encodeToString
import org.jooq.DSLContext
import org.jooq.InsertQuery
import org.jooq.Record
import org.jooq.Table
import org.jooq.exception.DataAccessException
import kotlin.coroutines.coroutineContext

Expand Down Expand Up @@ -86,7 +85,6 @@ class JooqEventRepository(
dslContext.insertQuery(UOW_EVENTS).apply {
setRecord(toUERecord(uowEvent))
},
UOW_EVENTS
)
} catch (e: Exception) {
val constraintName = when {
Expand Down Expand Up @@ -128,16 +126,14 @@ class JooqEventRepository(
addRecord(mer)
}
},
MODEL_EVENTS
)
}
}

private suspend fun <R : Record> insert(query: InsertQuery<R>, table: Table<R>) {
queryExecutor.executeStore(
private suspend fun <R : Record> insert(query: InsertQuery<R>) {
queryExecutor.executeQuery(
dslContext = dslContext,
jooqQuery = query,
table = table
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import com.razz.eva.events.db.tables.UowEvents.UOW_EVENTS
import com.razz.eva.events.db.tables.records.ModelEventsRecord
import com.razz.eva.events.db.tables.records.UowEventsRecord
import com.razz.eva.persistence.executor.FakeMemorizingQueryExecutor
import com.razz.eva.persistence.executor.FakeMemorizingQueryExecutor.ExecutionStep.StoreExecuted
import com.razz.eva.persistence.executor.FakeMemorizingQueryExecutor.ExecutionStep.QueryExecuted
import com.razz.eva.serialization.json.JsonFormat.json
import com.razz.eva.uow.UowParams
import com.razz.eva.tracing.Tracing.notReportingTracer
Expand Down Expand Up @@ -110,7 +110,7 @@ class JooqEventRepositorySpec : BehaviorSpec({

Then("Query executor should receive one uow event and two model events") {
queryExecutor.executionHistory shouldBe listOf(
StoreExecuted(
QueryExecuted(
dslContext,
dslContext.insertQuery(UOW_EVENTS)
.also {
Expand All @@ -136,9 +136,8 @@ class JooqEventRepositorySpec : BehaviorSpec({
}
)
},
UOW_EVENTS
),
StoreExecuted(
QueryExecuted(
dslContext,
dslContext.insertQuery(MODEL_EVENTS)
.also {
Expand Down Expand Up @@ -189,7 +188,6 @@ class JooqEventRepositorySpec : BehaviorSpec({
}
)
},
MODEL_EVENTS
)
)
}
Expand Down Expand Up @@ -238,7 +236,7 @@ class JooqEventRepositorySpec : BehaviorSpec({

Then("Query executor should receive one uow event") {
queryExecutor.executionHistory shouldBe listOf(
StoreExecuted(
QueryExecuted(
dslContext,
dslContext.insertQuery(UOW_EVENTS)
.also {
Expand All @@ -264,7 +262,6 @@ class JooqEventRepositorySpec : BehaviorSpec({
}
)
},
UOW_EVENTS
)
)
}
Expand Down Expand Up @@ -295,7 +292,7 @@ class JooqEventRepositorySpec : BehaviorSpec({

Then("Query executor should receive one uow event") {
queryExecutor.executionHistory shouldBe listOf(
StoreExecuted(
QueryExecuted(
dslContext,
dslContext.insertQuery(UOW_EVENTS)
.also {
Expand All @@ -321,7 +318,6 @@ class JooqEventRepositorySpec : BehaviorSpec({
}
)
},
UOW_EVENTS
)
)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package com.razz.eva.persistence.executor

import com.razz.eva.persistence.executor.FakeMemorizingQueryExecutor.ExecutionStep.DeleteExecuted
import com.razz.eva.persistence.executor.FakeMemorizingQueryExecutor.ExecutionStep.QueryExecuted
import com.razz.eva.persistence.executor.FakeMemorizingQueryExecutor.ExecutionStep.SelectExecuted
import com.razz.eva.persistence.executor.FakeMemorizingQueryExecutor.ExecutionStep.StoreExecuted
import org.jooq.DMLQuery
import org.jooq.DSLContext
import org.jooq.DeleteQuery
import org.jooq.Record
import org.jooq.SQLDialect.POSTGRES
import org.jooq.Select
Expand Down Expand Up @@ -57,12 +57,11 @@ class FakeMemorizingQueryExecutor(
.into(table)
}

override suspend fun <R : Record> executeDelete(
override suspend fun <R : Record> executeQuery(
dslContext: DSLContext,
jooqQuery: DeleteQuery<R>,
table: Table<R>,
jooqQuery: DMLQuery<R>,
): Int {
executions += DeleteExecuted(dslContext, jooqQuery)
executions += QueryExecuted(dslContext, jooqQuery)
return DSL.using(MockConnection(MockProvider(queries)), POSTGRES, dslContext.settings())
.execute(jooqQuery.getSQL(INLINED))
}
Expand All @@ -81,9 +80,9 @@ class FakeMemorizingQueryExecutor(
val table: Table<out Record>,
) : ExecutionStep()

data class DeleteExecuted(
data class QueryExecuted(
val dslContext: DSLContext,
val jooqQuery: DeleteQuery<out Record>,
val jooqQuery: DMLQuery<out Record>,
) : ExecutionStep()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,12 +143,11 @@ class PersistenceSpec : PersistenceBaseSpec({

When("Principal performs delete on query executor") {
module.transactionManager.inTransaction(REQUIRE_NEW) { _ ->
module.queryExecutor.executeDelete(
module.queryExecutor.executeQuery(
module.dslContext,
module.dslContext.deleteQuery(Tables.EMPLOYEES).apply {
addConditions(Tables.EMPLOYEES.ID.eq(boss.id().id))
},
Tables.EMPLOYEES,
)
}

Expand Down
17 changes: 6 additions & 11 deletions eva-uow/src/test/kotlin/com/razz/eva/uow/func/TestModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,16 @@ import com.razz.eva.uow.Persisting
import com.razz.eva.uow.UnitOfWorkExecutor
import com.razz.eva.uow.withFactory
import io.micrometer.core.instrument.simple.SimpleMeterRegistry
import org.jooq.DMLQuery
import org.jooq.DSLContext
import org.jooq.Field
import org.jooq.InsertQuery
import org.jooq.Record
import org.jooq.StoreQuery
import org.jooq.Table
import org.jooq.impl.DSL
import org.jooq.impl.SQLDataType
import java.sql.Timestamp
import java.time.Clock
import java.time.Duration
import java.time.Instant.now
import java.util.*

class TestModule(config: DatabaseConfig) : TransactionalModule(config) {
Expand Down Expand Up @@ -88,24 +87,20 @@ class TestModule(config: DatabaseConfig) : TransactionalModule(config) {
)

private val patchingQueryExecutor = object : QueryExecutor by queryExecutor {
override suspend fun <RIN : Record, ROUT : Record> executeStore(
dslContext: DSLContext,
jooqQuery: StoreQuery<RIN>,
table: Table<ROUT>,
): List<ROUT> {
if (table.name == "uow_events") {
override suspend fun <R : Record> executeQuery(dslContext: DSLContext, jooqQuery: DMLQuery<R>): Int {
if (jooqQuery is InsertQuery<*> && jooqQuery.sql.contains("uow_events")) {
val occurredAtParam = jooqQuery.getParam("6") as Field<Timestamp>
jooqQuery.addValue(
DSL.field("inserted_at", SQLDataType.TIMESTAMP),
occurredAtParam,
)
}
return queryExecutor.executeStore(dslContext, jooqQuery, table)
return queryExecutor.executeQuery(dslContext, jooqQuery)
}
}

val uowxInFuture = UnitOfWorkExecutor(
factories = factories(fixedUTC(now() + Duration.ofDays(6))),
factories = factories(fixedUTC(now + Duration.ofDays(6))),
persisting = Persisting(
transactionManager = transactionManager,
modelRepos = repos,
Expand Down

0 comments on commit b67e040

Please sign in to comment.