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

feat: Extension for umbraco connection string #15186

Open
wants to merge 1 commit into
base: contrib
Choose a base branch
from

Conversation

nikcio
Copy link
Contributor

@nikcio nikcio commented Nov 13, 2023

Prerequisites

  • I have added steps to test this contribution in the description below

This fixes PR comment

Description

This PR adds the GetUmbracoConnectionString extension on the IServiceProvider. This makes it simple to get the connection string from the provider when using a custom DbContext. (See the PR comment).

This new extension method allows for the following code snippet being transformed into a much smaller example:

Before:

services.AddUmbracoDbContext<MovieContext>((serviceProvider, options) =>
{
    ConnectionStrings connectionStrings = serviceProvider.GetRequiredService<IOptionsMonitor<ConnectionStrings>>().CurrentValue;

    // Replace data directory
    string? dataDirectory = AppDomain.CurrentDomain.GetData(Constants.System.DataDirectoryName)?.ToString();
    if (string.IsNullOrEmpty(dataDirectory) is false)
    {
        connectionStrings.ConnectionString = connectionStrings.ConnectionString?.Replace(Constants.System.DataDirectoryPlaceholder, dataDirectory);
    }

    if (string.IsNullOrEmpty(connectionStrings.ProviderName))
    {
        Log.Warning("No database provider was set. ProviderName is null");
        return;
    }

    if (string.IsNullOrEmpty(connectionStrings.ConnectionString))
    {
        Log.Warning("No database provider was set. Connection string is null");
        return;
    }

    builder.UseSqlServer(connectionStrings.ConnectionString, x => 
    {
        x.MigrationsAssembly(typeof(MovieContext).Assembly.FullName);
    });
});

After:

services.AddUmbracoDbContext<MovieContext>((serviceProvider, options) =>
{
    ConnectionStrings connectionStrings = serviceProvider.GetUmbracoConnectionString();

    builder.UseSqlServer(connectionStrings.ConnectionString, x => 
    {
        x.MigrationsAssembly(typeof(MovieContext).Assembly.FullName);
    });
});

Copy link

github-actions bot commented Nov 13, 2023

Hi there @nikcio, thank you for this contribution! 👍

While we wait for one of the Core Collaborators team to have a look at your work, we wanted to let you know about that we have a checklist for some of the things we will consider during review:

  • It's clear what problem this is solving, there's a connected issue or a description of what the changes do and how to test them
  • The automated tests all pass (see "Checks" tab on this PR)
  • The level of security for this contribution is the same or improved
  • The level of performance for this contribution is the same or improved
  • Avoids creating breaking changes; note that behavioral changes might also be perceived as breaking
  • If this is a new feature, Umbraco HQ provided guidance on the implementation beforehand
  • 💡 The contribution looks original and the contributor is presumably allowed to share it

Don't worry if you got something wrong. We like to think of a pull request as the start of a conversation, we're happy to provide guidance on improving your contribution.

If you realize that you might want to make some changes then you can do that by adding new commits to the branch you created for this work and pushing new commits. They should then automatically show up as updates to this pull request.

Thanks, from your friendly Umbraco GitHub bot 🤖 🙂

@jemayn
Copy link
Contributor

jemayn commented Nov 13, 2023

This is definitely an improvement!

I haven't looked at the code at all, but I had thought it would be possible to add the sqlServerOptionsAction to the new UseUmbracoDatabaseProvider method to emulate the UseSqlServer method?

Something like this:

services.AddUmbracoDbContext<MovieContext>((serviceProvider, options) =>
{
    options.UseUmbracoDatabaseProvider(serviceProvider, x =>
    {
        x.MigrationsAssembly(typeof(MovieContext).Assembly.GetName().FullName);
    });
});

But like I said I haven't looked at the underlying code, so if that is not possible then your method should work just fine to solve the actual issue 🙂

@nikcio
Copy link
Contributor Author

nikcio commented Nov 13, 2023

That would be nice to have but the reason why I don't think it's a great fit is because the UseUmbracoDatabaseProvider supports multiple data providers (Sqlite and SqlServer) via the UseDatabaseProvider extension. So for this to work we would need an extension method for each provider which would end up just being extensions for calling UseSqlServer or UseSqlite directly which doesn't make much sense.

@jemayn
Copy link
Contributor

jemayn commented Nov 13, 2023

Ahh ok, yep that makes sense then 🙂

@busrasengul
Copy link
Contributor

Thank you @nikcio Your PR is a great improvement! Someone from the team will look into it soon! 🎉

@nul800sebastiaan
Copy link
Member

I feel like we're going to be duplicating code here, any way you can use the existing GetUmbracoConnectionString extension on IConfiguration?

From looking at the previous PR, noticed things going in Program.cs, instead of that we encourage people to use composers, in an IComposer I can get the connections string:

public class PersistenceComposer : IComposer
{
    public void Compose(IUmbracoBuilder umbracoBuilder)
    {
        var connectionString = umbracoBuilder.Config.GetUmbracoConnectionString();
    }
}

I haven't tried out much more than that, but could it be a good start to accomplish what is needed?

@nul800sebastiaan
Copy link
Member

I've re-read the whole thing now, you are calling that GetUmbracoConnectionString extension already so I think it looks good. I'll give it a spin!

@nikcio
Copy link
Contributor Author

nikcio commented Aug 29, 2024

Hey @nul800sebastiaan I can see this has been laying still for a while now how do you think we should proceed?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants