-
Notifications
You must be signed in to change notification settings - Fork 11
/
MigrationHelper.cs
33 lines (30 loc) · 1.24 KB
/
MigrationHelper.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
using System.Collections.Generic;
using System.IO;
using System.Text.RegularExpressions;
namespace Mite.Core
{
public static class MigrationHelper
{
public static IEnumerable<Migration> ReadFromDirectory(string directoryName)
{
var files = Directory.GetFiles(directoryName, "*.sql", SearchOption.TopDirectoryOnly);
var sqlMatch = new Regex("/\\* ?up ?\\*/(.*?)/\\* ?down ?\\*/(.*?)$", RegexOptions.IgnoreCase | RegexOptions.Singleline);
foreach (var file in files)
{
var info = new FileInfo(file);
if (info.Name.StartsWith("_") || info.Name.ToLower().Equals("mite.config"))
continue;
var sql = File.ReadAllText(file);
var match= sqlMatch.Match(sql);
var version = !string.IsNullOrEmpty(info.Extension) ? info.Name.Replace(info.Extension, "") : info.Name;
if ( match.Success)
{
yield return new Migration(version, match.Groups[1].Value, match.Groups[2].Value);
}else
{
yield return new Migration(version, sql, "");
}
}
}
}
}