Skip to content

Commit 1cb8516

Browse files
authored
Merge pull request #1901 from fluentmigrator/topic/MsBuildLoggingStatements
Add trace statements to MigrationSource task
2 parents a85d0ac + f8a330f commit 1cb8516

File tree

6 files changed

+52
-232
lines changed

6 files changed

+52
-232
lines changed

src/FluentMigrator.MSBuild/FluentMigrator.MSBuild.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
<!-- Dependencies -->
4444
<!-- mark every dependency of this Task project, both PackageReference and ProjectReference with the PrivateAssets="all" attribute. -->
4545
<ItemGroup>
46-
<PackageReference Include="Microsoft.Build.Utilities.Core" Version="17.8.3" PrivateAssets="all" ExcludeAssets="Runtime" />
46+
<PackageReference Include="Microsoft.Build.Utilities.Core" Version="17.11.4" PrivateAssets="all" ExcludeAssets="Runtime" />
4747
</ItemGroup>
4848
<ItemGroup>
4949
<PackageReference Update="JetBrains.Annotations" Version="2024.2.0" PrivateAssets="all" />

src/FluentMigrator.MSBuild/Migrate.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333
using Microsoft.Extensions.DependencyInjection;
3434
using Microsoft.Extensions.Logging;
3535

36+
using ILogger = Microsoft.Extensions.Logging.ILogger;
37+
3638
namespace FluentMigrator.MSBuild
3739
{
3840
public class Migrate :
@@ -116,6 +118,8 @@ public int Timeout
116118

117119
public bool IncludeUntaggedMigrations { get; set; } = true;
118120

121+
public bool UseMsBuildLogging { get; set; } = false;
122+
119123
public string DefaultSchemaName { get; set; }
120124

121125
private bool ExecutingAgainstMsSql => _databaseType.StartsWith("SqlServer", StringComparison.InvariantCultureIgnoreCase);
@@ -211,6 +215,11 @@ private void ExecuteMigrations()
211215
.AddSingleton<ILoggerProvider, LogFileFluentMigratorLoggerProvider>();
212216
}
213217

