Skip to content

Commit

Permalink
fix(postgres): enable json conversions in npgsql
Browse files Browse the repository at this point in the history
  • Loading branch information
SonicGD committed Dec 6, 2023
1 parent b87a7a6 commit 6160717
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 12 deletions.
26 changes: 16 additions & 10 deletions src/Sitko.Core.Db.Postgres/PostgresDatabaseModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,17 +83,23 @@ private void ConfigureNpgsql(DbContextOptionsBuilder options,
{
var config = GetOptions(serviceProvider);
var schemaExtensions = new SchemaDbContextOptionsExtension(config.Schema);
options.UseNpgsql(config.CreateBuilder().ConnectionString,
builder =>
var dataSourceBuilder = new NpgsqlDataSourceBuilder(config.CreateBuilder().ConnectionString);
if (config.EnableJsonConversion)
{
dataSourceBuilder.EnableDynamicJsonMappings();
}

var dataSource = dataSourceBuilder.Build();
options.UseNpgsql(dataSource, builder =>
{
builder.MigrationsAssembly(config.MigrationsAssembly != null
? config.MigrationsAssembly.FullName
: typeof(TDbContext).Assembly.FullName);
if (schemaExtensions.IsCustomSchema)
{
builder.MigrationsAssembly(config.MigrationsAssembly != null
? config.MigrationsAssembly.FullName
: typeof(TDbContext).Assembly.FullName);
if (schemaExtensions.IsCustomSchema)
{
builder.MigrationsHistoryTable("__EFMigrationsHistory", schemaExtensions.Schema);
}
});
builder.MigrationsHistoryTable("__EFMigrationsHistory", schemaExtensions.Schema);
}
});
if (config.EnableSensitiveLogging)
{
options.EnableSensitiveDataLogging();
Expand Down
2 changes: 2 additions & 0 deletions src/Sitko.Core.Db.Postgres/PostgresDatabaseModuleConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ public class PostgresDatabaseModuleOptions<TDbContext> : BaseDbModuleOptions<TDb

public string Schema { get; set; } = "";

public bool EnableJsonConversion { get; set; } = true;

public Type GetValidatorType() => typeof(PostgresDatabaseModuleOptionsValidator<TDbContext>);

public NpgsqlConnectionStringBuilder CreateBuilder()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using Npgsql;
using Sitko.Core.App;
using Sitko.Core.Storage.Metadata.Postgres.DB;

Expand All @@ -22,11 +23,13 @@ public override void ConfigureServices(IApplicationContext applicationContext, I
{
var options = serviceProvider
.GetRequiredService<IOptions<PostgresStorageMetadataModuleOptions<TStorageOptions>>>();
builder.UseNpgsql(options.Value.GetConnectionString(), optionsBuilder =>
var dataSourceBuilder = new NpgsqlDataSourceBuilder(options.Value.GetConnectionString());
dataSourceBuilder.EnableDynamicJsonMappings();
var dataSource = dataSourceBuilder.Build();
builder.UseNpgsql(dataSource, optionsBuilder =>
{
optionsBuilder.MigrationsHistoryTable("__EFMigrationsHistory", StorageDbContext.Schema);
});
});
}
}

0 comments on commit 6160717

Please sign in to comment.