1+ using System . Linq . Expressions ;
12using System . Reflection ;
3+ using System . Text . RegularExpressions ;
24using Microsoft . Extensions . Logging ;
35using OrchardCore . Data . Migration . Records ;
46using OrchardCore . Environment . Extensions ;
@@ -11,11 +13,25 @@ namespace OrchardCore.Data.Migration;
1113/// <summary>
1214/// Represents a class that manages the database migrations.
1315/// </summary>
14- public class DataMigrationManager : IDataMigrationManager
16+ public partial class DataMigrationManager : IDataMigrationManager
1517{
1618 private const string _updateFromPrefix = "UpdateFrom" ;
1719 private const string _asyncSuffix = "Async" ;
1820
21+ private static readonly Expression < Func < MethodInfo , bool > > _createMethodExpression = m
22+ => m . Name == "Create" && m . ReturnType == typeof ( int ) || m . Name == "CreateAsync" && m . ReturnType == typeof ( Task < int > ) ;
23+
24+ private static readonly Expression < Func < MethodInfo , bool > > _updateMethodExpression = m
25+ => UpdateFromRegex ( ) . IsMatch ( m . Name ) && m . ReturnType == typeof ( int ) ||
26+ UpdateFromAsyncRegex ( ) . IsMatch ( m . Name ) && m . ReturnType == typeof ( Task < int > ) ;
27+
28+ private static readonly Expression < Func < MethodInfo , bool > > _uninstallMethodExpression = m
29+ => m . Name == "Uninstall" && m . ReturnType == typeof ( void ) || m . Name == "UninstallAsync" && m . ReturnType == typeof ( Task ) ;
30+
31+ private static readonly Func < MethodInfo , bool > _createMethod = _createMethodExpression . Compile ( ) ;
32+ private static readonly Func < MethodInfo , bool > _updateMethod = _updateMethodExpression . Compile ( ) ;
33+ private static readonly Func < MethodInfo , bool > _uninstallMethod = _uninstallMethodExpression . Compile ( ) ;
34+
1935 private readonly IEnumerable < IDataMigration > _dataMigrations ;
2036 private readonly ISession _session ;
2137 private readonly IStore _store ;
@@ -312,24 +328,19 @@ private static Tuple<int, MethodInfo> GetUpdateFromMethod(MethodInfo methodInfo)
312328 /// </summary>
313329 private static MethodInfo GetMethod ( IDataMigration dataMigration , string name )
314330 {
315- var methodInfo = dataMigration . GetType ( )
316- . GetMethod ( name , BindingFlags . Public | BindingFlags . Instance ) ;
331+ var methodInfo = dataMigration . GetType ( ) . GetMethod ( name , BindingFlags . Public | BindingFlags . Instance ) ;
317332
318- if ( methodInfo is null )
319- {
320- return null ;
321- }
322-
323- if ( methodInfo . Name == "Create" && methodInfo . ReturnType == typeof ( int ) ||
324- methodInfo . Name == "CreateAsync" && methodInfo . ReturnType == typeof ( Task < int > ) ||
325- methodInfo . Name == "Update" && methodInfo . ReturnType == typeof ( int ) ||
326- methodInfo . Name == "UpdateAsync" && methodInfo . ReturnType == typeof ( Task < int > ) ||
327- methodInfo . Name == "Update" && methodInfo . ReturnType == typeof ( void ) ||
328- methodInfo . Name == "UpdateAsync" && methodInfo . ReturnType == typeof ( Task ) )
333+ if ( methodInfo is not null && ( _createMethod ( methodInfo ) || _updateMethod ( methodInfo ) || _uninstallMethod ( methodInfo ) ) )
329334 {
330335 return methodInfo ;
331336 }
332337
333338 return null ;
334339 }
340+
341+ [ GeneratedRegex ( @"^UpdateFrom(\d+)$" ) ]
342+ private static partial Regex UpdateFromRegex ( ) ;
343+
344+ [ GeneratedRegex ( @"^UpdateFrom(\d+)Async$" ) ]
345+ private static partial Regex UpdateFromAsyncRegex ( ) ;
335346}
0 commit comments