-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #31834 from yrodiere/i31586-tz-storage
Introduce `quarkus.hibernate-orm.mapping.timezone.default-storage`
- Loading branch information
Showing
11 changed files
with
413 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
...nt/src/test/java/io/quarkus/hibernate/orm/mapping/AbstractTimezoneDefaultStorageTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package io.quarkus.hibernate.orm.mapping; | ||
|
||
import java.time.LocalDateTime; | ||
import java.time.Month; | ||
import java.time.OffsetDateTime; | ||
import java.time.OffsetTime; | ||
import java.time.ZoneId; | ||
import java.time.ZoneOffset; | ||
import java.time.ZonedDateTime; | ||
|
||
import jakarta.inject.Inject; | ||
|
||
import org.assertj.core.api.SoftAssertions; | ||
import org.hibernate.Session; | ||
import org.hibernate.SessionFactory; | ||
|
||
import io.quarkus.narayana.jta.QuarkusTransaction; | ||
|
||
public class AbstractTimezoneDefaultStorageTest { | ||
|
||
private static final LocalDateTime LOCAL_DATE_TIME_TO_TEST = LocalDateTime.of(2017, Month.NOVEMBER, 6, 19, 19, 0); | ||
public static final ZonedDateTime PERSISTED_ZONED_DATE_TIME = LOCAL_DATE_TIME_TO_TEST.atZone(ZoneId.of("Africa/Cairo")); | ||
public static final OffsetDateTime PERSISTED_OFFSET_DATE_TIME = LOCAL_DATE_TIME_TO_TEST.atOffset(ZoneOffset.ofHours(3)); | ||
public static final OffsetTime PERSISTED_OFFSET_TIME = LOCAL_DATE_TIME_TO_TEST.toLocalTime() | ||
.atOffset(ZoneOffset.ofHours(3)); | ||
|
||
@Inject | ||
SessionFactory sessionFactory; | ||
|
||
@Inject | ||
Session session; | ||
|
||
protected long persistWithValuesToTest() { | ||
return QuarkusTransaction.requiringNew().call(() -> { | ||
var entity = new EntityWithTimezones(PERSISTED_ZONED_DATE_TIME, PERSISTED_OFFSET_DATE_TIME); | ||
session.persist(entity); | ||
return entity.id; | ||
}); | ||
} | ||
|
||
protected void assertLoadedValues(long id, ZonedDateTime expectedZonedDateTime, OffsetDateTime expectedOffsetDateTime) { | ||
QuarkusTransaction.requiringNew().run(() -> { | ||
var entity = session.find(EntityWithTimezones.class, id); | ||
SoftAssertions.assertSoftly(assertions -> { | ||
assertions.assertThat(entity).extracting("zonedDateTime").isEqualTo(expectedZonedDateTime); | ||
assertions.assertThat(entity).extracting("offsetDateTime").isEqualTo(expectedOffsetDateTime); | ||
}); | ||
}); | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
...te-orm/deployment/src/test/java/io/quarkus/hibernate/orm/mapping/EntityWithTimezones.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package io.quarkus.hibernate.orm.mapping; | ||
|
||
import java.time.OffsetDateTime; | ||
import java.time.ZonedDateTime; | ||
|
||
import jakarta.persistence.Entity; | ||
import jakarta.persistence.GeneratedValue; | ||
import jakarta.persistence.Id; | ||
|
||
@Entity | ||
public class EntityWithTimezones { | ||
|
||
@Id | ||
@GeneratedValue | ||
Long id; | ||
|
||
public EntityWithTimezones() { | ||
} | ||
|
||
public EntityWithTimezones(ZonedDateTime zonedDateTime, OffsetDateTime offsetDateTime) { | ||
this.zonedDateTime = zonedDateTime; | ||
this.offsetDateTime = offsetDateTime; | ||
} | ||
|
||
public ZonedDateTime zonedDateTime; | ||
|
||
public OffsetDateTime offsetDateTime; | ||
|
||
} |
39 changes: 39 additions & 0 deletions
39
...oyment/src/test/java/io/quarkus/hibernate/orm/mapping/TimezoneDefaultStorageAutoTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package io.quarkus.hibernate.orm.mapping; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import io.quarkus.hibernate.orm.SchemaUtil; | ||
import io.quarkus.hibernate.orm.SmokeTestUtils; | ||
import io.quarkus.test.QuarkusUnitTest; | ||
|
||
public class TimezoneDefaultStorageAutoTest extends AbstractTimezoneDefaultStorageTest { | ||
|
||
@RegisterExtension | ||
static QuarkusUnitTest TEST = new QuarkusUnitTest() | ||
.withApplicationRoot((jar) -> jar | ||
.addClasses(EntityWithTimezones.class) | ||
.addClasses(SchemaUtil.class, SmokeTestUtils.class)) | ||
.withConfigurationResource("application.properties") | ||
.overrideConfigKey("quarkus.hibernate-orm.mapping.timezone.default-storage", "auto"); | ||
|
||
@Test | ||
public void schema() throws Exception { | ||
assertThat(SchemaUtil.getColumnNames(sessionFactory, EntityWithTimezones.class)) | ||
.doesNotContain("zonedDateTime_tz", "offsetDateTime_tz"); | ||
assertThat(SchemaUtil.getColumnTypeName(sessionFactory, EntityWithTimezones.class, "zonedDateTime")) | ||
.isEqualTo("TIMESTAMP_WITH_TIMEZONE"); | ||
assertThat(SchemaUtil.getColumnTypeName(sessionFactory, EntityWithTimezones.class, "offsetDateTime")) | ||
.isEqualTo("TIMESTAMP_WITH_TIMEZONE"); | ||
} | ||
|
||
@Test | ||
public void persistAndLoad() { | ||
long id = persistWithValuesToTest(); | ||
// For some reason native storage (with H2 at least) preserves the offset, but not the zone ID. | ||
assertLoadedValues(id, PERSISTED_ZONED_DATE_TIME.withZoneSameInstant(PERSISTED_ZONED_DATE_TIME.getOffset()), | ||
PERSISTED_OFFSET_DATE_TIME); | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
...ment/src/test/java/io/quarkus/hibernate/orm/mapping/TimezoneDefaultStorageColumnTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package io.quarkus.hibernate.orm.mapping; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import io.quarkus.hibernate.orm.SchemaUtil; | ||
import io.quarkus.hibernate.orm.SmokeTestUtils; | ||
import io.quarkus.test.QuarkusUnitTest; | ||
|
||
public class TimezoneDefaultStorageColumnTest extends AbstractTimezoneDefaultStorageTest { | ||
|
||
@RegisterExtension | ||
static QuarkusUnitTest TEST = new QuarkusUnitTest() | ||
.withApplicationRoot((jar) -> jar | ||
.addClasses(EntityWithTimezones.class) | ||
.addClasses(SchemaUtil.class, SmokeTestUtils.class)) | ||
.withConfigurationResource("application.properties") | ||
.overrideConfigKey("quarkus.hibernate-orm.mapping.timezone.default-storage", "column"); | ||
|
||
@Test | ||
public void schema() throws Exception { | ||
assertThat(SchemaUtil.getColumnNames(sessionFactory, EntityWithTimezones.class)) | ||
.contains("zonedDateTime_tz", "offsetDateTime_tz") | ||
// For some reason we don't get a TZ column for OffsetTime | ||
.doesNotContain("offsetTime_tz"); | ||
assertThat(SchemaUtil.getColumnTypeName(sessionFactory, EntityWithTimezones.class, "zonedDateTime")) | ||
.isEqualTo("TIMESTAMP_UTC"); | ||
assertThat(SchemaUtil.getColumnTypeName(sessionFactory, EntityWithTimezones.class, "offsetDateTime")) | ||
.isEqualTo("TIMESTAMP_UTC"); | ||
} | ||
|
||
@Test | ||
public void persistAndLoad() { | ||
long id = persistWithValuesToTest(); | ||
// For some reason column storage preserves the offset, but not the zone ID. | ||
assertLoadedValues(id, PERSISTED_ZONED_DATE_TIME.withZoneSameInstant(PERSISTED_ZONED_DATE_TIME.getOffset()), | ||
PERSISTED_OFFSET_DATE_TIME); | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
...ent/src/test/java/io/quarkus/hibernate/orm/mapping/TimezoneDefaultStorageDefaultTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package io.quarkus.hibernate.orm.mapping; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import io.quarkus.hibernate.orm.SchemaUtil; | ||
import io.quarkus.hibernate.orm.SmokeTestUtils; | ||
import io.quarkus.test.QuarkusUnitTest; | ||
|
||
public class TimezoneDefaultStorageDefaultTest extends AbstractTimezoneDefaultStorageTest { | ||
|
||
@RegisterExtension | ||
static QuarkusUnitTest TEST = new QuarkusUnitTest() | ||
.withApplicationRoot((jar) -> jar | ||
.addClasses(EntityWithTimezones.class) | ||
.addClasses(SchemaUtil.class, SmokeTestUtils.class)) | ||
.withConfigurationResource("application.properties"); | ||
|
||
@Test | ||
public void schema() throws Exception { | ||
assertThat(SchemaUtil.getColumnNames(sessionFactory, EntityWithTimezones.class)) | ||
.doesNotContain("zonedDateTime_tz", "offsetDateTime_tz"); | ||
assertThat(SchemaUtil.getColumnTypeName(sessionFactory, EntityWithTimezones.class, "zonedDateTime")) | ||
.isEqualTo("TIMESTAMP_WITH_TIMEZONE"); | ||
assertThat(SchemaUtil.getColumnTypeName(sessionFactory, EntityWithTimezones.class, "offsetDateTime")) | ||
.isEqualTo("TIMESTAMP_WITH_TIMEZONE"); | ||
} | ||
|
||
@Test | ||
public void persistAndLoad() { | ||
long id = persistWithValuesToTest(); | ||
// For some reason native storage (with H2 at least) preserves the offset, but not the zone ID. | ||
assertLoadedValues(id, PERSISTED_ZONED_DATE_TIME.withZoneSameInstant(PERSISTED_ZONED_DATE_TIME.getOffset()), | ||
PERSISTED_OFFSET_DATE_TIME); | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
...ment/src/test/java/io/quarkus/hibernate/orm/mapping/TimezoneDefaultStorageNativeTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package io.quarkus.hibernate.orm.mapping; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import io.quarkus.hibernate.orm.SchemaUtil; | ||
import io.quarkus.hibernate.orm.SmokeTestUtils; | ||
import io.quarkus.test.QuarkusUnitTest; | ||
|
||
public class TimezoneDefaultStorageNativeTest extends AbstractTimezoneDefaultStorageTest { | ||
|
||
@RegisterExtension | ||
static QuarkusUnitTest TEST = new QuarkusUnitTest() | ||
.withApplicationRoot((jar) -> jar | ||
.addClasses(EntityWithTimezones.class) | ||
.addClasses(SchemaUtil.class, SmokeTestUtils.class)) | ||
.withConfigurationResource("application.properties") | ||
.overrideConfigKey("quarkus.hibernate-orm.mapping.timezone.default-storage", "native"); | ||
|
||
@Test | ||
public void schema() throws Exception { | ||
assertThat(SchemaUtil.getColumnNames(sessionFactory, EntityWithTimezones.class)) | ||
.doesNotContain("zonedDateTime_tz", "offsetDateTime_tz"); | ||
assertThat(SchemaUtil.getColumnTypeName(sessionFactory, EntityWithTimezones.class, "zonedDateTime")) | ||
.isEqualTo("TIMESTAMP_WITH_TIMEZONE"); | ||
assertThat(SchemaUtil.getColumnTypeName(sessionFactory, EntityWithTimezones.class, "offsetDateTime")) | ||
.isEqualTo("TIMESTAMP_WITH_TIMEZONE"); | ||
} | ||
|
||
@Test | ||
public void persistAndLoad() { | ||
long id = persistWithValuesToTest(); | ||
// For some reason native storage (with H2 at least) preserves the offset, but not the zone ID. | ||
assertLoadedValues(id, PERSISTED_ZONED_DATE_TIME.withZoneSameInstant(PERSISTED_ZONED_DATE_TIME.getOffset()), | ||
PERSISTED_OFFSET_DATE_TIME); | ||
} | ||
} |
Oops, something went wrong.