Skip to content

Commit

Permalink
feat: add integration projections
Browse files Browse the repository at this point in the history
  • Loading branch information
emalfroy authored and ArneD committed Jan 31, 2024
1 parent 3f0afab commit 9eed859
Show file tree
Hide file tree
Showing 36 changed files with 1,338 additions and 7 deletions.
7 changes: 7 additions & 0 deletions PostalRegistry.sln
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PostalRegistry.Producer", "
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PostalRegistry.Producer.Snapshot.Oslo", "src\PostalRegistry.Producer.Snapshot.Oslo\PostalRegistry.Producer.Snapshot.Oslo.csproj", "{B6B9729E-0EF7-48C8-88B1-BB4C437D20E6}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PostalRegistry.Projections.Integration", "src\PostalRegistry.Projections.Integration\PostalRegistry.Projections.Integration.csproj", "{8049759B-20E1-4AEE-9086-5422C437D742}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -137,6 +139,10 @@ Global
{B6B9729E-0EF7-48C8-88B1-BB4C437D20E6}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B6B9729E-0EF7-48C8-88B1-BB4C437D20E6}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B6B9729E-0EF7-48C8-88B1-BB4C437D20E6}.Release|Any CPU.Build.0 = Release|Any CPU
{8049759B-20E1-4AEE-9086-5422C437D742}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{8049759B-20E1-4AEE-9086-5422C437D742}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8049759B-20E1-4AEE-9086-5422C437D742}.Release|Any CPU.ActiveCfg = Release|Any CPU
{8049759B-20E1-4AEE-9086-5422C437D742}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -159,6 +165,7 @@ Global
{D13F985B-F16B-4F38-B170-F86E5D447926} = {81BE6CDF-434E-42AB-9CB1-B5E7A26DC317}
{DAC34E64-9C52-495E-9BF6-868E3DFE99EE} = {81BE6CDF-434E-42AB-9CB1-B5E7A26DC317}
{B6B9729E-0EF7-48C8-88B1-BB4C437D20E6} = {81BE6CDF-434E-42AB-9CB1-B5E7A26DC317}
{8049759B-20E1-4AEE-9086-5422C437D742} = {81BE6CDF-434E-42AB-9CB1-B5E7A26DC317}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {14E5CC1E-7FCC-4086-A7DD-CEFDAD492801}
Expand Down
3 changes: 2 additions & 1 deletion paket.dependencies
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ nuget CsvHelper 27.2.1

nuget NetTopologySuite 2.4.0
nuget Dapper 2.0.123

nuget NodaTime 3.1.6

// For more healtchecks, look at https://github.com/Xabaril/AspNetCore.Diagnostics.HealthChecks
nuget AspNetCore.HealthChecks.SqlServer 6.0.2
Expand Down Expand Up @@ -54,6 +54,7 @@ nuget Be.Vlaanderen.Basisregisters.ProjectionHandling.Runner 12.0.1
nuget Be.Vlaanderen.Basisregisters.ProjectionHandling.Runner.SqlServer 12.0.1
nuget Be.Vlaanderen.Basisregisters.ProjectionHandling.Runner.Npgsql 12.0.1
nuget Be.Vlaanderen.Basisregisters.ProjectionHandling.Connector.Testing 12.0.1
nuget Be.Vlaanderen.Basisregisters.ProjectionHandling.Testing.Xunit 12.0.1
nuget Be.Vlaanderen.Basisregisters.ProjectionHandling.Syndication 12.0.1

nuget Be.Vlaanderen.Basisregisters.Projector 13.1.0
Expand Down
8 changes: 7 additions & 1 deletion paket.lock
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,12 @@ NUGET
Microsoft.Extensions.Http (>= 6.0)
Microsoft.Extensions.Logging (>= 6.0)
Microsoft.SyndicationFeed.ReaderWriter (>= 1.0.2)
Be.Vlaanderen.Basisregisters.ProjectionHandling.Testing.Xunit (12.0.1)
Be.Vlaanderen.Basisregisters.ProjectionHandling.Connector.Testing (12.0.1)
Be.Vlaanderen.Basisregisters.ProjectionHandling.SqlStreamStore (12.0.1)
Microsoft.EntityFrameworkCore (>= 6.0.3)
Microsoft.Extensions.Logging (>= 6.0)
xunit (>= 2.4.1)
Be.Vlaanderen.Basisregisters.Projector (13.1)
Autofac (>= 6.3)
Autofac.Extensions.DependencyInjection (>= 7.2)
Expand Down Expand Up @@ -814,7 +820,7 @@ NUGET
Newtonsoft.Json (>= 9.0.1)
NJsonSchema (>= 10.6.10)
NJsonSchema.CodeGeneration (>= 10.6.10)
NodaTime (3.0.10)
NodaTime (3.1.6)
System.Runtime.CompilerServices.Unsafe (>= 4.7.1)
NodaTime.Serialization.JsonNet (3.0)
Newtonsoft.Json (>= 12.0.1)
Expand Down
1 change: 1 addition & 0 deletions src/EF.MigrationsHelper/EF.MigrationsHelper.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
<ProjectReference Include="..\PostalRegistry.Projections.Legacy\PostalRegistry.Projections.Legacy.csproj" />
<ProjectReference Include="..\PostalRegistry.Projections.LastChangedList\PostalRegistry.Projections.LastChangedList.csproj" />
<ProjectReference Include="..\PostalRegistry.Projections.Syndication\PostalRegistry.Projections.Syndication.csproj" />
<ProjectReference Include="..\PostalRegistry.Projections.Integration\PostalRegistry.Projections.Integration.csproj" />
<ProjectReference Include="..\PostalRegistry.Producer\PostalRegistry.Producer.csproj" />
<ProjectReference Include="..\PostalRegistry.Producer.Snapshot.Oslo\PostalRegistry.Producer.Snapshot.Oslo.csproj" />
</ItemGroup>
Expand Down
4 changes: 3 additions & 1 deletion src/EF.MigrationsHelper/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
"SyndicationProjections": "Server=(localdb)\\mssqllocaldb;Database=EFProviders.InMemory.PostalRegistry;Trusted_Connection=True;",
"SyndicationProjectionsAdmin": "Server=(localdb)\\mssqllocaldb;Database=EFProviders.InMemory.PostalRegistry;Trusted_Connection=True;",
"LastChangedListAdmin": "Server=(localdb)\\mssqllocaldb;Database=EFProviders.InMemory.MunicipalityRegistry;Trusted_Connection=True;",
"ProducerProjectionsAdmin": "Server=(localdb)\\mssqllocaldb;Database=EFProviders.InMemory.MunicipalityRegistry;Trusted_Connection=True;"
"ProducerProjectionsAdmin": "Server=(localdb)\\mssqllocaldb;Database=EFProviders.InMemory.MunicipalityRegistry;Trusted_Connection=True;",
"IntegrationProjections": ".",
"IntegrationProjectionsAdmin": "."
},

