-
Notifications
You must be signed in to change notification settings - Fork 11
/
MigrationTracker.cs
126 lines (110 loc) · 4.18 KB
/
MigrationTracker.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
namespace Mite.Core
{
/// <summary>
/// Item which computes which scripts have been run and not run
/// </summary>
public class MigrationTracker : IMigrationTracker
{
private bool _permissive;
private readonly IList<Migration> migrations;
private readonly IDictionary<string, string> hashes = new Dictionary<string, string>();
/// <summary>
///
/// </summary>
/// <param name="migrations"></param>
/// <param name="hashes"></param>
/// <param name="permissive">If true will allow for a skipped migration to be executed</param>
public MigrationTracker(IEnumerable<Migration> migrations, IDictionary<string, string> hashes, bool permissive=false)
{
this._permissive = permissive;
this.migrations = migrations.OrderBy(x => x.Version).ToList();
this.hashes = hashes ?? new Dictionary<string, string>();
}
public IEnumerable<Migration> UnexcutedMigrations
{
get { return migrations.Where(x => !hashes.ContainsKey(x.Version)).OrderBy(x => x.Version); }
}
public bool IsHashMismatch()
{
return migrations.Where(mig => hashes.ContainsKey(mig.Version)).Any(mig => mig.Hash != hashes[mig.Version]);
}
public IDictionary<string , Migration> GetMigrationDictionary()
{
return migrations.ToDictionary(mig => mig.Version);
}
public bool IsMigrationGap()
{
var latestDatabaseVersion = hashes.Keys.OrderByDescending(x => x).FirstOrDefault();
if (latestDatabaseVersion == null)
return false;
var itemsToTest =migrations.Where(x => x.Version.CompareTo(latestDatabaseVersion) < 0).OrderBy(x => x.Version);
return itemsToTest.Any(mig => !hashes.Keys.Contains(mig.Version));
}
public bool IsValidState()
{
var misMatch = IsHashMismatch();
var isGap = IsMigrationGap();
var result = !misMatch && (_permissive || !isGap);
return result;
}
public bool CanGoUp { get { return IsValidState() && this.UnexcutedMigrations.Any(); } }
public bool CanGoDown { get { return IsValidState() && this.UnexcutedMigrations.Count() != this.migrations.Count(); } }
public bool Permissive
{
get => _permissive;
set => _permissive = value;
}
public Migration LastValidMigration
{
get
{
Migration lastValidMigration = null;
foreach (var mig in migrations)
{
if (hashes.ContainsKey(mig.Version) && hashes[mig.Version] == mig.Hash)
{
lastValidMigration = mig;
}else
{
return lastValidMigration;
}
}
return lastValidMigration;
}
}
public IEnumerable<Migration> Migrations
{
get { return this.migrations; }
}
public IEnumerable<Migration> InvalidMigrations()
{
return from mig in migrations
where hashes.ContainsKey(mig.Version)
let hash = hashes[mig.Version]
where !string.Equals(hash, mig.Hash) select mig;
}
public IEnumerable<Migration> MigrationsSince(DateTime dateTime)
{
throw new NotImplementedException();
}
public string Version
{
get {
if (hashes.Count == 0)
return "0";
var lastKey = hashes.Keys.LastOrDefault();
if (lastKey == null)
return string.Empty;
return lastKey;
}
}
public IEnumerable<Migration> ExecutedMigrations
{
get { return migrations.Where(x => hashes.ContainsKey(x.Version)).OrderByDescending(x => x.Version); }
}
}
}