diff --git a/src/modules/Elsa.EntityFrameworkCore.Common/MigrationOptions.cs b/src/modules/Elsa.EntityFrameworkCore.Common/MigrationOptions.cs new file mode 100644 index 000000000..0ff4cb70f --- /dev/null +++ b/src/modules/Elsa.EntityFrameworkCore.Common/MigrationOptions.cs @@ -0,0 +1,14 @@ +namespace Elsa.EntityFrameworkCore; + +public class MigrationOptions +{ + /// + /// Gets or sets a collection that determines whether Entity Framework Core migrations + /// should be executed for specific DbContext types. + /// + /// + /// The key is the DbContext type, and the value is a boolean indicating whether migrations + /// should be applied for that specific context (true to apply, false to skip). + /// + public IDictionary RunMigrations { get; set; } = new Dictionary(); +} \ No newline at end of file diff --git a/src/modules/Elsa.EntityFrameworkCore.Common/PersistenceFeatureBase.cs b/src/modules/Elsa.EntityFrameworkCore.Common/PersistenceFeatureBase.cs index 1fd3352c0..c6620abe7 100644 --- a/src/modules/Elsa.EntityFrameworkCore.Common/PersistenceFeatureBase.cs +++ b/src/modules/Elsa.EntityFrameworkCore.Common/PersistenceFeatureBase.cs @@ -40,8 +40,7 @@ public abstract class PersistenceFeatureBase(IModule modul public override void ConfigureHostedServices() { - if (RunMigrations) - ConfigureMigrations(); + ConfigureMigrations(); } /// @@ -62,6 +61,11 @@ public abstract class PersistenceFeatureBase(IModule modul Services.AddDbContextFactory(setup, DbContextFactoryLifetime); Services.Decorate, TenantAwareDbContextFactory>(); + + Services.Configure(options => + { + options.RunMigrations[typeof(TDbContext)] = RunMigrations; + }); } protected virtual void ConfigureMigrations() diff --git a/src/modules/Elsa.EntityFrameworkCore.Common/RunMigrationsStartupTask.cs b/src/modules/Elsa.EntityFrameworkCore.Common/RunMigrationsStartupTask.cs index 45f9c4a5e..0a37bc581 100644 --- a/src/modules/Elsa.EntityFrameworkCore.Common/RunMigrationsStartupTask.cs +++ b/src/modules/Elsa.EntityFrameworkCore.Common/RunMigrationsStartupTask.cs @@ -2,6 +2,7 @@ using Elsa.Common.RecurringTasks; using JetBrains.Annotations; using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.Options; namespace Elsa.EntityFrameworkCore; @@ -11,11 +12,16 @@ namespace Elsa.EntityFrameworkCore; [UsedImplicitly] [SingleNodeTask] [Order(-100)] -public class RunMigrationsStartupTask(IDbContextFactory dbContextFactory) : IStartupTask where TDbContext : DbContext +public class RunMigrationsStartupTask(IDbContextFactory dbContextFactory, IOptions options) : IStartupTask where TDbContext : DbContext { - /// public async Task ExecuteAsync(CancellationToken cancellationToken) { + bool shouldRunMigrations = false; + options.Value.RunMigrations.TryGetValue(typeof(TDbContext), out shouldRunMigrations); + if (!shouldRunMigrations) + return; + var dbContext = await dbContextFactory.CreateDbContextAsync(cancellationToken); await dbContext.Database.MigrateAsync(cancellationToken); } diff --git a/src/modules/Elsa.EntityFrameworkCore/Modules/Management/WorkflowManagementPersistenceFeature.cs b/src/modules/Elsa.EntityFrameworkCore/Modules/Management/WorkflowManagementPersistenceFeature.cs index e89f9a299..b741625f4 100644 --- a/src/modules/Elsa.EntityFrameworkCore/Modules/Management/WorkflowManagementPersistenceFeature.cs +++ b/src/modules/Elsa.EntityFrameworkCore/Modules/Management/WorkflowManagementPersistenceFeature.cs @@ -27,7 +27,7 @@ public class WorkflowManagementPersistenceFeature(IModule module) : PersistenceF public override bool UseContextPooling { - get => base.UseContextPooling; + get => base.UseContextPooling; set { base.UseContextPooling = value; @@ -38,7 +38,7 @@ public class WorkflowManagementPersistenceFeature(IModule module) : PersistenceF public override bool RunMigrations { - get => base.RunMigrations; + get => base.RunMigrations; set { base.RunMigrations = value;