"Serilog": {
Expand Down
4 changes: 4 additions & 0 deletions src/PostalRegistry.Infrastructure/Schema.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ public static class Schema

public const string Producer = "PostalRegistryProducer";
public const string ProducerSnapshotOslo = "PostalRegistryProducerSnapshotOslo";

public const string Integration = "integration_postal";
}

public static class MigrationTables
Expand All @@ -22,5 +24,7 @@ public static class MigrationTables

public const string Producer = "__EFMigrationsHistoryProducer";
public const string ProducerSnapshotOslo = "__EFMigrationsHistoryProducerSnapshotOslo";

public const string Integration = "__EFMigrationsHistory";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
namespace PostalRegistry.Projections.Integration.Infrastructure
{
using System;
using Autofac;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using PostalRegistry.Infrastructure;

public class IntegrationModule : Module
{
public IntegrationModule(
IConfiguration configuration,
IServiceCollection services,
ILoggerFactory loggerFactory)
{
var logger = loggerFactory.CreateLogger<IntegrationModule>();
var connectionString = configuration.GetConnectionString("IntegrationProjections");

var hasConnectionString = !string.IsNullOrWhiteSpace(connectionString);
if (hasConnectionString)
RunOnNpgSqlServer(services, connectionString);
else
RunInMemoryDb(services, loggerFactory, logger);

logger.LogInformation(
"Added {Context} to services:" +
Environment.NewLine +
"\tSchema: {Schema}" +
Environment.NewLine +
"\tTableName: {TableName}",
nameof(IntegrationContext), Schema.Integration, MigrationTables.Integration);
}

private static void RunOnNpgSqlServer(
IServiceCollection services,
string backofficeProjectionsConnectionString)
{
services
.AddNpgsql<IntegrationContext>(backofficeProjectionsConnectionString, sqlServerOptions =>
{
sqlServerOptions.EnableRetryOnFailure();
sqlServerOptions.MigrationsHistoryTable(MigrationTables.Integration, Schema.Integration);
});
}

private static void RunInMemoryDb(
IServiceCollection services,
ILoggerFactory loggerFactory,
ILogger logger)
{
services
.AddDbContext<IntegrationContext>(options => options
.UseLoggerFactory(loggerFactory)
.UseInMemoryDatabase(Guid.NewGuid().ToString(), _ => { }));

logger.LogWarning("Running InMemory for {Context}!", nameof(IntegrationContext));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace PostalRegistry.Projections.Integration.Infrastructure
{
public class IntegrationOptions
{
public string Namespace { get; set; }
public bool Enabled { get; set; }
}
}
20 changes: 20 additions & 0 deletions src/PostalRegistry.Projections.Integration/IntegrationContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
namespace PostalRegistry.Projections.Integration
{
using Be.Vlaanderen.Basisregisters.ProjectionHandling.Runner;
using Microsoft.EntityFrameworkCore;
using PostalRegistry.Infrastructure;

public class IntegrationContext : RunnerDbContext<IntegrationContext>
{
public override string ProjectionStateSchema => Schema.Integration;

public DbSet<PostalLatestItem> PostalLatestItems => Set<PostalLatestItem>();

// This needs to be here to please EF
public IntegrationContext() { }

// This needs to be DbContextOptions<T> for Autofac!
public IntegrationContext(DbContextOptions<IntegrationContext> options)
: base(options) { }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
namespace PostalRegistry.Projections.Integration
{
using Be.Vlaanderen.Basisregisters.ProjectionHandling.Runner.Npgsql;
using Microsoft.EntityFrameworkCore;
using PostalRegistry.Infrastructure;

public class IntegrationContextMigrationFactory : NpgsqlRunnerDbContextMigrationFactory<IntegrationContext>
{
public IntegrationContextMigrationFactory()
: base("IntegrationProjectionsAdmin", HistoryConfiguration) { }

private static MigrationHistoryConfiguration HistoryConfiguration =>
new MigrationHistoryConfiguration
{
Schema = Schema.Integration,
Table = MigrationTables.Integration
};

protected override IntegrationContext CreateContext(
DbContextOptions<IntegrationContext> migrationContextOptions) =>
new IntegrationContext(migrationContextOptions);
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 9eed859

Please sign in to comment.