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

refactor NuGet migrations code #4875

Merged
merged 2 commits into from
Oct 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 10 additions & 37 deletions src/NuGet.Core/NuGet.Common/Migrations/MigrationRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,17 @@
// 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.Diagnostics;
using System.Globalization;
using System.IO;
using System.Threading;

namespace NuGet.Common.Migrations
{
public static class MigrationRunner
{
private static readonly IReadOnlyList<Action> Migrations = new List<Action>()
{
Migration1.Run
};
private const string MaxMigrationFilename = "1";

public static void Run()
{
// since migrations run once per machine, optimize for the scenario where the migration has already run
Debug.Assert(MaxMigrationFilename == Migrations.Count.ToString(CultureInfo.InvariantCulture));

string migrationsDirectory = GetMigrationsDirectory();
var expectedMigrationFilename = Path.Combine(migrationsDirectory, MaxMigrationFilename);

Expand All @@ -34,24 +24,21 @@ public static void Run()
{
if (WaitForMutex(mutex))
{
if (!File.Exists(expectedMigrationFilename))
try
{
// Only run migrations that have not already been run
int highestMigrationRun = GetHighestMigrationRun(migrationsDirectory);
for (int i = highestMigrationRun + 1; i < Migrations.Count; i++)
if (!File.Exists(expectedMigrationFilename))
{
try
{
Migrations[i]();
// Create file for every migration run, so that if an older version of NuGet is run, it doesn't try to run
// migrations again.
string migrationFile = Path.Combine(migrationsDirectory, (i + 1).ToString(CultureInfo.InvariantCulture));
File.WriteAllText(migrationFile, string.Empty);
}
catch { }
Migration1.Run();
// Create file for the migration run, so that if an older version of NuGet is run, it doesn't try to run migrations again.
File.WriteAllText(expectedMigrationFilename, string.Empty);
}
}
mutex.ReleaseMutex();
catch { }
kartheekp-ms marked this conversation as resolved.
Show resolved Hide resolved
finally
{
mutex.ReleaseMutex();
}
}
}
}
Expand Down Expand Up @@ -90,19 +77,5 @@ internal static string GetMigrationsDirectory()
Directory.CreateDirectory(migrationsDirectory);
return migrationsDirectory;
}

private static int GetHighestMigrationRun(string directory)
{
for (int i = Migrations.Count - 1; i >= 0; --i)
{
var filename = Path.Combine(directory, (i + 1).ToString(CultureInfo.InvariantCulture));
if (File.Exists(filename))
{
return i;
}
}

return -1;
}
}
}
25 changes: 22 additions & 3 deletions test/NuGet.Core.Tests/NuGet.Common.Test/MigrationRunnerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using System.Collections.Generic;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using NuGet.Common.Migrations;
using Xunit;

Expand All @@ -15,7 +14,25 @@ namespace NuGet.Common.Test
public class MigrationRunnerTests
{
[Fact]
public void WhenExecutedInParallelOnlyOneFileIsCreatedForEveryMigration_Success()
public void Run_WhenExecutedOnSingleThreadThenOneMigrationFileIsCreated_Success()
{
// Arrange
string directory = MigrationRunner.GetMigrationsDirectory();
if (Directory.Exists(directory))
Directory.Delete(path: directory, recursive: true);

// Act
MigrationRunner.Run();

// Assert
Assert.True(Directory.Exists(directory));
var files = Directory.GetFiles(directory);
Assert.Equal(1, files.Length);
Assert.Equal(Path.Combine(directory, "1"), files[0]);
}

[Fact]
public void Run_WhenExecutedInParallelThenOnlyOneMigrationFileIsCreated_Success()
{
var threads = new List<Thread>();
int numThreads = 5;
Expand All @@ -41,7 +58,9 @@ public void WhenExecutedInParallelOnlyOneFileIsCreatedForEveryMigration_Success(

// Assert
Assert.True(Directory.Exists(directory));
Assert.Equal(1, Directory.GetFiles(directory).Length);
var files = Directory.GetFiles(directory);
Assert.Equal(1, files.Length);
Assert.Equal(Path.Combine(directory, "1"), files[0]);
}
}
}