Skip to content

Commit

Permalink
* fix breaking change on mapping pgsql enum type to c# in https://www…
Browse files Browse the repository at this point in the history
  • Loading branch information
n0099 committed Dec 8, 2024
1 parent d482607 commit 8d636a0
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 17 deletions.
5 changes: 3 additions & 2 deletions c#/crawler/src/Db/CrawlerDbContext.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Microsoft.EntityFrameworkCore.Infrastructure;
using Npgsql;
using Npgsql.EntityFrameworkCore.PostgreSQL.Infrastructure;
using static tbm.Crawler.Db.Revision.Splitting.ReplyRevision;
using static tbm.Crawler.Db.Revision.Splitting.SubReplyRevision;
using static tbm.Crawler.Db.Revision.Splitting.ThreadRevision;
Expand Down Expand Up @@ -117,8 +118,8 @@ protected override void OnModelCreating(ModelBuilder b)
b.Entity<Forum>().ToTable("tbm_forum");
}

protected override void OnBuildingNpgsqlDataSource(NpgsqlDataSourceBuilder builder) =>
builder.MapEnum<PostType>("tbmcr_triggeredBy", NpgsqlCamelCaseNameTranslator.Instance);
protected override void OnConfiguringNpgsql(NpgsqlDbContextOptionsBuilder builder) =>
builder.MapEnum<PostType>("tbmcr_triggeredBy", nameTranslator: NpgsqlCamelCaseNameTranslator.Instance);

public class ModelCacheKeyFactory : IModelCacheKeyFactory
{ // https://stackoverflow.com/questions/51864015/entity-framework-map-model-class-to-table-at-run-time/51899590#51899590
Expand Down
16 changes: 1 addition & 15 deletions c#/shared/src/Db/TbmDbContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -135,10 +135,6 @@ public class TbmDbContext<TModelCacheKeyFactory>(ILogger<TbmDbContext<TModelCach
: TbmDbContext(logger)
where TModelCacheKeyFactory : class, IModelCacheKeyFactory
{
[SuppressMessage("ReSharper", "StaticMemberInGenericType")]
[SuppressMessage("Major Code Smell", "S2743:Static fields should not be used in generic types")]
private static Lazy<NpgsqlDataSource>? _dataSourceSingleton;

// ReSharper disable once UnusedAutoPropertyAccessor.Global
public required IConfiguration Config { private get; init; }
public DbSet<ImageInReply> ImageInReplies => Set<ImageInReply>();
Expand All @@ -152,7 +148,7 @@ protected static void OnModelCreatingWithFid(ModelBuilder b, uint fid) =>
[SuppressMessage("Style", "IDE0058:Expression value is never used")]
protected override void OnConfiguring(DbContextOptionsBuilder options)
{
options.UseNpgsql(GetNpgsqlDataSource(Config.GetConnectionString("Main")).Value, OnConfiguringNpgsql)
options.UseNpgsql(Config.GetConnectionString("Main"), OnConfiguringNpgsql)
.ReplaceService<IModelCacheKeyFactory, TModelCacheKeyFactory>()
.ReplaceService<IRelationalTransactionFactory, NoSavePointTransactionFactory>()
.AddInterceptors(UseCurrentXactIdAsConcurrencyTokenCommandInterceptor.Instance)
Expand Down Expand Up @@ -180,14 +176,4 @@ protected override void OnModelCreating(ModelBuilder b)
}

protected virtual void OnConfiguringNpgsql(NpgsqlDbContextOptionsBuilder builder) { }
protected virtual void OnBuildingNpgsqlDataSource(NpgsqlDataSourceBuilder builder) { }

[SuppressMessage("Critical Code Smell", "S2696:Instance members should not write to \"static\" fields")]
private Lazy<NpgsqlDataSource> GetNpgsqlDataSource(string? connectionString) =>
_dataSourceSingleton ??= new(() =>
{
var dataSourceBuilder = new NpgsqlDataSourceBuilder(connectionString);
OnBuildingNpgsqlDataSource(dataSourceBuilder);
return dataSourceBuilder.Build();
});
}

0 comments on commit 8d636a0

Please sign in to comment.