Skip to content

Commit

Permalink
refacto: Expose dates as Instant in dao requests
Browse files Browse the repository at this point in the history
  • Loading branch information
asolovieff committed Feb 20, 2024
1 parent bda42aa commit cb5c393
Show file tree
Hide file tree
Showing 13 changed files with 90 additions and 70 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package dao.execution.batch

import dao.utils.toDto
import dao.utils.toOffsetDateTime
import execution.INITIAL_STATUS
import execution.batch.*
import generated.domain.enums.ExecutionStatus
Expand All @@ -12,6 +13,9 @@ import org.jooq.Condition
import org.jooq.DSLContext
import org.jooq.impl.DSL
import org.jooq.impl.DSL.`val`
import java.time.Instant
import java.time.OffsetDateTime
import java.time.ZoneId
import java.util.*

class BatchExecutionDao(private val dslContext: DSLContext): BatchExecutionDaoInterface {
Expand All @@ -26,7 +30,7 @@ class BatchExecutionDao(private val dslContext: DSLContext): BatchExecutionDaoIn
DM_BATCH_EXECUTION.FK_ENVIRONMENT_REF
)
.values(
`val`(data.startDate),
`val`(data.startDate?.toOffsetDateTime()),
`val`(data.origin.toDto()),
`val`(data.type.toDto()),
`val`(INITIAL_STATUS.toDto()),
Expand All @@ -40,7 +44,7 @@ class BatchExecutionDao(private val dslContext: DSLContext): BatchExecutionDaoIn
batchExecutionStartUpdateRequest: BatchExecutionStartUpdateRequest
): BatchExecution? =
dslContext.update(DM_BATCH_EXECUTION)
.set(DM_BATCH_EXECUTION.START_DATE, batchExecutionStartUpdateRequest.startDate)
.set(DM_BATCH_EXECUTION.START_DATE, batchExecutionStartUpdateRequest.startDate.toOffsetDateTime())
.set(DM_BATCH_EXECUTION.STATUS, ExecutionStatus.IN_PROGRESS)
.where(DM_BATCH_EXECUTION.ID.eq(batchExecutionId))
.returning()
Expand All @@ -51,7 +55,7 @@ class BatchExecutionDao(private val dslContext: DSLContext): BatchExecutionDaoIn
batchExecutionEndUpdateRequest: BatchExecutionEndUpdateRequest
): BatchExecution? =
dslContext.update(DM_BATCH_EXECUTION)
.set(DM_BATCH_EXECUTION.END_DATE, batchExecutionEndUpdateRequest.endDate)
.set(DM_BATCH_EXECUTION.END_DATE, batchExecutionEndUpdateRequest.endDate.toOffsetDateTime())
.set(DM_BATCH_EXECUTION.STATUS, batchExecutionEndUpdateRequest.status.toDto())
.where(DM_BATCH_EXECUTION.ID.eq(batchExecutionId))
.returning()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package dao.execution.script

