Skip to content

Commit e9759a9

Browse files
Add tool to upgrade gallery deployments to support SemVer2 feature (#6711)
1 parent 7b0aee8 commit e9759a9

4 files changed

Lines changed: 90 additions & 6 deletions

File tree

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
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+
}

src/GalleryTools/Commands/VerifyApiKeyCommand.cs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
using System;
2-
using System.Collections.Generic;
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;
35
using System.Linq;
4-
using System.Text;
5-
using System.Threading.Tasks;
66
using Microsoft.Extensions.CommandLineUtils;
77
using NuGet.Services.Entities;
8-
using NuGetGallery;
98
using NuGetGallery.Infrastructure.Authentication;
109

1110
namespace GalleryTools.Commands
@@ -93,4 +92,4 @@ private static int Execute(
9392
return 0;
9493
}
9594
}
96-
}
95+
}

src/GalleryTools/GalleryTools.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
<Compile Include="Commands\BackfillRepositoryMetadataCommand.cs" />
4848
<Compile Include="Commands\HashCommand.cs" />
4949
<Compile Include="Commands\ReflowCommand.cs" />
50+
<Compile Include="Commands\UpdateIsLatestCommand.cs" />
5051
<Compile Include="Commands\VerifyApiKeyCommand.cs" />
5152
<Compile Include="Program.cs" />
5253
<Compile Include="Properties\AssemblyInfo.cs" />

src/GalleryTools/Program.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ public static int Main(params string[] args)
1818
commandLineApplication.Command("reflow", ReflowCommand.Configure);
1919
commandLineApplication.Command("fillrepodata", BackfillRepositoryMetadataCommand.Configure);
2020
commandLineApplication.Command("verifyapikey", VerifyApiKeyCommand.Configure);
21+
commandLineApplication.Command("updateIsLatest", UpdateIsLatestCommand.Configure);
2122

2223
try
2324
{

0 commit comments

Comments
 (0)