Skip to content

Commit

Permalink
Fixed processing script migrations prefix
Browse files Browse the repository at this point in the history
  • Loading branch information
Markeli committed Apr 12, 2023
1 parent 3e67464 commit 548babf
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -155,14 +155,17 @@ public ScriptMigrationsProvider()
var scripts = new Dictionary<MigrationVersion, MigrationScriptInfo>();

var regex = new Regex(MigrationConstants.MigrationFileNamePattern, RegexOptions.IgnoreCase);
var regexWithPrefix = String.IsNullOrWhiteSpace(scriptParsingOptions.Prefix)
? null
: new Regex($"{scriptParsingOptions.Prefix}{MigrationConstants.MigrationFileNamePattern}", RegexOptions.IgnoreCase);

for (var i = 0; i < fileNames.Count; i++)
{
var fileName = fileNames[i];

if (fileName.ToLower().EndsWith("sql"))
{
if (!String.IsNullOrWhiteSpace(scriptParsingOptions.Prefix) && !fileName.StartsWith(scriptParsingOptions.Prefix))
if (regexWithPrefix != null && !regexWithPrefix.IsMatch(fileName))
{
migrationLogger?.LogTrace($"\"{fileName}\" skipped because of incorrect prefix. Prefix \"{scriptParsingOptions.Prefix}\" is expected");
continue;
Expand Down Expand Up @@ -198,13 +201,12 @@ public ScriptMigrationsProvider()
continue;
}

if (!scripts.ContainsKey(version))
if (!scripts.TryGetValue(version, out var scriptInfo))
{
scripts[version] = new MigrationScriptInfo();
scriptInfo = new MigrationScriptInfo();
scripts[version] = scriptInfo;
}

var scriptInfo = scripts[version];

var script = sqlScriptReadFunc.Invoke(fileName);

// extract options for current migration
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ public void GetMigrations_FromAssembly_WithPrefix_Ok()
var logger = Mock.Of<ILogger>();

var migrationsProvider = new ScriptMigrationsProvider();
migrationsProvider.FromAssembly(Assembly.GetExecutingAssembly(), "PreMigration.");
migrationsProvider.FromAssembly(typeof(ScriptConstants).Assembly, "ScriptAsResources.PreMigration.");

var migrations = migrationsProvider
.GetMigrations(dbProvider, new Dictionary<string, string>(), logger)
Expand Down

0 comments on commit 548babf

Please sign in to comment.