import dao.utils.toDto
import dao.utils.toOffsetDateTime
import execution.INITIAL_STATUS
import execution.script.*
import generated.domain.enums.ExecutionStatus
Expand All @@ -23,7 +24,7 @@ class ScriptExecutionDao(val dslContext: DSLContext): ScriptExecutionDaoInterfac
DM_SCRIPT_EXECUTION.FK_SCRIPT_REF,
DM_SCRIPT_EXECUTION.FK_BATCH_EXECUTION_REF,
).values(
`val`(data.startDate),
`val`(data.startDate?.toOffsetDateTime()),
`val`(data.executionOrderIndex),
`val`(INITIAL_STATUS.toDto()),
`val`(data.fkScriptRef),
Expand All @@ -37,7 +38,7 @@ class ScriptExecutionDao(val dslContext: DSLContext): ScriptExecutionDaoInterfac
executionStartData: ScriptExecutionStartUpdateRequest
): ScriptExecution? =
dslContext.update(DM_SCRIPT_EXECUTION)
.set(DM_SCRIPT_EXECUTION.START_DATE, executionStartData.startDate)
.set(DM_SCRIPT_EXECUTION.START_DATE, executionStartData.startDate.toOffsetDateTime())
.set(DM_SCRIPT_EXECUTION.STATUS, ExecutionStatus.IN_PROGRESS)
.where(DM_SCRIPT_EXECUTION.ID.eq(scriptExecutionId))
.returning()
Expand All @@ -49,7 +50,7 @@ class ScriptExecutionDao(val dslContext: DSLContext): ScriptExecutionDaoInterfac
executionEndData: ScriptExecutionEndUpdateRequest
): ScriptExecution? =
dslContext.update(DM_SCRIPT_EXECUTION)
.set(DM_SCRIPT_EXECUTION.END_DATE, executionEndData.endDate)
.set(DM_SCRIPT_EXECUTION.END_DATE, executionEndData.endDate.toOffsetDateTime())
.set(DM_SCRIPT_EXECUTION.OUTPUT, executionEndData.output)
.set(DM_SCRIPT_EXECUTION.STATUS, executionEndData.status.toDto())
.where(DM_SCRIPT_EXECUTION.ID.eq(scriptExecutionId))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ import execution.batch.Type
import generated.domain.enums.BatchExecutionOrigin
import generated.domain.enums.BatchExecutionType
import generated.domain.enums.ExecutionStatus
import java.time.Instant
import java.time.OffsetDateTime
import java.time.ZoneId

fun Origin.toDto(): BatchExecutionOrigin =
when (this) {
Expand All @@ -26,4 +29,15 @@ fun Status.toDto(): ExecutionStatus =
Status.IN_PROGRESS -> ExecutionStatus.IN_PROGRESS
Status.COMPLETED -> ExecutionStatus.COMPLETED
Status.ERROR -> ExecutionStatus.ERROR
}
}

