Skip to content

Commit

Permalink
refactor NuGet migrations code (#4875)
Browse files Browse the repository at this point in the history
  • Loading branch information
kartheekp-ms committed Nov 3, 2022
1 parent 3a7670c commit 01aa84e
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 40 deletions.
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 @@ -3,27 +3,17 @@

#if IS_DESKTOP
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 @@ -35,24 +25,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 { }
finally
{
mutex.ReleaseMutex();
}
}
}
}
Expand Down Expand Up @@ -91,20 +78,6 @@ 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;
}
}
}
#endif
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]);
}
}
}

0 comments on commit 01aa84e

Please sign in to comment.