|
| 1 | +// Copyright (c) .NET Foundation. All rights reserved. |
| 2 | +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. |
| 3 | + |
| 4 | +using System; |
| 5 | +using System.Data.SqlClient; |
| 6 | +using System.Linq; |
| 7 | +using System.Threading.Tasks; |
| 8 | +using Microsoft.Extensions.CommandLineUtils; |
| 9 | +using NuGet.Services.Entities; |
| 10 | +using NuGetGallery; |
| 11 | + |
| 12 | +namespace GalleryTools.Commands |
| 13 | +{ |
| 14 | + public static class UpdateIsLatestCommand |
| 15 | + { |
| 16 | + public static void Configure(CommandLineApplication config) |
| 17 | + { |
| 18 | + config.Description = "Update IsLatest(Stable)(SemVer2) properties of all packages in the target NuGetGallery database. !!Please make sure you have created a back-up of your database first!!"; |
| 19 | + config.HelpOption("-? | -h | --help"); |
| 20 | + |
| 21 | + var connectionstringOption = config.Option( |
| 22 | + "--connectionstring", |
| 23 | + "The SQL connectionstring of the target NuGetGallery database.", |
| 24 | + CommandOptionType.SingleValue); |
| 25 | + |
| 26 | + config.OnExecute(async () => await ExecuteAsync(connectionstringOption)); |
| 27 | + } |
| 28 | + |
| 29 | + private static async Task<int> ExecuteAsync( |
| 30 | + CommandOption connectionStringOption) |
| 31 | + { |
| 32 | + if (!connectionStringOption.HasValue()) |
| 33 | + { |
| 34 | + Console.Error.WriteLine($"The {connectionStringOption.Template} option is required."); |
| 35 | + return 1; |
| 36 | + } |
| 37 | + |
| 38 | + Console.Write("Testing database connectivity..."); |
| 39 | + |
| 40 | + using (var sqlConnection = new SqlConnection(connectionStringOption.Value())) |
| 41 | + { |
| 42 | + try |
| 43 | + { |
| 44 | + await sqlConnection.OpenAsync(); |
| 45 | + Console.WriteLine(" OK"); |
| 46 | + } |
| 47 | + catch (Exception exception) |
| 48 | + { |
| 49 | + Console.WriteLine(" Failed"); |
| 50 | + Console.Error.WriteLine(exception.Message); |
| 51 | + return -1; |
| 52 | + } |
| 53 | + |
| 54 | + using (var entitiesContext = new EntitiesContext(sqlConnection, readOnly: false)) |
| 55 | + { |
| 56 | + var packageRegistrationRepository = new EntityRepository<PackageRegistration>(entitiesContext); |
| 57 | + |
| 58 | + var packageService = new CorePackageService( |
| 59 | + new EntityRepository<Package>(entitiesContext), |
| 60 | + packageRegistrationRepository, |
| 61 | + new EntityRepository<Certificate>(entitiesContext)); |
| 62 | + |
| 63 | + Console.WriteLine("Retrieving package registrations..."); |
| 64 | + var packageRegistrations = packageRegistrationRepository.GetAll().ToList(); |
| 65 | + Console.WriteLine($"Processing {packageRegistrations.Count} records..."); |
| 66 | + |
| 67 | + double counter = 0; |
| 68 | + foreach (var packageRegistration in packageRegistrations.OrderBy(pr => pr.Id)) |
| 69 | + { |
| 70 | + var pct = 100 * counter++ / packageRegistrations.Count; |
| 71 | + |
| 72 | + Console.Write($" [{pct.ToString("N2")} %] Updating {packageRegistration.Id} ..."); |
| 73 | + await packageService.UpdateIsLatestAsync(packageRegistration, commitChanges: true); |
| 74 | + Console.WriteLine($" OK"); |
| 75 | + } |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + Console.WriteLine("DONE"); |
| 80 | + return 0; |
| 81 | + } |
| 82 | + } |
| 83 | +} |
0 commit comments