From 9541b9e6b4483e28d7c9e3421057555acceb34aa Mon Sep 17 00:00:00 2001 From: MaksimChegulov Date: Tue, 12 Oct 2021 12:04:24 +0300 Subject: [PATCH 1/2] optimization migration --- .../EF/Context/DbContextManager.cs | 14 ++++++++++---- common/ASC.Core.Common/EF/MigrationHistory.cs | 18 ++++++++++++++++++ 2 files changed, 28 insertions(+), 4 deletions(-) create mode 100644 common/ASC.Core.Common/EF/MigrationHistory.cs diff --git a/common/ASC.Core.Common/EF/Context/DbContextManager.cs b/common/ASC.Core.Common/EF/Context/DbContextManager.cs index d897cd7e61..6e720c513e 100644 --- a/common/ASC.Core.Common/EF/Context/DbContextManager.cs +++ b/common/ASC.Core.Common/EF/Context/DbContextManager.cs @@ -11,16 +11,19 @@ namespace ASC.Core.Common.EF public class BaseDbContextManager : OptionsManager, IDisposable where T : class, IDisposable, IAsyncDisposable, new() { private Dictionary Pairs { get; set; } + private MigrationHistory MigrationHistory { get; set; } private List AsyncList { get; set; } private IOptionsFactory Factory { get; } private IConfiguration Configuration { get; } - public BaseDbContextManager(IOptionsFactory factory, IConfiguration configuration) : base(factory) + public BaseDbContextManager(IOptionsFactory factory, IConfiguration configuration, + MigrationHistory migrationHistory) : base(factory) { Pairs = new Dictionary(); AsyncList = new List(); Factory = factory; Configuration = configuration; + MigrationHistory = migrationHistory; } public override T Get(string name) @@ -32,7 +35,8 @@ namespace ASC.Core.Common.EF if (t is BaseDbContext dbContext) { - if (Configuration["migration:enabled"] == "true") + if (Configuration["migration:enabled"] == "true" + && MigrationHistory.TryAddMigratedContext(t.ToString())) { dbContext.Migrate(); } @@ -68,14 +72,16 @@ namespace ASC.Core.Common.EF [Scope(typeof(ConfigureDbContext<>))] public class DbContextManager : BaseDbContextManager where T : BaseDbContext, new() { - public DbContextManager(IOptionsFactory factory, IConfiguration configuration) : base(factory, configuration) + public DbContextManager(IOptionsFactory factory, IConfiguration configuration, + MigrationHistory migrationHistory) : base(factory, configuration, migrationHistory) { } } public class MultiRegionalDbContextManager : BaseDbContextManager> where T : BaseDbContext, new() { - public MultiRegionalDbContextManager(IOptionsFactory> factory, IConfiguration configuration) : base(factory, configuration) + public MultiRegionalDbContextManager(IOptionsFactory> factory, IConfiguration configuration, + MigrationHistory migrationHistory) : base(factory, configuration, migrationHistory) { } } diff --git a/common/ASC.Core.Common/EF/MigrationHistory.cs b/common/ASC.Core.Common/EF/MigrationHistory.cs new file mode 100644 index 0000000000..5d252f5a6c --- /dev/null +++ b/common/ASC.Core.Common/EF/MigrationHistory.cs @@ -0,0 +1,18 @@ +using System.Collections.Concurrent; + +using ASC.Common; + +namespace ASC.Core.Common.EF +{ + [Singletone] + public class MigrationHistory + { + private ConcurrentDictionary _historyStore + = new ConcurrentDictionary(); + + public bool TryAddMigratedContext(string contextTypeName) + { + return _historyStore.TryAdd(contextTypeName, true); + } + } +} From 018c1939ab441872edfb7492020c8a3a753f33fa Mon Sep 17 00:00:00 2001 From: MaksimChegulov Date: Fri, 22 Oct 2021 13:08:09 +0300 Subject: [PATCH 2/2] replace type name with type --- common/ASC.Core.Common/EF/Context/DbContextManager.cs | 2 +- common/ASC.Core.Common/EF/MigrationHistory.cs | 11 ++++++----- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/common/ASC.Core.Common/EF/Context/DbContextManager.cs b/common/ASC.Core.Common/EF/Context/DbContextManager.cs index 6e720c513e..690367553b 100644 --- a/common/ASC.Core.Common/EF/Context/DbContextManager.cs +++ b/common/ASC.Core.Common/EF/Context/DbContextManager.cs @@ -36,7 +36,7 @@ namespace ASC.Core.Common.EF if (t is BaseDbContext dbContext) { if (Configuration["migration:enabled"] == "true" - && MigrationHistory.TryAddMigratedContext(t.ToString())) + && MigrationHistory.TryAddMigratedContext(t.GetType())) { dbContext.Migrate(); } diff --git a/common/ASC.Core.Common/EF/MigrationHistory.cs b/common/ASC.Core.Common/EF/MigrationHistory.cs index 5d252f5a6c..49b812d4c9 100644 --- a/common/ASC.Core.Common/EF/MigrationHistory.cs +++ b/common/ASC.Core.Common/EF/MigrationHistory.cs @@ -1,4 +1,5 @@ -using System.Collections.Concurrent; +using System; +using System.Collections.Concurrent; using ASC.Common; @@ -7,12 +8,12 @@ namespace ASC.Core.Common.EF [Singletone] public class MigrationHistory { - private ConcurrentDictionary _historyStore - = new ConcurrentDictionary(); + private ConcurrentDictionary _historyStore + = new ConcurrentDictionary(); - public bool TryAddMigratedContext(string contextTypeName) + public bool TryAddMigratedContext(Type contextType) { - return _historyStore.TryAdd(contextTypeName, true); + return _historyStore.TryAdd(contextType, true); } } }