Skip to content

Commit 8cbfbe1

Browse files
authored
refactor: Add support for converting to DATE UTC values for PostgreSQL (#376)
In AlertQueryContext: -Introduce a custom value converter to ensure the accuracy of DATE and DATE? Types are stored and processed in UTC time. -Added the ApplyProvierSpecificConfiguration method, specifically designed for configuring value converters for PostgreSQL. Add two value converters: -FHIR UttConverter: deals with non empty DATE types. -NullableFHIR UttConverter: Handling Vacable Dates? Type. This change ensures that all DATE properties are stored in UTC time when using a PostgreSQL database.
1 parent 93105a1 commit 8cbfbe1

4 files changed

Lines changed: 51 additions & 0 deletions

File tree

src/Infrastructure/Masa.Alert.EntityFrameworkCore/AlertQueryContext.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,30 @@ protected override void OnModelCreatingExecuting(ModelBuilder builder)
9999
b.ToView(AlertConsts.DB_TABLE_PREFIX + "WebHooks", AlertConsts.DB_SCHEMA);
100100
});
101101

102+
// Apply provider-specific configurations
103+
ApplyProviderSpecificConfigurations(builder);
104+
102105
base.OnModelCreatingExecuting(builder);
103106
}
107+
108+
private void ApplyProviderSpecificConfigurations(ModelBuilder builder)
109+
{
110+
if (Database.ProviderName == "Npgsql.EntityFrameworkCore.PostgreSQL")
111+
{
112+
foreach (var entityType in builder.Model.GetEntityTypes())
113+
{
114+
foreach (var property in entityType.GetProperties())
115+
{
116+
if (property.ClrType == typeof(DateTime))
117+
{
118+
property.SetValueConverter(new DateTimeUtcConverter());
119+
}
120+
else if (property.ClrType == typeof(DateTime?))
121+
{
122+
property.SetValueConverter(new NullableDateTimeUtcConverter());
123+
}
124+
}
125+
}
126+
}
127+
}
104128
}

src/Infrastructure/Masa.Alert.EntityFrameworkCore/_Imports.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
global using Masa.Alert.Domain.WebHooks.Aggregates;
1414
global using Masa.Alert.Domain.WebHooks.Repositories;
1515
global using Masa.Alert.Infrastructure.EntityFrameworkCore.EntityFrameworkCore.ValueConverters;
16+
global using Masa.Alert.Infrastructure.EntityFrameworkCore.ValueConverters;
1617
global using Masa.BuildingBlocks.Data.UoW;
1718
global using Masa.BuildingBlocks.Data.Contracts;
1819
global using Masa.Contrib.Ddd.Domain.Repository.EFCore;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Copyright (c) MASA Stack All rights reserved.
2+
// Licensed under the Apache License. See LICENSE.txt in the project root for license information.
3+
4+
namespace Masa.Alert.Infrastructure.EntityFrameworkCore.ValueConverters;
5+
6+
public class DateTimeUtcConverter : ValueConverter<DateTime, DateTime>
7+
{
8+
public DateTimeUtcConverter() : base(
9+
v => v.ToUniversalTime(),
10+
v => DateTime.SpecifyKind(v, DateTimeKind.Utc))
11+
{
12+
}
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Copyright (c) MASA Stack All rights reserved.
2+
// Licensed under the Apache License. See LICENSE.txt in the project root for license information.
3+
4+
namespace Masa.Alert.Infrastructure.EntityFrameworkCore.ValueConverters;
5+
6+
public class NullableDateTimeUtcConverter : ValueConverter<DateTime?, DateTime?>
7+
{
8+
public NullableDateTimeUtcConverter() : base(
9+
v => v.HasValue ? v.Value.Kind == DateTimeKind.Unspecified ? DateTime.SpecifyKind(v.Value, DateTimeKind.Utc).ToUniversalTime() : v.Value.ToUniversalTime() : null,
10+
v => v.HasValue ? DateTime.SpecifyKind(v.Value, DateTimeKind.Utc) : null)
11+
{
12+
}
13+
}

0 commit comments

Comments
 (0)