Skip to content

Commit

Permalink
Add ReloadTypes function to document store advanced to reload npgsql …
Browse files Browse the repository at this point in the history
…type cache

- Add ReloadTypes function to document store advanced to reload npgsql type cache
- Add unit test
- Add docs

fixes GH-2515
  • Loading branch information
mysticmind committed Dec 3, 2023
1 parent f28f096 commit 75de9c8
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 0 deletions.
7 changes: 7 additions & 0 deletions docs/schema/extensions.md
Original file line number Diff line number Diff line change
Expand Up @@ -159,3 +159,10 @@ await theStore.Storage.ApplyAllConfiguredChangesToDatabaseAsync();
```
<sup><a href='https://github.com/JasperFx/marten/blob/master/src/CoreTests/adding_custom_schema_objects.cs#L76-L91' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_customschemaextension' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->

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)`.

<!-- snippet: sample_reload-types -->
<!-- endSnippet -->
Original file line number Diff line number Diff line change
@@ -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");
}
}
}
23 changes: 23 additions & 0 deletions src/Marten/AdvancedOperations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -153,4 +153,27 @@ public Task EventProjectionScenario(Action<ProjectionScenario> configuration, Ca

return scenario.Execute(ct);
}

/// <summary>
/// Reload types to flush Npgsql cache for a tenant
/// </summary>
/// <param name="tenantId"></param>
/// <typeparam name="T"></typeparam>
public async Task ReloadTypes(string tenantId)
{
var tenant = await _store.Tenancy.GetTenantAsync(tenantId).ConfigureAwait(false);
await tenant.Database.ReloadTypesAsync().ConfigureAwait(false);
}

/// <summary>
/// Reload types to flush Npgsql cache across all databases
/// </summary>
/// <param name="floor"></param>
/// <typeparam name="T"></typeparam>
public async Task ReloadTypes()
{
var databases = await _store.Tenancy.BuildDatabases().ConfigureAwait(false);
foreach (var database in databases.OfType<IMartenDatabase>())
await database.ReloadTypesAsync().ConfigureAwait(false);
}
}
6 changes: 6 additions & 0 deletions src/Marten/Storage/IMartenDatabase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,4 +108,10 @@ Task<IReadOnlyList<ShardState>> AllProjectionProgress(
/// <returns></returns>
Task<long> ProjectionProgressFor(ShardName name,
CancellationToken token = default);

/// <summary>
/// Reload types to flush Npgsql cache
/// </summary>
/// <returns></returns>
Task ReloadTypesAsync();
}
7 changes: 7 additions & 0 deletions src/Marten/Storage/MartenDatabase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,13 @@ public async Task<Table> 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()
{
Expand Down
5 changes: 5 additions & 0 deletions src/Marten/Storage/StandinDatabase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -222,4 +222,9 @@ public Task<long> ProjectionProgressFor(ShardName name, CancellationToken token
{
throw new NotImplementedException();
}

public Task ReloadTypesAsync()
{
throw new NotImplementedException();
}
}

0 comments on commit 75de9c8

Please sign in to comment.