Skip to content

Commit

Permalink
feat: use postgres module for storage
Browse files Browse the repository at this point in the history
  • Loading branch information
SonicGD committed Oct 30, 2024
1 parent 637f5e2 commit e1415ae
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 27 deletions.
23 changes: 19 additions & 4 deletions src/Sitko.Core.Storage.Metadata.Postgres/ApplicationExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using JetBrains.Annotations;
using Sitko.Core.App;
using Sitko.Core.Db.Postgres;
using Sitko.Core.Storage.Metadata.Postgres.DB;

namespace Sitko.Core.Storage.Metadata.Postgres;

Expand All @@ -9,18 +11,31 @@ public static class ApplicationExtensions
public static Application AddPostgresStorageMetadata<TStorageOptions>(this Application application,
Action<IApplicationContext, PostgresStorageMetadataModuleOptions<TStorageOptions>> configure,
string? optionsKey = null)
where TStorageOptions : StorageOptions =>
application
where TStorageOptions : StorageOptions
{
AddPostgresDatabase<TStorageOptions>(application);
return application
.AddModule<PostgresStorageMetadataModule<TStorageOptions>,
PostgresStorageMetadataModuleOptions<TStorageOptions>>(
configure, optionsKey);
}

public static Application AddPostgresStorageMetadata<TStorageOptions>(this Application application,
Action<PostgresStorageMetadataModuleOptions<TStorageOptions>>? configure = null, string? optionsKey = null)
where TStorageOptions : StorageOptions =>
application
where TStorageOptions : StorageOptions
{
AddPostgresDatabase<TStorageOptions>(application);
return application
.AddModule<PostgresStorageMetadataModule<TStorageOptions>,
PostgresStorageMetadataModuleOptions<TStorageOptions>>(
configure, optionsKey);
}

private static void AddPostgresDatabase<TStorageOptions>(Application application)
where TStorageOptions : StorageOptions =>
application.AddPostgresDatabase<StorageDbContext>(options =>
{
options.Schema = StorageDbContext.Schema;
}, $"Storage:Metadata:Postgres:{typeof(TStorageOptions).Name}");
}

Original file line number Diff line number Diff line change
@@ -1,32 +1,11 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using Sitko.Core.App;
using Sitko.Core.Storage.Metadata.Postgres.DB;

namespace Sitko.Core.Storage.Metadata.Postgres;
namespace Sitko.Core.Storage.Metadata.Postgres;

public class
PostgresStorageMetadataModule<TStorageOptions> : BaseStorageMetadataModule<TStorageOptions,
PostgresStorageMetadataProvider<TStorageOptions>, PostgresStorageMetadataModuleOptions<TStorageOptions>>
where TStorageOptions : StorageOptions
{
public override string OptionsKey => $"Storage:Metadata:Postgres:{typeof(TStorageOptions).Name}";
public override string[] OptionKeys => new[] { "Storage:Metadata:Postgres:Default", OptionsKey };

public override void ConfigureServices(IApplicationContext applicationContext, IServiceCollection services,
PostgresStorageMetadataModuleOptions<TStorageOptions> startupOptions)
{
base.ConfigureServices(applicationContext, services, startupOptions);
services.AddDbContextFactory<StorageDbContext>((serviceProvider, builder) =>
{
var options = serviceProvider
.GetRequiredService<IOptions<PostgresStorageMetadataModuleOptions<TStorageOptions>>>();
builder.UseNpgsql(options.Value.GetConnectionString(), optionsBuilder =>
{
optionsBuilder.MigrationsHistoryTable("__EFMigrationsHistory", StorageDbContext.Schema);
});
});
}
public override string[] OptionKeys => ["Storage:Metadata:Postgres:Default", OptionsKey];
}

0 comments on commit e1415ae

Please sign in to comment.