Something went wrong. Try again.
A database migration tool for .NET
Something went wrong. Try again.
1.4 kB · 38 lines
C#
at develop
123456789101112131415161718192021222324252627282930313233343536373839using System.Collections.Generic;using System.IO;using System.Linq;using Microsoft.Extensions.Logging;
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);
return duplicate is not null ? throw new MigrationFormatException($"Multiple migration files share version {duplicate.Key}") : migrations; }}