From d8b9fbd324cb3b15573be1331143f6a86d6c4282 Mon Sep 17 00:00:00 2001 From: Joel Verhagen Date: Sat, 6 Apr 2019 15:43:41 -0700 Subject: [PATCH] Add DatabaseOwnerFetcher to query for current owner information Progress on https://github.com/NuGet/NuGetGallery/issues/6475 --- .../DependencyInjectionExtensions.cs | 1 + .../NuGet.Services.AzureSearch.csproj | 2 + .../DatabaseOwnerFetcher.cs | 59 +++++++++++++++++++ .../IDatabaseOwnerFetcher.cs | 13 ++++ 4 files changed, 75 insertions(+) create mode 100644 src/NuGet.Services.AzureSearch/Owners2AzureSearch/DatabaseOwnerFetcher.cs create mode 100644 src/NuGet.Services.AzureSearch/Owners2AzureSearch/IDatabaseOwnerFetcher.cs diff --git a/src/NuGet.Services.AzureSearch/DependencyInjectionExtensions.cs b/src/NuGet.Services.AzureSearch/DependencyInjectionExtensions.cs index a42be7c99..c2179dc83 100644 --- a/src/NuGet.Services.AzureSearch/DependencyInjectionExtensions.cs +++ b/src/NuGet.Services.AzureSearch/DependencyInjectionExtensions.cs @@ -217,6 +217,7 @@ public static IServiceCollection AddAzureSearch(this IServiceCollection services services.AddTransient(); services.AddTransient(); services.AddTransient(); + services.AddTransient(); services.AddTransient(); services.AddTransient(); services.AddTransient(); diff --git a/src/NuGet.Services.AzureSearch/NuGet.Services.AzureSearch.csproj b/src/NuGet.Services.AzureSearch/NuGet.Services.AzureSearch.csproj index ec0dcd0b5..cb4087e52 100644 --- a/src/NuGet.Services.AzureSearch/NuGet.Services.AzureSearch.csproj +++ b/src/NuGet.Services.AzureSearch/NuGet.Services.AzureSearch.csproj @@ -47,6 +47,8 @@ + + diff --git a/src/NuGet.Services.AzureSearch/Owners2AzureSearch/DatabaseOwnerFetcher.cs b/src/NuGet.Services.AzureSearch/Owners2AzureSearch/DatabaseOwnerFetcher.cs new file mode 100644 index 000000000..8295ae539 --- /dev/null +++ b/src/NuGet.Services.AzureSearch/Owners2AzureSearch/DatabaseOwnerFetcher.cs @@ -0,0 +1,59 @@ +// Copyright (c) .NET Foundation. All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +using System; +using System.Collections.Generic; +using System.Threading.Tasks; +using Microsoft.Extensions.Logging; +using NuGet.Jobs; +using NuGet.Jobs.Configuration; + +namespace NuGet.Services.AzureSearch.Owners2AzureSearch +{ + public class DatabaseOwnerFetcher : IDatabaseOwnerFetcher + { + private readonly ISqlConnectionFactory _connectionFactory; + private readonly ILogger _logger; + + private const string Sql = @" +SELECT + pr.Id, + u.Username +FROM PackageRegistrations pr (NOLOCK) +INNER JOIN PackageRegistrationOwners pro (NOLOCK) ON pro.PackageRegistrationKey = pr.[Key] +INNER JOIN Users u (NOLOCK) ON pro.UserKey = u.[Key] +"; + + public DatabaseOwnerFetcher( + ISqlConnectionFactory connectionFactory, + ILogger logger) + { + _connectionFactory = connectionFactory ?? throw new ArgumentNullException(nameof(connectionFactory)); + _logger = logger; + } + + public async Task>> GetPackageIdToOwnersAsync() + { + using (var connection = await _connectionFactory.OpenAsync()) + using (var command = connection.CreateCommand()) + { + command.CommandText = Sql; + + using (var reader = await command.ExecuteReaderAsync()) + { + var builder = new PackageIdToOwnersBuilder(_logger); + while (await reader.ReadAsync()) + { + var id = reader.GetString(0); + var username = reader.GetString(1); + + builder.Add(id, username); + } + + return builder.GetResult(); + } + } + } + } +} + diff --git a/src/NuGet.Services.AzureSearch/Owners2AzureSearch/IDatabaseOwnerFetcher.cs b/src/NuGet.Services.AzureSearch/Owners2AzureSearch/IDatabaseOwnerFetcher.cs new file mode 100644 index 000000000..a6c8d7dae --- /dev/null +++ b/src/NuGet.Services.AzureSearch/Owners2AzureSearch/IDatabaseOwnerFetcher.cs @@ -0,0 +1,13 @@ +// Copyright (c) .NET Foundation. All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +using System.Collections.Generic; +using System.Threading.Tasks; + +namespace NuGet.Services.AzureSearch.Owners2AzureSearch +{ + public interface IDatabaseOwnerFetcher + { + Task>> GetPackageIdToOwnersAsync(); + } +} \ No newline at end of file