/**
* According to Postgresql documentation (https://www.postgresql.org/docs/current/datatype-datetime.html) :
*
* For timestamp with time zone, the internally stored value is always in UTC (Universal Coordinated Time, traditionally known as Greenwich Mean Time, GMT). An input value that has an explicit time zone specified is converted to UTC using the appropriate offset for that time zone. If no time zone is stated in the input string, then it is assumed to be in the time zone indicated by the system's TimeZone parameter, and is converted to UTC using the offset for the timezone zone.
* When a timestamp with time zone value is output, it is always converted from UTC to the current timezone zone, and displayed as local time in that zone. To see the time in another time zone, either change timezone or use the AT TIME ZONE construct (see Section 9.9.4).
* Conversions between timestamp without time zone and timestamp with time zone normally assume that the timestamp without time zone value should be taken or given as timezone local time. A different time zone can be specified for the conversion using AT TIME ZONE.
*/
fun Instant.toOffsetDateTime(): OffsetDateTime {
return OffsetDateTime.ofInstant(this, ZoneId.of("UTC").normalized())
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ package dao.execution.batch

import execution.Status
import execution.batch.*
import java.time.Instant
import java.time.OffsetDateTime
import java.time.ZoneOffset
import java.util.*

fun buildBatchExecutionCreationRequest(
startDate: OffsetDateTime? = null,
startDate: Instant? = null,
origin: Origin = Origin.TIER,
type: Type = Type.ON_DEMAND,
fkEnvironmentRef: UUID,
Expand All @@ -20,13 +21,13 @@ fun buildBatchExecutionCreationRequest(
)

fun buildBatchExecutionStartUpdateRequest(
startDate: OffsetDateTime = OffsetDateTime.of(2023, 5, 2, 14, 26, 0, 0, ZoneOffset.UTC),
startDate: Instant = OffsetDateTime.of(2023, 5, 2, 14, 26, 0, 0, ZoneOffset.UTC).toInstant(),
) = BatchExecutionStartUpdateRequest(
startDate = startDate
)

fun buildBatchExecutionEndUpdateRequest(
endDate: OffsetDateTime = OffsetDateTime.of(2023, 5, 2, 14, 26, 0, 0, ZoneOffset.UTC),
endDate: Instant = OffsetDateTime.of(2023, 5, 2, 14, 26, 0, 0, ZoneOffset.UTC).toInstant(),
status: Status = Status.COMPLETED
) = BatchExecutionEndUpdateRequest(
endDate = endDate,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ internal class BatchExecutionDaoTest : AbstractDaoTest() {
fun `insert should return inserted document`() {
// Given
val batchExecutionCreationRequest = buildBatchExecutionCreationRequest(
startDate = OffsetDateTime.of(2023, 5, 2, 14, 26, 0, 0, ZoneOffset.UTC),
startDate = OffsetDateTime.of(2023, 5, 2, 14, 26, 0, 0, ZoneOffset.UTC).toInstant(),
fkModuleRef = module1.id,
fkEnvironmentRef = environment1.id
)
Expand All @@ -71,7 +71,7 @@ internal class BatchExecutionDaoTest : AbstractDaoTest() {

// Then
expectThat(insertedBatchExecution).isNotNull().and {
get { startDate?.isEqual(batchExecutionCreationRequest.startDate) }.isTrue()
get { startDate?.toInstant() }.isEqualTo(batchExecutionCreationRequest.startDate)
get { endDate }.isNull()
get { origin }.isEqualTo(batchExecutionCreationRequest.origin)
get { type }.isEqualTo(batchExecutionCreationRequest.type)
Expand All @@ -85,7 +85,7 @@ internal class BatchExecutionDaoTest : AbstractDaoTest() {
fun `insert should write document in database`() {
// Given
val batchExecutionCreationRequest = buildBatchExecutionCreationRequest(
startDate = OffsetDateTime.of(2023, 5, 2, 14, 26, 0, 0, ZoneOffset.UTC),
startDate = OffsetDateTime.of(2023, 5, 2, 14, 26, 0, 0, ZoneOffset.UTC).toInstant(),
fkEnvironmentRef = environment1.id,
fkModuleRef = module1.id
)
Expand All @@ -100,7 +100,7 @@ internal class BatchExecutionDaoTest : AbstractDaoTest() {
insertedDmBatchExecution
).isNotNull().and {
get { id }.isEqualTo(insertedId)
get { startDate?.isEqual(batchExecutionCreationRequest.startDate) }.isTrue()
get { startDate?.toInstant() }.isEqualTo(batchExecutionCreationRequest.startDate)
get { endDate }.isNull()
get { origin }.isEqualTo(batchExecutionCreationRequest.origin.toDto())
get { type }.isEqualTo(batchExecutionCreationRequest.type.toDto())
Expand Down Expand Up @@ -235,7 +235,7 @@ internal class BatchExecutionDaoTest : AbstractDaoTest() {
batchExecutionDao.updateBatchExecutionEndData(
batchExecution.id,
BatchExecutionEndUpdateRequest(
endDate = OffsetDateTime.now().plusDays(1L),
endDate = OffsetDateTime.now().plusDays(1L).toInstant(),
status = Status.COMPLETED
)
)!!
Expand Down Expand Up @@ -347,7 +347,7 @@ internal class BatchExecutionDaoTest : AbstractDaoTest() {
fun `should return null when id does not exist in db`() {
// Given
val batchExecutionCreationRequest = buildBatchExecutionCreationRequest(
startDate = OffsetDateTime.of(2023, 5, 2, 14, 26, 0, 0, ZoneOffset.UTC),
startDate = OffsetDateTime.of(2023, 5, 2, 14, 26, 0, 0, ZoneOffset.UTC).toInstant(),
fkModuleRef = module1.id,
fkEnvironmentRef = environment1.id
)
Expand All @@ -369,7 +369,7 @@ internal class BatchExecutionDaoTest : AbstractDaoTest() {
fun `should not update anything when id does not exist`() {
// Given
val batchExecutionCreationRequest = buildBatchExecutionCreationRequest(
startDate = OffsetDateTime.of(2023, 5, 2, 14, 26, 0, 0, ZoneOffset.UTC),
startDate = OffsetDateTime.of(2023, 5, 2, 14, 26, 0, 0, ZoneOffset.UTC).toInstant(),
fkModuleRef = module1.id,
fkEnvironmentRef = environment1.id
)
Expand All @@ -387,7 +387,7 @@ internal class BatchExecutionDaoTest : AbstractDaoTest() {
val batchExecutionFromDb = batchExecutionDao.findOneById(insertedId)
expectThat(batchExecutionFromDb).isNotNull().and {
get { id }.isEqualTo(insertedId)
get { startDate?.isEqual(batchExecutionCreationRequest.startDate) }.isTrue()
get { startDate?.toInstant() }.isEqualTo(batchExecutionCreationRequest.startDate)
get { endDate }.isNull()
get { durationInMs }.isNull()
get { origin }.isEqualTo(batchExecutionCreationRequest.origin)
Expand All @@ -408,7 +408,7 @@ internal class BatchExecutionDaoTest : AbstractDaoTest() {
)

val insertedId = batchExecutionDao.insert(batchExecutionCreationRequest).id
val newStartDate = OffsetDateTime.of(2023, 5, 2, 14, 26, 0, 0, ZoneOffset.UTC)
val newStartDate = OffsetDateTime.of(2023, 5, 2, 14, 26, 0, 0, ZoneOffset.UTC).toInstant()

// When
batchExecutionDao.updateBatchExecutionStartData(
Expand All @@ -422,7 +422,7 @@ internal class BatchExecutionDaoTest : AbstractDaoTest() {
val batchExecutionFromDb = batchExecutionDao.findOneById(insertedId)
expectThat(batchExecutionFromDb).isNotNull().and {
get { id }.isEqualTo(insertedId)
get { startDate?.isEqual(newStartDate) }.isTrue()
get { startDate?.toInstant() }.isEqualTo(newStartDate)
get { endDate }.isNull()
get { durationInMs }.isNull()
get { origin }.isEqualTo(batchExecutionCreationRequest.origin)
Expand All @@ -440,7 +440,7 @@ internal class BatchExecutionDaoTest : AbstractDaoTest() {
fun `should return null when id does not exist in db`() {
// Given
val batchExecutionCreationRequest = buildBatchExecutionCreationRequest(
startDate = OffsetDateTime.of(2023, 5, 2, 14, 26, 0, 0, ZoneOffset.UTC),
startDate = OffsetDateTime.of(2023, 5, 2, 14, 26, 0, 0, ZoneOffset.UTC).toInstant(),
fkModuleRef = module1.id,
fkEnvironmentRef = environment1.id
)
Expand All @@ -462,7 +462,7 @@ internal class BatchExecutionDaoTest : AbstractDaoTest() {
fun `should not update anything when id does not exist`() {
// Given
val batchExecutionCreationRequest = buildBatchExecutionCreationRequest(
startDate = OffsetDateTime.of(2023, 5, 2, 14, 26, 0, 0, ZoneOffset.UTC),
startDate = OffsetDateTime.of(2023, 5, 2, 14, 26, 0, 0, ZoneOffset.UTC).toInstant(),
fkModuleRef = module1.id,
fkEnvironmentRef = environment1.id
)
Expand All @@ -480,7 +480,7 @@ internal class BatchExecutionDaoTest : AbstractDaoTest() {
val batchExecutionFromDb = batchExecutionDao.findOneById(insertedId)
expectThat(batchExecutionFromDb).isNotNull().and {
get { id }.isEqualTo(insertedId)
get { startDate?.isEqual(batchExecutionCreationRequest.startDate) }.isTrue()
get { startDate?.toInstant() }.isEqualTo(batchExecutionCreationRequest.startDate)
get { endDate }.isNull()
get { durationInMs }.isNull()
get { origin }.isEqualTo(batchExecutionCreationRequest.origin)
Expand All @@ -495,13 +495,13 @@ internal class BatchExecutionDaoTest : AbstractDaoTest() {
fun `should return updated script execution`() {
// Given
val batchExecutionCreationRequest = buildBatchExecutionCreationRequest(
startDate = OffsetDateTime.of(2023, 5, 2, 14, 26, 0, 0, ZoneOffset.UTC),
startDate = OffsetDateTime.of(2023, 5, 2, 14, 26, 0, 0, ZoneOffset.UTC).toInstant(),
fkModuleRef = module1.id,
fkEnvironmentRef = environment1.id
)

val insertedId = batchExecutionDao.insert(batchExecutionCreationRequest).id
val newEndDate = OffsetDateTime.of(2023, 5, 2, 14, 30, 0, 0, ZoneOffset.UTC)
val newEndDate = OffsetDateTime.of(2023, 5, 2, 14, 30, 0, 0, ZoneOffset.UTC).toInstant()

// When
batchExecutionDao.updateBatchExecutionEndData(
Expand All @@ -516,8 +516,8 @@ internal class BatchExecutionDaoTest : AbstractDaoTest() {
val batchExecutionFromDb = batchExecutionDao.findOneById(insertedId)
expectThat(batchExecutionFromDb).isNotNull().and {
get { id }.isEqualTo(insertedId)
get { startDate?.isEqual(batchExecutionCreationRequest.startDate) }.isTrue()
get { endDate?.isEqual(newEndDate) }.isTrue()
get { startDate?.toInstant() }.isEqualTo(batchExecutionCreationRequest.startDate)
get { endDate?.toInstant() }.isEqualTo(newEndDate)
get { durationInMs }.isEqualTo(240_000)
get { origin }.isEqualTo(batchExecutionCreationRequest.origin)
get { status }.isEqualTo(Status.COMPLETED)
Expand All @@ -535,7 +535,7 @@ internal class BatchExecutionDaoTest : AbstractDaoTest() {
// Given
val insertedId = batchExecutionDao.insert(
buildBatchExecutionCreationRequest(
startDate = OffsetDateTime.of(2023, 5, 2, 14, 26, 0, 0, ZoneOffset.UTC),
startDate = OffsetDateTime.of(2023, 5, 2, 14, 26, 0, 0, ZoneOffset.UTC).toInstant(),
fkModuleRef = module1.id,
fkEnvironmentRef = environment1.id
)
Expand All @@ -554,7 +554,7 @@ internal class BatchExecutionDaoTest : AbstractDaoTest() {
// Given
val insertedId1 = batchExecutionDao.insert(
buildBatchExecutionCreationRequest(
startDate = OffsetDateTime.of(2023, 5, 2, 14, 26, 0, 0, ZoneOffset.UTC),
startDate = OffsetDateTime.of(2023, 5, 2, 14, 26, 0, 0, ZoneOffset.UTC).toInstant(),
fkModuleRef = module1.id,
fkEnvironmentRef = environment1.id
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ import execution.Status
import execution.script.ScriptExecutionCreationRequest
import execution.script.ScriptExecutionEndUpdateRequest
import execution.script.ScriptExecutionStartUpdateRequest
import java.time.Instant
import java.time.OffsetDateTime
import java.time.ZoneOffset
import java.util.*

fun buildScriptExecutionCreationRequest(
startDate: OffsetDateTime? = null,
startDate: Instant? = null,
executionOrderIndex: Int = 0,
scriptRef: String,
batchExecutionRef: UUID
Expand All @@ -21,13 +22,13 @@ fun buildScriptExecutionCreationRequest(
)

fun buildScriptExecutionStartUpdateRequest(
startDate: OffsetDateTime = OffsetDateTime.of(2023, 5, 30, 14, 26, 0, 0, ZoneOffset.UTC)
startDate: Instant = OffsetDateTime.of(2023, 5, 30, 14, 26, 0, 0, ZoneOffset.UTC).toInstant()
) = ScriptExecutionStartUpdateRequest(
startDate = startDate
)

fun buildScriptExecutionEndUpdateRequest(
endDate: OffsetDateTime = OffsetDateTime.of(2023, 5, 30, 14, 26, 0, 0, ZoneOffset.UTC),
endDate: Instant = OffsetDateTime.of(2023, 5, 30, 14, 26, 0, 0, ZoneOffset.UTC).toInstant(),
output: String = "output",
status: Status = Status.COMPLETED,
) = ScriptExecutionEndUpdateRequest(
Expand Down
Loading

0 comments on commit cb5c393

Please sign in to comment.