218+
if (UseMsBuildLogging)
219+
{
220+
services.AddScoped<ILogger>(provider => new MicrosoftBuildLogger(this));
221+
}
222+
214223
using (var serviceProvider = services.BuildServiceProvider(validateScopes: false))
215224
{
216225
var executor = serviceProvider.GetRequiredService<TaskExecutor>();

src/FluentMigrator.Runner.Core/DefaultMigrationInformationLoader.cs

Lines changed: 0 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
using System;
2020
using System.Collections.Generic;
2121
using System.Linq;
22-
using System.Reflection;
2322

2423
using FluentMigrator.Exceptions;
2524
using FluentMigrator.Infrastructure;
@@ -47,40 +46,6 @@ public class DefaultMigrationInformationLoader : IMigrationInformationLoader
4746
[CanBeNull]
4847
private SortedList<long, IMigrationInfo> _migrationInfos;
4948

50-
[Obsolete]
51-
public DefaultMigrationInformationLoader(IMigrationRunnerConventions conventions, Assembly assembly, string @namespace,
52-
IEnumerable<string> tagsToMatch)
53-
: this(conventions, new SingleAssembly(assembly), @namespace, false, tagsToMatch)
54-
{
55-
}
56-
57-
[Obsolete]
58-
public DefaultMigrationInformationLoader(IMigrationRunnerConventions conventions, IAssemblyCollection assemblies, string @namespace,
59-
IEnumerable<string> tagsToMatch)
60-
: this(conventions, assemblies, @namespace, false, tagsToMatch)
61-
{
62-
}
63-
64-
[Obsolete]
65-
public DefaultMigrationInformationLoader(IMigrationRunnerConventions conventions, Assembly assembly, string @namespace,
66-
bool loadNestedNamespaces, IEnumerable<string> tagsToMatch)
67-
: this(conventions, new SingleAssembly(assembly), @namespace, loadNestedNamespaces, tagsToMatch)
68-
{
69-
}
70-
71-
[Obsolete]
72-
public DefaultMigrationInformationLoader(IMigrationRunnerConventions conventions, IAssemblyCollection assemblies, string @namespace,
73-
bool loadNestedNamespaces, IEnumerable<string> tagsToMatch)
74-
{
75-
Conventions = conventions;
76-
Assemblies = assemblies;
77-
Namespace = @namespace;
78-
LoadNestedNamespaces = loadNestedNamespaces;
79-
_tagsToMatch = tagsToMatch as string[] ?? tagsToMatch?.ToArray() ?? Array.Empty<string>();
80-
_source = new MigrationSource(new AssemblySource(() => assemblies), conventions);
81-
_includeUntaggedMigrations = true;
82-
}
83-
8449
public DefaultMigrationInformationLoader(
8550
#pragma warning disable 618
8651
[NotNull] IMigrationSource source,
@@ -100,19 +65,11 @@ public DefaultMigrationInformationLoader(
10065
[NotNull]
10166
public IMigrationRunnerConventions Conventions { get; }
10267

103-
[Obsolete]
104-
[CanBeNull]
105-
public IAssemblyCollection Assemblies { get; }
106-
10768
[CanBeNull]
10869
public string Namespace { get; }
10970

11071
public bool LoadNestedNamespaces { get; }
11172

112-
[NotNull, ItemNotNull]
113-
[Obsolete]
114-
public IEnumerable<string> TagsToMatch => _tagsToMatch;
115-
11673
public SortedList<long, IMigrationInfo> LoadMigrations()
11774
{
11875
if (_migrationInfos != null)

src/FluentMigrator.Runner.Core/FluentMigratorServiceCollectionExtensions.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333

3434
using Microsoft.Extensions.DependencyInjection.Extensions;
3535
using Microsoft.Extensions.Logging;
36+
using Microsoft.Extensions.Logging.Abstractions;
3637
using Microsoft.Extensions.Options;
3738

3839
// ReSharper disable once CheckNamespace
@@ -59,8 +60,9 @@ public static IServiceCollection AddFluentMigratorCore(
5960
// Add support for options
6061
.AddOptions()
6162

62-
// Add loggins support
63+
// Add logging support
6364
.AddLogging()
65+
.AddScoped<ILogger>(provider => NullLogger.Instance)
6466

6567
// The default assembly loader factory
6668
.AddSingleton<AssemblyLoaderFactory>()

src/FluentMigrator.Runner.Core/Initialization/MigrationSource.cs

Lines changed: 39 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
using JetBrains.Annotations;
2323

2424
using Microsoft.Extensions.DependencyInjection;
25+
using Microsoft.Extensions.Logging;
2526

2627
namespace FluentMigrator.Runner.Initialization
2728
{
@@ -45,23 +46,29 @@ public class MigrationSource : IFilteringMigrationSource
4546
[NotNull, ItemNotNull]
4647
private readonly IEnumerable<IMigrationSourceItem> _sourceItems;
4748

49+
[NotNull]
50+
private readonly ILogger _logger;
51+
4852
/// <summary>
4953
/// Initializes a new instance of the <see cref="ProfileSource"/> class.
5054
/// </summary>
5155
/// <param name="source">The assembly source</param>
5256
/// <param name="conventions">The migration runner conventios</param>
5357
/// <param name="serviceProvider">The service provider</param>
5458
/// <param name="sourceItems">The additional migration source items</param>
59+
/// <param name="logger">The logger for troubleshooting "No migrations found" error.</param>
5560
public MigrationSource(
5661
[NotNull] IAssemblySource source,
5762
[NotNull] IMigrationRunnerConventions conventions,
5863
[NotNull] IServiceProvider serviceProvider,
59-
[NotNull, ItemNotNull] IEnumerable<IMigrationSourceItem> sourceItems)
64+
[NotNull, ItemNotNull] IEnumerable<IMigrationSourceItem> sourceItems,
65+
[NotNull] ILogger logger)
6066
{
6167
_source = source;
6268
_conventions = conventions;
6369
_serviceProvider = serviceProvider;
6470
_sourceItems = sourceItems;
71+
_logger = logger;
6572
}
6673

6774
/// <summary>
@@ -88,18 +95,41 @@ public IEnumerable<IMigration> GetMigrations()
8895
/// <inheritdoc />
8996
public IEnumerable<IMigration> GetMigrations(Func<Type, bool> predicate)
9097
{
91-
var instances =
92-
from type in GetMigrationTypeCandidates()
93-
where !type.IsAbstract && typeof(IMigration).IsAssignableFrom(type)
94-
where predicate == null || predicate(type)
95-
select _instanceCache.GetOrAdd(type, CreateInstance);
96-
return instances;
98+
foreach (var type in GetMigrationTypeCandidates())
99+
{
100+
if (type.IsAbstract)
101+
{
102+
_logger.Log(LogLevel.Trace, $"Type [{type.AssemblyQualifiedName}] is abstract. Skipping.");
103+
continue;
104+
}
105+
106+
if (!(typeof(IMigration).IsAssignableFrom(type) || typeof(MigrationBase).IsAssignableFrom(type)))
107+
{
108+
_logger.Log(LogLevel.Trace, $"Type [{type.AssemblyQualifiedName}] is not assignable to IMigration. Skipping.");
109+
continue;
110+
}
111+
112+
if (!(predicate == null || predicate(type)))
113+
{
114+
_logger.Log(LogLevel.Trace, $"Type [{type.AssemblyQualifiedName}] doesn't satisfy predicate. Skipping.");
115+
continue;
116+
}
117+
118+
_logger.Log(LogLevel.Trace, $"Type {type.AssemblyQualifiedName} is a migration. Adding.");
119+
120+
yield return _instanceCache.GetOrAdd(type, CreateInstance);
121+
}
122+
}
123+
124+
private IEnumerable<Type> GetExportedTypes()
125+
{
126+
return _source
127+
.Assemblies.SelectMany(a => a.GetExportedTypes());
97128
}
98129

99130
private IEnumerable<Type> GetMigrationTypeCandidates()
100131
{
101-
return _source
102-
.Assemblies.SelectMany(a => a.GetExportedTypes())
132+
return GetExportedTypes()
103133
.Union(_sourceItems.SelectMany(i => i.MigrationTypeCandidates));
104134
}
105135

0 commit comments

Comments
 (0)