Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Tech] refacto gestion timestamps #1169

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,6 @@ class CreateOrUpdateMission(
UpdateMissionEvent(savedMission.mission),
)

/**
* TODO When doing an update, the `createdAtUtc` field is returned as null.
* To fix this, we return the stored `createdAtUtc`
*/
return savedMission.mission.copy(
createdAtUtc = savedMission.mission.createdAtUtc ?: storedMission?.createdAtUtc,
)
return savedMission.mission
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,8 @@ class MissionModel(
observationsCnsp = mission.observationsCnsp,
openBy = mission.openBy,
startDateTimeUtc = mission.startDateTimeUtc.toInstant(),
createdAtUtc = mission.createdAtUtc?.toInstant(),
updatedAtUtc = mission.updatedAtUtc?.toInstant(),
)

mission.envActions?.map {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
-- let hibernate manage the timestamps
ALTER TABLE missions ALTER created_at_utc SET DEFAULT null;
ALTER TABLE missions ALTER updated_at_utc SET DEFAULT null;

-- ensure at least a timestamp is set so hibernates knows how to handle it
UPDATE missions set created_at_utc = start_datetime_utc where created_at_utc is null;
UPDATE missions set updated_at_utc = start_datetime_utc where updated_at_utc is null;
Original file line number Diff line number Diff line change
Expand Up @@ -160,3 +160,8 @@ UPDATE public.missions SET
start_datetime_utc = start_datetime_utc + (now() - '2022-06-01 23:00:00'),
end_datetime_utc = end_datetime_utc + (now() - '2022-06-01 23:00:00')
WHERE id > 20;



UPDATE missions set created_at_utc = least(now()- interval '10 minutes', start_datetime_utc) where created_at_utc is null;
UPDATE missions set updated_at_utc = least(now()- interval '10 minutes', start_datetime_utc) where updated_at_utc is null;
Original file line number Diff line number Diff line change
Expand Up @@ -156,61 +156,4 @@ class CreateOrUpdateMissionUTests {
)
assertThat(createdMission).isEqualTo(expectedCreatedMission)
}

@Test
fun `should return the stored createAtUtc field When an update returned null createAtUtc`() {
// Given
val missionToUpdate =
MissionEntity(
id = 100,
missionTypes = listOf(MissionTypeEnum.LAND),
facade = "Outre-Mer",
geom = null,
startDateTimeUtc = ZonedDateTime.parse("2022-01-15T04:50:09Z"),
endDateTimeUtc = ZonedDateTime.parse("2022-01-23T20:29:03Z"),
isClosed = false,
missionSource = MissionSourceEnum.MONITORENV,
hasMissionOrder = false,
isDeleted = false,
isUnderJdp = false,
isGeometryComputedFromControls = false,
)

val returnedSavedMission =
MissionEntity(
id = 100,
endDateTimeUtc = ZonedDateTime.parse("2022-01-23T20:29:03Z"),
createdAtUtc = null,
updatedAtUtc = ZonedDateTime.now(),
facade = "La Face Ade",
hasMissionOrder = false,
isClosed = false,
isDeleted = false,
isGeometryComputedFromControls = false,
isUnderJdp = false,
missionSource = MissionSourceEnum.MONITORENV,
missionTypes = listOf(MissionTypeEnum.LAND),
startDateTimeUtc = ZonedDateTime.parse("2022-01-15T04:50:09Z"),
)

given(facadeAreasRepository.findFacadeFromGeometry(anyOrNull())).willReturn("La Face Ade")
given(missionRepository.findById(100))
.willReturn(missionToUpdate.copy(createdAtUtc = ZonedDateTime.parse("2022-01-23T20:29:03Z")))
given(missionRepository.save(anyOrNull()))
.willReturn(MissionDTO(mission = returnedSavedMission))

// When
val createdMission =
CreateOrUpdateMission(
missionRepository = missionRepository,
facadeRepository = facadeAreasRepository,
eventPublisher = applicationEventPublisher,
)
.execute(
missionToUpdate,
)

// Then
assertThat(createdMission.createdAtUtc).isEqualTo(ZonedDateTime.parse("2022-01-23T20:29:03Z"))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -755,7 +755,7 @@ class JpaMissionRepositoryITests : AbstractDBTests() {

val updatedMission = jpaMissionRepository.save(nextMission)

assertThat(updatedMission.mission.createdAtUtc).isNull()
assertThat(updatedMission.mission.createdAtUtc).isNotNull()
assertThat(updatedMission.mission.updatedAtUtc).isAfter(ZonedDateTime.now().minusMinutes(1))
assertThat(updatedMission.mission.controlUnits).hasSize(2)
assertThat(updatedMission.mission.controlUnits.first().id).isEqualTo(10002)
Expand Down
Loading