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

Add ReloadTypes function to document store advanced #2838

Closed
wants to merge 3 commits into from
Closed
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
12 changes: 12 additions & 0 deletions docs/schema/extensions.md
Original file line number Diff line number Diff line change
Expand Up @@ -159,3 +159,15 @@ 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 -->
<a id='snippet-sample_reload-types'></a>
```cs
await theStore.Advanced.ReloadTypes();
```
<sup><a href='https://github.com/JasperFx/marten/blob/master/src/DocumentDbTests/Configuration/ability_to_add_custom_pg_type_fetch_data.cs#L23-L25' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_reload-types' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
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 theStore.Storage.ApplyAllConfiguredChangesToDatabaseAsync();
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();
}

#region sample_reload-types
await theStore.Advanced.ReloadTypes();
#endregion

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();
}
}
Loading