-
Notifications
You must be signed in to change notification settings - Fork 695
/
Copy pathMigrationRunner.cs
91 lines (82 loc) · 3.79 KB
/
MigrationRunner.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// 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.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);
if (!File.Exists(expectedMigrationFilename))
{
// Multiple processes or threads might be trying to call this concurrently (especially via NuGetSdkResolver)
// so use a global mutex and then check if someone else already did the work.
using (var mutex = new Mutex(false, "NuGet-Migrations"))
{
bool signal = mutex.WaitOne(TimeSpan.FromMinutes(1), false);
if (signal && !File.Exists(expectedMigrationFilename))
{
// Only run migrations that have not already been run
int highestMigrationRun = GetHighestMigrationRun(migrationsDirectory);
for (int i = highestMigrationRun + 1; i < Migrations.Count; i++)
{
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 { }
}
mutex.ReleaseMutex();
}
}
}
}
private static string GetMigrationsDirectory()
{
string migrationsDirectory;
if (RuntimeEnvironmentHelper.IsWindows)
{
migrationsDirectory = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "NuGet", "Migrations");
}
else
{
var XdgDataHome = Environment.GetEnvironmentVariable("XDG_DATA_HOME");
migrationsDirectory = string.IsNullOrEmpty(XdgDataHome)
? Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), ".local", "share", "NuGet", "Migrations")
: Path.Combine(XdgDataHome, "NuGet", "Migrations");
}
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;
}
}
}