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

Consolidation of Postgres resources. #2097

Merged
merged 5 commits into from
Feb 7, 2024

Conversation

mitchdenny
Copy link
Member

@mitchdenny mitchdenny commented Feb 6, 2024

Fixes #2096

@eerhardt could you take a look at this. I fixed a bug that we have in the Postgres EF Aspire component (linked above) but I want to get your input on the solution.

The main purpose for this PR is getting rid of PostgresContainerResource and changing PostgresServiceResource to derive from ContainerResource and introducing a PublishAsContainer() method to preserve the ability to deploy Postgres as a container.v0 resource.

Also consolidates PostgresServerResource and PostgresContainerResource as part of the P4 API changes.

Still some tests failing which I'm working through.

Microsoft Reviewers: Open in CodeFlow

@mitchdenny mitchdenny self-assigned this Feb 6, 2024
@dotnet-issue-labeler dotnet-issue-labeler bot added the area-integrations Issues pertaining to Aspire Integrations packages label Feb 6, 2024
@mitchdenny mitchdenny added area-app-model Issues pertaining to the APIs in Aspire.Hosting, e.g. DistributedApplication area-integrations Issues pertaining to Aspire Integrations packages and removed area-integrations Issues pertaining to Aspire Integrations packages labels Feb 6, 2024
@mitchdenny mitchdenny added this to the preview 4 (Mar) milestone Feb 6, 2024
@@ -75,7 +75,7 @@ public static partial class AspireEFPostgreSqlExtensions
}

builder.UseLoggerFactory(null); // a workaround for https://github.com/npgsql/efcore.pg/issues/2821
});
}, serviceKey: typeof(TContext));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a breaking change. If someone is relying on getting a NpgsqlDataSource out of DI after calling AddNpgsqlDbContext, their code no longer works because it is now keyed.

// We don't provide the connection string, it's going to use the pre-registered DataSource.
// We don't register logger factory, because there is no need to: https://learn.microsoft.com/dotnet/api/microsoft.entityframeworkcore.dbcontextoptionsbuilder.useloggerfactory?view=efcore-7.0#remarks
dbContextOptionsBuilder.UseNpgsql(builder =>
dbContextOptionsBuilder.UseNpgsql(connection, builder =>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@roji - What are the differences in semantics here? Previously the Npgsql code would get the NpgsqlDataSource from DI and use it. But now this is providing a single NpgsqlConnection.

Should this instead be grabbing the NpgsqlDataSource from DI and passing it here?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we should scrap the idea of registering an NpgsqlDataSource in DI, and instead just build one here and passing it to UseNpgsql. Thoughts?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I looked back in private source control history, and it looks like we added the call to services.AddNpgsqlDataSource with a commit titled:

use Data Source as this is the recommended way (perf)

Would we still get the same perf benefits if we didn't register the NpgsqlDataSource in DI and instead just built one here and passed it in?

cc @adamsitnik

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll share what I know, but please wait for @roji for the ultimate answer.

The call to builder.Services.AddNpgsqlDataSource registers a NpgsqlDataSource factory and also a NpgsqlConnection factory that will internally resolve NpgsqlDataSource.

What I don't know is how creating an EF DB Context from NpgsqlConnection is different than from NpgsqlDataSource, namely how is it going to work when there will be a need for having more than one NpgsqlConnection

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've pushed a change to the way we register the Npgsql EF component. Now we are just passing in the connection string. There isn't a need for us to register an NpgsqlDataSource in DI. We are now following the exact code from https://www.npgsql.org/efcore/index.html#additional-configuration-for-aspnet-core-applications. @roji can you take a look?

If the user wants to also register an NpgsqlDataSource, they can do it themselves once #1403 is completed (cc @sebastienros).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@eerhardt so i revert this change?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh you did it on this branch. Cool thanks for that.

Copy link
Member

@eerhardt eerhardt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Thanks!

@@ -130,7 +111,12 @@ private static void WritePostgresDatabaseToManifest(ManifestPublishingContext co
context.Writer.WriteString("parent", postgresDatabase.Parent.Name);
}

private static void WritePostgresContainerResourceToManifest(ManifestPublishingContext context, PostgresContainerResource resource)
public static IResourceBuilder<PostgresServerResource> PublishAsContainer(this IResourceBuilder<PostgresServerResource> builder)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

XML docs on a public method?

@mitchdenny mitchdenny merged commit ae11632 into main Feb 7, 2024
8 checks passed
@mitchdenny mitchdenny deleted the mitchdenny/consolidation-postgres-resource-types branch February 7, 2024 02:11
radical pushed a commit to radical/aspire that referenced this pull request Feb 7, 2024
* Consolidation of Postgres resources.

* Fix up test cases.

* Don't register an NpgsqlDataSource in DI anymore. Just pass the connection string to UseNpgsql

* Add XML doc comments.

---------

Co-authored-by: Eric Erhardt <[email protected]>
eerhardt added a commit to eerhardt/aspire that referenced this pull request Feb 8, 2024
eerhardt added a commit that referenced this pull request Feb 8, 2024
radical pushed a commit to radical/aspire that referenced this pull request Feb 9, 2024
@github-actions github-actions bot locked and limited conversation to collaborators Apr 22, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-app-model Issues pertaining to the APIs in Aspire.Hosting, e.g. DistributedApplication area-integrations Issues pertaining to Aspire Integrations packages
Projects
None yet
3 participants