Skip to content

Commit

Permalink
Use NpgsqlDataSource
Browse files Browse the repository at this point in the history
  • Loading branch information
gunndabad committed Jul 25, 2024
1 parent 1d114b2 commit 72e32d9
Show file tree
Hide file tree
Showing 14 changed files with 45 additions and 61 deletions.
1 change: 1 addition & 0 deletions TeachingRecordSystem/Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
<PackageVersion Include="Microsoft.PowerPlatform.Dataverse.Client" Version="1.1.27" />
<PackageVersion Include="Microsoft.PowerPlatform.Dataverse.Client.Dynamics" Version="1.1.27" />
<PackageVersion Include="Moq" Version="4.20.70" />
<PackageVersion Include="Npgsql.DependencyInjection" Version="8.0.3" />
<PackageVersion Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="8.0.0" />
<PackageVersion Include="OpenIddict.AspNetCore" Version="5.2.0" />
<PackageVersion Include="OpenIddict.EntityFrameworkCore" Version="5.2.0" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public static TrsDbContext Create(string connectionString, int? commandTimeout =

public DbSet<TpsEstablishmentType> TpsEstablishmentTypes => Set<TpsEstablishmentType>();

public static void ConfigureOptions(DbContextOptionsBuilder optionsBuilder, string connectionString, int? commandTimeout = null)
public static void ConfigureOptions(DbContextOptionsBuilder optionsBuilder, string? connectionString = null, int? commandTimeout = null)
{
Action<NpgsqlDbContextOptionsBuilder> configureOptions = o => o.CommandTimeout(commandTimeout);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public TrsDbContext CreateDbContext(string[] args)
.AddUserSecrets<TrsDesignTimeDbContextFactory>(optional: true) // Optional for CI
.Build();

var connectionString = configuration.GetRequiredConnectionString("DefaultConnection");
var connectionString = configuration.GetPostgresConnectionString();

var optionsBuilder = new DbContextOptionsBuilder<TrsDbContext>();
TrsDbContext.ConfigureOptions(optionsBuilder, connectionString);
Expand Down
21 changes: 15 additions & 6 deletions TeachingRecordSystem/src/TeachingRecordSystem.Core/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,23 @@ public static IHostApplicationBuilder AddDatabase(this IHostApplicationBuilder b
{
var pgConnectionString = GetPostgresConnectionString(builder.Configuration);

builder.Services.AddDbContext<TrsDbContext>(
options => TrsDbContext.ConfigureOptions(options, pgConnectionString),
builder.Services.AddDatabase(pgConnectionString);

return builder;
}

public static IServiceCollection AddDatabase(this IServiceCollection services, string connectionString)
{
services.AddNpgsqlDataSource(connectionString, builder => builder.Name = "TrsDb");

services.AddDbContext<TrsDbContext>(
options => TrsDbContext.ConfigureOptions(options),
contextLifetime: ServiceLifetime.Transient,
optionsLifetime: ServiceLifetime.Singleton);

builder.Services.AddDbContextFactory<TrsDbContext>(options => TrsDbContext.ConfigureOptions(options, pgConnectionString));
services.AddDbContextFactory<TrsDbContext>(options => TrsDbContext.ConfigureOptions(options));

return builder;
return services;
}

public static IHostApplicationBuilder AddHangfire(this IHostApplicationBuilder builder)
Expand All @@ -48,11 +57,11 @@ public static IHostApplicationBuilder AddHangfire(this IHostApplicationBuilder b
{
var pgConnectionString = GetPostgresConnectionString(builder.Configuration);

builder.Services.AddHangfire(configuration => configuration
builder.Services.AddHangfire((sp, configuration) => configuration
.SetDataCompatibilityLevel(CompatibilityLevel.Version_170)
.UseSimpleAssemblyNameTypeSerializer()
.UseRecommendedSerializerSettings()
.UsePostgreSqlStorage(o => o.UseNpgsqlConnection(pgConnectionString)));
.UsePostgreSqlStorage(o => o.UseExistingNpgsqlConnection(sp.GetRequiredService<NpgsqlDataSource>().CreateConnection())));
}

return builder;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
namespace TeachingRecordSystem.Core.Services.TrsDataSync;

public class TrsDataSyncHelper(
IDbContextFactory<TrsDbContext> dbContextFactory,
NpgsqlDataSource trsDbDataSource,
[FromKeyedServices(TrsDataSyncService.CrmClientName)] IOrganizationServiceAsync2 organizationService,
ReferenceDataCache referenceDataCache,
IClock clock)
Expand Down Expand Up @@ -114,10 +114,7 @@ public async Task DeleteRecords(string modelType, IReadOnlyCollection<Guid> ids,

var modelTypeSyncInfo = GetModelTypeSyncInfo(modelType);

await using var dbContext = await dbContextFactory.CreateDbContextAsync();

var connection = (NpgsqlConnection)dbContext.Database.GetDbConnection();
await connection.OpenAsync(cancellationToken);
await using var connection = await trsDbDataSource.OpenConnectionAsync(cancellationToken);

using (var cmd = connection.CreateCommand())
{
Expand All @@ -131,10 +128,7 @@ public async Task DeleteRecords(string modelType, IReadOnlyCollection<Guid> ids,
{
var modelTypeSyncInfo = GetModelTypeSyncInfo(modelType);

await using var dbContext = await dbContextFactory.CreateDbContextAsync();

var connection = (NpgsqlConnection)dbContext.Database.GetDbConnection();
await connection.OpenAsync();
await using var connection = await trsDbDataSource.OpenConnectionAsync();

using (var cmd = connection.CreateCommand())
{
Expand Down Expand Up @@ -190,10 +184,7 @@ public async Task<int> SyncPersons(IReadOnlyCollection<Contact> entities, bool i

var modelTypeSyncInfo = GetModelTypeSyncInfo<Person>(ModelTypes.Person);

await using var dbContext = await dbContextFactory.CreateDbContextAsync();

var connection = (NpgsqlConnection)dbContext.Database.GetDbConnection();
await connection.OpenAsync(cancellationToken);
await using var connection = await trsDbDataSource.OpenConnectionAsync(cancellationToken);
using var txn = await connection.BeginTransactionAsync(cancellationToken);

using (var createTempTableCommand = connection.CreateCommand())
Expand Down Expand Up @@ -249,7 +240,6 @@ public async Task<int> SyncPersons(IReadOnlyCollection<Contact> entities, bool i

await txn.DisposeAsync();
await connection.DisposeAsync();
await dbContext.DisposeAsync();

return await SyncPersons(entitiesExceptFailedOne, ignoreInvalid, cancellationToken);
}
Expand Down Expand Up @@ -341,10 +331,7 @@ private async Task<int> SyncMandatoryQualifications(

var modelTypeSyncInfo = GetModelTypeSyncInfo<MandatoryQualification>(ModelTypes.MandatoryQualification);

await using var dbContext = await dbContextFactory.CreateDbContextAsync();

var connection = (NpgsqlConnection)dbContext.Database.GetDbConnection();
await connection.OpenAsync(cancellationToken);
await using var connection = await trsDbDataSource.OpenConnectionAsync(cancellationToken);

var toSync = mqs.ToList();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@
<PackageReference Include="Microsoft.Extensions.Options.DataAnnotations" />
<PackageReference Include="Microsoft.PowerPlatform.Dataverse.Client" />
<PackageReference Include="Microsoft.PowerPlatform.Dataverse.Client.Dynamics" />
<PackageReference Include="Npgsql.DependencyInjection" />
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" />
<PackageReference Include="OpenIddict.EntityFrameworkCore" />
<PackageReference Include="Optional" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,14 @@ public EstablishmentRefresherTests(
DbFixture = dbFixture;
Clock = new();

var dbContextFactory = dbFixture.GetDbContextFactory();

Helper = new TrsDataSyncHelper(
dbContextFactory,
dbFixture.GetDataSource(),
organizationService,
referenceDataCache,
Clock);

TestData = new TestData(
dbContextFactory,
dbFixture.GetDbContextFactory(),
organizationService,
referenceDataCache,
Clock,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,14 @@ public TpsEstablishmentRefresherTests(
DbFixture = dbFixture;
Clock = new();

var dbContextFactory = dbFixture.GetDbContextFactory();

Helper = new TrsDataSyncHelper(
dbContextFactory,
dbFixture.GetDataSource(),
organizationService,
referenceDataCache,
Clock);

TestData = new TestData(
dbContextFactory,
dbFixture.GetDbContextFactory(),
organizationService,
referenceDataCache,
Clock,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,14 @@ public PersonMatchingServiceTests(
DbFixture = dbFixture;
Clock = new();

var dbContextFactory = dbFixture.GetDbContextFactory();

var syncHelper = new TrsDataSyncHelper(
dbContextFactory,
dbFixture.GetDataSource(),
organizationService,
referenceDataCache,
Clock);

TestData = new TestData(
dbContextFactory,
dbFixture.GetDbContextFactory(),
organizationService,
referenceDataCache,
Clock,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,14 @@ public TrsDataSyncHelperTests(
DbFixture = dbFixture;
Clock = new();

var dbContextFactory = dbFixture.GetDbContextFactory();

Helper = new TrsDataSyncHelper(
dbContextFactory,
dbFixture.GetDataSource(),
organizationService,
referenceDataCache,
Clock);

TestData = new TestData(
dbContextFactory,
dbFixture.GetDbContextFactory(),
organizationService,
referenceDataCache,
Clock,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,14 @@ public TrsDataSyncServiceFixture(
DbFixture = dbFixture;
Clock = new();

var dbContextFactory = dbFixture.GetDbContextFactory();

Helper = new TrsDataSyncHelper(
dbContextFactory,
dbFixture.GetDataSource(),
organizationService,
referenceDataCache,
Clock);

TestData = new TestData(
dbContextFactory,
dbFixture.GetDbContextFactory(),
organizationService,
referenceDataCache,
Clock,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,14 @@ public TpsCsvExtractProcessorTests(
DbFixture = dbFixture;
Clock = new();

var dbContextFactory = dbFixture.GetDbContextFactory();

Helper = new TrsDataSyncHelper(
dbContextFactory,
dbFixture.GetDataSource(),
organizationService,
referenceDataCache,
Clock);

TestData = new TestData(
dbContextFactory,
dbFixture.GetDbContextFactory(),
organizationService,
referenceDataCache,
Clock,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.Extensions.DependencyInjection;
using Npgsql;
using TeachingRecordSystem.Core.DataStore.Postgres;

namespace TeachingRecordSystem.TestCommon;
Expand All @@ -9,6 +10,8 @@ public class DbFixture(DbHelper dbHelper, IServiceProvider serviceProvider)

public IServiceProvider Services { get; } = serviceProvider;

public NpgsqlDataSource GetDataSource() => Services.GetRequiredService<NpgsqlDataSource>();

public TrsDbContext GetDbContext() => Services.GetRequiredService<TrsDbContext>();

public IDbContextFactory<TrsDbContext> GetDbContextFactory() => Services.GetRequiredService<IDbContextFactory<TrsDbContext>>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,31 +8,26 @@

namespace TeachingRecordSystem.TestCommon;

public class DbHelper(string connectionString)
public class DbHelper(IDbContextFactory<TrsDbContext> dbContextFactory)
{
private Respawner? _respawner;
private readonly SemaphoreSlim _schemaLock = new(1, 1);
private bool _haveResetSchema = false;

public string ConnectionString { get; } = connectionString;
public IDbContextFactory<TrsDbContext> DbContextFactory { get; } = dbContextFactory;

public static void ConfigureDbServices(IServiceCollection services, string connectionString)
{
services.AddDbContext<TrsDbContext>(
options => TrsDbContext.ConfigureOptions(options, connectionString),
contextLifetime: ServiceLifetime.Transient);
services.AddDatabase(connectionString);

services.AddDbContextFactory<TrsDbContext>(
options => TrsDbContext.ConfigureOptions(options, connectionString));

services.AddSingleton(new DbHelper(connectionString));
services.AddSingleton<DbHelper>();

services.AddStartupTask(sp => sp.GetRequiredService<DbHelper>().EnsureSchema());
}

public async Task ClearData()
{
using var dbContext = TrsDbContext.Create(ConnectionString);
using var dbContext = await DbContextFactory.CreateDbContextAsync();
await dbContext.Database.OpenConnectionAsync();
var connection = dbContext.Database.GetDbConnection();
await EnsureRespawner(connection);
Expand Down Expand Up @@ -63,7 +58,7 @@ public async Task EnsureSchema()

public async Task ResetSchema()
{
using var dbContext = TrsDbContext.Create(ConnectionString);
using var dbContext = await DbContextFactory.CreateDbContextAsync();

var connection = dbContext.Database.GetDbConnection();
var dbName = connection.Database;
Expand Down

0 comments on commit 72e32d9

Please sign in to comment.