Something went wrong. Try again.
A database migration tool for .NET
Something went wrong. Try again.
1.3 kB · 40 lines
C#
at main
1234567891011121314151617181920212223242526272829303132333435363738394041using System.Collections.Generic;using System.IO;using System.Linq;
namespace Migrato.Core;
/// <summary>/// Loads and orders the migrations found in a directory/// </summary>public static class MigrationLoader{ /// <summary> /// Parses every "*.sql" file in <paramref name="directory"/> and returns them ordered by version /// </summary> /// <exception cref="DirectoryNotFoundException"><paramref name="directory"/> does not exist</exception> /// <exception cref="MigrationFormatException">A file is malformed, or two files share the same version</exception> public static IReadOnlyList<Migration> LoadFromDirectory(string directory) { if (!Directory.Exists(directory)) { throw new DirectoryNotFoundException($"Migrations directory '{directory}' does not exist"); }
var migrations = Directory.EnumerateFiles(directory, "*.sql") .Select(MigrationParser.Parse) .OrderBy(m => m.Version) .ToList();
var duplicate = migrations .GroupBy(m => m.Version) .FirstOrDefault(g => g.Count() > 1);
if (duplicate is not null) { throw new MigrationFormatException($"Multiple migration files share version {duplicate.Key}"); }
return migrations; }}