OffsetDateTime support on DateUtils class
skaya129 opened this issue · comments
Description
We are using custom mongo datetetime converters on our project and OffsetDateTime format. We switched from Mongock 4 to 5 and our migration code stop running. We noticed that OffsetDateTime support doesn't exist on DateUtils.class (https://github.com/mongock/mongock/blob/d14863ca63307e9e22ff25155e5939b74c4dc8c2/mongock-core/mongock-utils/src/main/java/io/mongock/utils/DateUtils.java)
PRIORITY
NORMAL
Version and environment
Mongock
- Mongock version 5.4.1-SNAPSHOT
- Springboot 3.2 project, MongoDB 4.4
- Mongock is fetched from Maven repo
Environment
- Framework and libraries versions. Especially those that affect directly to Mongock(Spring, Spring data, MongoDB driver, etc.)
- Infrastructure: Kubernetes, Docker, SO, etc.
Steps to Reproduce
- Use a custome OffsetDateTimeCodec like
`public class OffsetDateTimeCodec implements Codec {
@OverRide
public void encode(final BsonWriter writer, final OffsetDateTime value, final EncoderContext encoderContext) {
writer.writeDateTime(value.toInstant().toEpochMilli());
}
@Override
public OffsetDateTime decode(final BsonReader reader, final DecoderContext decoderContext) {
if (reader.getCurrentBsonType().equals(BsonType.DATE_TIME)) {
return OffsetDateTime.ofInstant(Instant.ofEpochMilli(reader.readDateTime()), ZoneId.systemDefault());
}
return OffsetDateTime.parse(reader.readString());
}
@Override
public Class<OffsetDateTime> getEncoderClass() {
return OffsetDateTime.class;
}
`
2. Run migration code
Behaviour
Expected behavior: [What you expect to happen]
Migration should run
Actual behavior: [What actually happens]
Migration doesn't run
How often the bug happens: [What percentage of the time does it reproduce?]
Always
Additional context
Adding OffsetDateTime supoort will fix this issue like :
`package io.mongock.utils;
import java.time.LocalDateTime;
import java.time.OffsetDateTime;
import java.time.ZoneId;
import java.util.Date;
public final class DateUtils {
private DateUtils() {
}
public static Date toDate(Object value) {
if (value == null) {
return null;
}
else if (value.getClass().equals(Date.class)) {
return (Date)value;
}
else if (value.getClass().equals(LocalDateTime.class)) {
return localDateTimeToDate((LocalDateTime)value);
}
else if (value.getClass().equals(OffsetDateTime.class)) {
return offsetDateTimeToDate((OffsetDateTime)value);
}
else {
throw new RuntimeException(String.format("%s cannot be cast to %s", value.getClass().getName(), Date.class.getName()));
}
}
private static Date localDateTimeToDate(LocalDateTime dateToConvert) {
return Date
.from(dateToConvert.atZone(ZoneId.systemDefault())
.toInstant());
}
private static Date offsetDateTimeToDate(OffsetDateTime dateToConvert) {
return Date.from(dateToConvert.toInstant());
}
}`
Hello @skaya129 , thanks for your contribution. I see you have a clear idea of what the problem is and how to fix it. I suggest you to raise a pull request and we'll happily review it straight away.
If for any reason you're not in a good position to do, we'll do it as soon as we have bandwidth 😃
Thanks
Thank you @dieppa, I would be happy to do it.
I don't have permission, I will be glad if you do it, @dieppa
permission for what? If it's to do a PR, I guess you are trying to do ir directly. It should be done by forking the repo first, then create your branch, do the changes and then make the PR agains master in this original repository.
Give it a shoot and let me know how it goes 😉
Thank you and done (I hope)
I will try to take a look between today and tomorrow!
Thanks a lot!
PR reviewed and merged. We will do a release shortly.
Thanks!