Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use postgres module for storage #1039

Merged
merged 4 commits into from
Oct 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 20 additions & 4 deletions src/Sitko.Core.Storage.Metadata.Postgres/ApplicationExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using JetBrains.Annotations;
using Microsoft.Extensions.Hosting;
using Sitko.Core.App;
using Sitko.Core.Db.Postgres;
using Sitko.Core.Storage.Metadata.Postgres.DB;

namespace Sitko.Core.Storage.Metadata.Postgres;

Expand Down Expand Up @@ -30,18 +32,32 @@ public static ISitkoCoreApplicationBuilder AddPostgresStorageMetadata<TStorageOp
this ISitkoCoreApplicationBuilder applicationBuilder,
Action<IApplicationContext, PostgresStorageMetadataModuleOptions<TStorageOptions>> configure,
string? optionsKey = null)
where TStorageOptions : StorageOptions =>
applicationBuilder
where TStorageOptions : StorageOptions
{
AddPostgresDatabase<TStorageOptions>(applicationBuilder);
return applicationBuilder
.AddModule<PostgresStorageMetadataModule<TStorageOptions>,
PostgresStorageMetadataModuleOptions<TStorageOptions>>(
configure, optionsKey);
}

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

private static void AddPostgresDatabase<TStorageOptions>(ISitkoCoreApplicationBuilder applicationBuilder)
where TStorageOptions : StorageOptions =>
applicationBuilder.AddPostgresDatabase<StorageDbContext>(options =>
{
options.EnableJsonConversion = true;
options.Schema = StorageDbContext.Schema;
}, $"Storage:Metadata:Postgres:{typeof(TStorageOptions).Name}");
}
Original file line number Diff line number Diff line change
@@ -1,35 +1,10 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using Npgsql;
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>>
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>>>();
var dataSourceBuilder = new NpgsqlDataSourceBuilder(options.Value.GetConnectionString());
dataSourceBuilder.EnableDynamicJson();
var dataSource = dataSourceBuilder.Build();
builder.UseNpgsql(dataSource, optionsBuilder =>
{
optionsBuilder.MigrationsHistoryTable("__EFMigrationsHistory", StorageDbContext.Schema);
});
});
}
public override string[] OptionKeys => ["Storage:Metadata:Postgres:Default", OptionsKey];
}
3 changes: 2 additions & 1 deletion src/Sitko.Core.Xunit/DbBaseTestScope.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ protected void AddDbContext<TDbContext>(IHostApplicationBuilder applicationBuild
{
if (GetConfig(applicationContext.Configuration).UsePostgres)
{
moduleOptions.Database = $"{Id}_{dbName}";
var fullDbName = $"{Id.ToString()[..8]}_{dbName}";
moduleOptions.Database = fullDbName;
moduleOptions.EnableSensitiveLogging = true;
moduleOptions.IncludeErrorDetails = true;
moduleOptions.EnableNpgsqlPooling = false;
Expand Down
4 changes: 2 additions & 2 deletions tests/Sitko.Core.Pdf.Tests/PdfTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ public async Task Url()
var scope = await GetScopeAsync();
var renderer = scope.GetService<IPdfRenderer>();

var url = "https://github.com";
var url = "https://www.wikipedia.org/";
var bytes = await renderer.GetPdfByUrlAsync(url);
Assert.NotEmpty(bytes);

using var pdf = new PdfDocument(new PdfReader(new MemoryStream(bytes)));
var text = PdfTextExtractor.GetTextFromPage(pdf.GetPage(1), new LocationTextExtractionStrategy());
pdf.Close();

Assert.Contains("GitHub", text);
Assert.Contains("The Free Encyclopedia", text);
}

[Fact]
Expand Down