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

Installing an extension that defines extra types results in types not being found #2515

Open
xwipeoutx opened this issue Mar 2, 2023 · 2 comments

Comments

@xwipeoutx
Copy link

xwipeoutx commented Mar 2, 2023

I have the following configuration to install postgis for me:

options.Storage.ExtendedSchemaObjects.Add(new Extension("postgis"));

And the database is created with the extension just fine with this:

await store.Storage.ApplyAllConfiguredChangesToDatabaseAsync();

However, in order to use the types that the extension provides, I'm need to manually do the following:

using (var conn = store.Storage.Database.CreateConnection())
{
    conn.Open();
    conn.ReloadTypes();
    conn.Close();
}

or I get the error:

The NpgsqlDbType 'Geometry' isn't present in your database. You may need to install an extension or upgrade to a newer version.

This is a little clunky, it would be nice if Marten did this for me.

Additionally, if I've set up multi-tenancy via

options.MultiTenantedWithSingleServer(masterConnectionString);

Then the extension is never created - it looks like SingleServerMultiTenancy is never applying any of the extended schema objects defined earlier.

I've worked around this with my own implementation of ITenancy which adds the following to buildDatabase, but it's pretty clunky as well (I'm sure I can use the migrator in here somehow, but this gets me out of the weeds):

    protected override MartenDatabase buildDatabase(string databaseName, string connectionString)
    {
        var connectionFactory = new ConnectionFactory(connectionString);
        var martenDatabase = new MartenDatabase(_options, connectionFactory, databaseName);
        
        // TODO: We can probably use the migrator here or something
        using var conn = martenDatabase.CreateConnection();
        conn.Open();
        conn.Execute("CREATE EXTENSION IF NOT EXISTS \"postgis\";");
        conn.ReloadTypes();
        conn.Close();

        return martenDatabase;
    }

I have put a gist up with this scenario here: https://gist.github.com/xwipeoutx/39e55935e36361095e5e2dc32b711c06

Proposed solution:

  • Make sure any schema changes that could cause a change to the ADO types call connection.ReloadTypes()
  • Have new databases created via SingleServerMultiTenancy have the extended schema objects applied to them, including reloading the types above
@oskardudycz
Copy link
Collaborator

@xwipeoutx that sounds fair to me. Would you be open to contributing to add that? We could try to guide you.

@oskardudycz
Copy link
Collaborator

oskardudycz commented Jul 22, 2023

@xwipeoutx, regarding

Have new databases created via SingleServerMultiTenancy have the extended schema objects applied to them, including reloading the types above

See the PR: #2637

Regarding the ReloadTypes scenario, I need to do more investigation.

mysticmind added a commit that referenced this issue Dec 3, 2023
…type cache

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

fixes GH-2515
mysticmind added a commit that referenced this issue Dec 3, 2023
…type cache

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

fixes GH-2515
mysticmind added a commit to JasperFx/weasel that referenced this issue Dec 4, 2023
…sions

- Add funtionality to auto reload Npgsql types for PG extensions added via schema objects
- Add unit test

This fixes Marten issue JasperFx/marten#2515
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants