From 75de9c8137fcad57cd5b07c188bda1c23ec2829a Mon Sep 17 00:00:00 2001 From: mysticmind Date: Sun, 3 Dec 2023 16:43:43 +0530 Subject: [PATCH] Add ReloadTypes function to document store advanced to reload npgsql type cache - Add ReloadTypes function to document store advanced to reload npgsql type cache - Add unit test - Add docs fixes GH-2515 --- docs/schema/extensions.md | 7 ++++ ...bility_to_add_custom_pg_type_fetch_data.cs | 34 +++++++++++++++++++ src/Marten/AdvancedOperations.cs | 23 +++++++++++++ src/Marten/Storage/IMartenDatabase.cs | 6 ++++ src/Marten/Storage/MartenDatabase.cs | 7 ++++ src/Marten/Storage/StandinDatabase.cs | 5 +++ 6 files changed, 82 insertions(+) create mode 100644 src/DocumentDbTests/Configuration/ability_to_add_custom_pg_type_fetch_data.cs diff --git a/docs/schema/extensions.md b/docs/schema/extensions.md index 75e9e8b8b53..06b6a6d6725 100644 --- a/docs/schema/extensions.md +++ b/docs/schema/extensions.md @@ -159,3 +159,10 @@ await theStore.Storage.ApplyAllConfiguredChangesToDatabaseAsync(); ``` snippet source | anchor + +In some cases where you load a specific extension like [PostGIS](https://postgis.net/), it uses custom Postgres types like `geometry` and other custom types. Npgsql typically caches the type mappings so after installing the extension or when you create any new custom Postgres types, it is required to reload the cache for Npgsql to recognize those custom Postgres types. Npgsql provides a handy function `NpgsqlConnection.ReloadTypes()`. + +Marten provides a mechanism via `DocumentStore.Advanced` to invoke `ReloadTypes()` as needed as per the use case at hand. `store.Advanced.ReloadTypes()` will run this for all tenant databases. There is also a method to run it for a specific tenant database using `Store.Advanced.ReloadTypes(tenanId)`. + + + diff --git a/src/DocumentDbTests/Configuration/ability_to_add_custom_pg_type_fetch_data.cs b/src/DocumentDbTests/Configuration/ability_to_add_custom_pg_type_fetch_data.cs new file mode 100644 index 00000000000..b0887167719 --- /dev/null +++ b/src/DocumentDbTests/Configuration/ability_to_add_custom_pg_type_fetch_data.cs @@ -0,0 +1,34 @@ +using System.Threading.Tasks; +using Marten.Testing; +using Marten.Testing.Harness; +using Shouldly; +using Weasel.Postgresql; +using Xunit; + +namespace DocumentDbTests.Configuration; + +public class ability_to_add_custom_pg_type_fetch_data: OneOffConfigurationsContext +{ + [Fact] + public async Task can_register_a_custom_feature_and_reload_types() + { + await using (var conn = theStore.CreateConnection()) + { + await conn.OpenAsync(); + var cmd = conn.CreateCommand( + $"DROP TYPE IF EXISTS {SchemaName}.mood;CREATE TYPE {SchemaName}.mood AS ENUM ('sad', 'ok', 'happy');"); + await cmd.ExecuteNonQueryAsync(); + } + + await theStore.Advanced.ReloadTypes(); + + await using (var conn = theStore.CreateConnection()) + { + await conn.OpenAsync(); + var cmd = conn.CreateCommand($"select 'happy'::{SchemaName}.mood"); + await using var reader = await cmd.ExecuteReaderAsync(); + reader.Read(); + reader.GetValue(0).ShouldBe("happy"); + } + } +} diff --git a/src/Marten/AdvancedOperations.cs b/src/Marten/AdvancedOperations.cs index 3e16e34dd23..293346a7a57 100644 --- a/src/Marten/AdvancedOperations.cs +++ b/src/Marten/AdvancedOperations.cs @@ -153,4 +153,27 @@ public Task EventProjectionScenario(Action configuration, Ca return scenario.Execute(ct); } + + /// + /// Reload types to flush Npgsql cache for a tenant + /// + /// + /// + public async Task ReloadTypes(string tenantId) + { + var tenant = await _store.Tenancy.GetTenantAsync(tenantId).ConfigureAwait(false); + await tenant.Database.ReloadTypesAsync().ConfigureAwait(false); + } + + /// + /// Reload types to flush Npgsql cache across all databases + /// + /// + /// + public async Task ReloadTypes() + { + var databases = await _store.Tenancy.BuildDatabases().ConfigureAwait(false); + foreach (var database in databases.OfType()) + await database.ReloadTypesAsync().ConfigureAwait(false); + } } diff --git a/src/Marten/Storage/IMartenDatabase.cs b/src/Marten/Storage/IMartenDatabase.cs index cb87621e59e..1eca1cd897a 100644 --- a/src/Marten/Storage/IMartenDatabase.cs +++ b/src/Marten/Storage/IMartenDatabase.cs @@ -108,4 +108,10 @@ Task> AllProjectionProgress( /// Task ProjectionProgressFor(ShardName name, CancellationToken token = default); + + /// + /// Reload types to flush Npgsql cache + /// + /// + Task ReloadTypesAsync(); } diff --git a/src/Marten/Storage/MartenDatabase.cs b/src/Marten/Storage/MartenDatabase.cs index 14a04147047..97bd845b799 100644 --- a/src/Marten/Storage/MartenDatabase.cs +++ b/src/Marten/Storage/MartenDatabase.cs @@ -76,6 +76,13 @@ public async Task ExistingTableFor(Type type) return await expected.FetchExistingAsync(conn).ConfigureAwait(false); } + public async Task ReloadTypesAsync() + { + await using var conn = CreateConnection(); + await conn.OpenAsync().ConfigureAwait(false); + await conn.ReloadTypesAsync().ConfigureAwait(false); + } + public override IFeatureSchema[] BuildFeatureSchemas() { diff --git a/src/Marten/Storage/StandinDatabase.cs b/src/Marten/Storage/StandinDatabase.cs index faf5172c096..d616ccf27f5 100644 --- a/src/Marten/Storage/StandinDatabase.cs +++ b/src/Marten/Storage/StandinDatabase.cs @@ -222,4 +222,9 @@ public Task ProjectionProgressFor(ShardName name, CancellationToken token { throw new NotImplementedException(); } + + public Task ReloadTypesAsync() + { + throw new NotImplementedException(); + } }