Update EF core migrations table to use the same schema as Elsa tables

This commit is contained in:
Sipke Schoorstra 2021-05-11 10:36:09 +02:00
parent cb94f4dde5
commit 26dfe813d0
20 changed files with 171 additions and 96 deletions

View file

@ -1,37 +1,37 @@
version: '3.7'
services:
mongodb:
image: mongo
ports:
- "27017:27017"
postgres:
image: postgres:11
environment:
- POSTGRES_USER=root
- POSTGRES_PASSWORD=Password12!
- POSTGRES_DB=yessql
ports:
- "5432:5432"
azureblobstorage:
image: mcr.microsoft.com/azure-blob-storage
redis:
image: redis
ports:
- "6379:6379"
rabbitmq:
image: "rabbitmq:3-management"
ports:
- "15672:15672"
- "5672:5672"
smtp4dev:
image: rnwood/smtp4dev:linux-amd64-3.1.0-ci0856
ports:
- "3000:80"
- "2525:25"
mongodb:
image: mongo
ports:
- "27017:27017"
postgres:
image: postgres:11
environment:
- POSTGRES_USER=root
- POSTGRES_PASSWORD=Password12!
- POSTGRES_DB=yessql
ports:
- "5432:5432"
azureblobstorage:
image: mcr.microsoft.com/azure-blob-storage
redis:
image: redis
ports:
- "6379:6379"
rabbitmq:
image: "rabbitmq:3-management"
ports:
- "15672:15672"
- "5672:5672"
smtp4dev:
image: rnwood/smtp4dev:linux-amd64-3.1.0-ci0856
ports:
- "3000:80"
- "2525:25"

View file

@ -2,13 +2,10 @@ using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Elsa.Events;
using Elsa.Models;
using Elsa.Persistence.Specifications;
using MediatR;
using Open.Linq.AsyncExtensions;
namespace Elsa.Persistence.Decorators
@ -28,22 +25,29 @@ namespace Elsa.Persistence.Decorators
public async Task DeleteAsync(WorkflowDefinition entity, CancellationToken cancellationToken = default)
{
await _mediator.Publish(new WorkflowDefinitionDeleting(entity), cancellationToken);
await _store.DeleteAsync(entity, cancellationToken);
await _mediator.Publish(new WorkflowDefinitionDeleted(entity), cancellationToken);
}
public async Task<int> DeleteManyAsync(ISpecification<WorkflowDefinition> specification, CancellationToken cancellationToken = default)
{
var instances = await FindManyAsync(specification, cancellationToken: cancellationToken).ToList();
var workflowDefinitions = await FindManyAsync(specification, cancellationToken: cancellationToken).ToList();
if (!workflowDefinitions.Any())
return 0;
foreach (var workflowDefinition in workflowDefinitions)
await _mediator.Publish(new WorkflowDefinitionDeleting(workflowDefinition), cancellationToken);
await _mediator.Publish(new ManyWorkflowDefinitionsDeleting(workflowDefinitions), cancellationToken);
var count = await _store.DeleteManyAsync(specification, cancellationToken);
if (instances.Any())
{
foreach (var instance in instances)
await _mediator.Publish(new WorkflowDefinitionDeleted(instance), cancellationToken);
foreach (var instance in workflowDefinitions)
await _mediator.Publish(new WorkflowDefinitionDeleted(instance), cancellationToken);
await _mediator.Publish(new ManyWorkflowDefinitionsDeleted(instances), cancellationToken);
}
await _mediator.Publish(new ManyWorkflowDefinitionsDeleted(workflowDefinitions), cancellationToken);
return count;
}
@ -55,27 +59,34 @@ namespace Elsa.Persistence.Decorators
public async Task SaveAsync(WorkflowDefinition entity, CancellationToken cancellationToken = default)
{
await _mediator.Publish(new WorkflowDefinitionSaving(entity), cancellationToken);
await _store.SaveAsync(entity, cancellationToken);
await _mediator.Publish(new WorkflowDefinitionSaved(entity), cancellationToken);
}
public async Task AddAsync(WorkflowDefinition entity, CancellationToken cancellationToken = default)
{
await _mediator.Publish(new WorkflowDefinitionSaving(entity), cancellationToken);
await _store.AddAsync(entity, cancellationToken);
await _mediator.Publish(new WorkflowDefinitionSaved(entity), cancellationToken);
}
public async Task AddManyAsync(IEnumerable<WorkflowDefinition> entities, CancellationToken cancellationToken = default)
{
var list = entities.ToList();
foreach (var entity in list)
await _mediator.Publish(new WorkflowDefinitionSaving(entity), cancellationToken);
await _store.AddManyAsync(list, cancellationToken);
foreach (var entity in list)
foreach (var entity in list)
await _mediator.Publish(new WorkflowDefinitionSaved(entity), cancellationToken);
}
public async Task UpdateAsync(WorkflowDefinition entity, CancellationToken cancellationToken = default)
public async Task UpdateAsync(WorkflowDefinition entity, CancellationToken cancellationToken = default)
{
await _mediator.Publish(new WorkflowDefinitionSaving(entity), cancellationToken);
await _store.UpdateAsync(entity, cancellationToken);
await _mediator.Publish(new WorkflowDefinitionSaved(entity), cancellationToken);
}

View file

@ -8,11 +8,14 @@ namespace Elsa.Persistence.EntityFramework.Core
{
public class ElsaContext : DbContext
{
public const string ElsaSchema = "Elsa";
public const string MigrationsHistoryTable = "__EFMigrationsHistory";
public ElsaContext(DbContextOptions options) : base(options)
{
}
public virtual string Schema => "Elsa";
public virtual string Schema => ElsaSchema;
public DbSet<WorkflowDefinition> WorkflowDefinitions { get; set; } = default!;
public DbSet<WorkflowInstance> WorkflowInstances { get; set; } = default!;
public DbSet<WorkflowExecutionLogRecord> WorkflowExecutionLogRecords { get; set; } = default!;
@ -20,9 +23,9 @@ namespace Elsa.Persistence.EntityFramework.Core
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
if (!string.IsNullOrWhiteSpace(Schema))
if (!string.IsNullOrWhiteSpace(Schema))
modelBuilder.HasDefaultSchema(Schema);
modelBuilder.ApplyConfigurationsFromAssembly(typeof(ElsaContext).Assembly);
if (Database.IsSqlite())

View file

@ -24,6 +24,7 @@ namespace Elsa.Persistence.EntityFramework.Core.StartupTasks
{
await using var dbContext = _dbContextFactory.CreateDbContext();
await dbContext.Database.MigrateAsync(cancellationToken);
await dbContext.DisposeAsync();
}
}
}

View file

@ -1,3 +1,4 @@
using Elsa.Persistence.EntityFramework.Core;
using Microsoft.EntityFrameworkCore;
namespace Elsa.Persistence.EntityFramework.MySql
@ -8,6 +9,8 @@ namespace Elsa.Persistence.EntityFramework.MySql
/// Configures the context to use MySql
/// </summary>
public static DbContextOptionsBuilder UseMySql(this DbContextOptionsBuilder builder, string connectionString) =>
builder.UseMySql(connectionString, ServerVersion.AutoDetect(connectionString), db => db.MigrationsAssembly(typeof(MySqlElsaContextFactory).Assembly.GetName().Name));
builder.UseMySql(connectionString, ServerVersion.AutoDetect(connectionString), db => db
.MigrationsAssembly(typeof(MySqlElsaContextFactory).Assembly.GetName().Name)
.MigrationsHistoryTable(ElsaContext.MigrationsHistoryTable, ElsaContext.ElsaSchema));
}
}

View file

@ -9,7 +9,7 @@ using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
namespace Elsa.Persistence.EntityFramework.MySql.Migrations
{
[DbContext(typeof(ElsaContext))]
[Migration("20210506160115_Initial")]
[Migration("20210511074305_Initial")]
partial class Initial
{
protected override void BuildTargetModel(ModelBuilder modelBuilder)

View file

@ -18,91 +18,128 @@ namespace Elsa.Persistence.EntityFramework.MySql.Migrations
schema: "Elsa",
columns: table => new
{
Id = table.Column<string>(type: "varchar(255)", nullable: false),
TenantId = table.Column<string>(type: "varchar(255)", nullable: true),
Hash = table.Column<string>(type: "varchar(255)", nullable: false),
Model = table.Column<string>(type: "longtext", nullable: false),
ModelType = table.Column<string>(type: "longtext", nullable: false),
ActivityType = table.Column<string>(type: "varchar(255)", nullable: false),
ActivityId = table.Column<string>(type: "varchar(255)", nullable: false),
Id = table.Column<string>(type: "varchar(255)", nullable: false)
.Annotation("MySql:CharSet", "utf8mb4"),
TenantId = table.Column<string>(type: "varchar(255)", nullable: true)
.Annotation("MySql:CharSet", "utf8mb4"),
Hash = table.Column<string>(type: "varchar(255)", nullable: false)
.Annotation("MySql:CharSet", "utf8mb4"),
Model = table.Column<string>(type: "longtext", nullable: false)
.Annotation("MySql:CharSet", "utf8mb4"),
ModelType = table.Column<string>(type: "longtext", nullable: false)
.Annotation("MySql:CharSet", "utf8mb4"),
ActivityType = table.Column<string>(type: "varchar(255)", nullable: false)
.Annotation("MySql:CharSet", "utf8mb4"),
ActivityId = table.Column<string>(type: "varchar(255)", nullable: false)
.Annotation("MySql:CharSet", "utf8mb4"),
WorkflowInstanceId = table.Column<string>(type: "varchar(255)", nullable: false)
.Annotation("MySql:CharSet", "utf8mb4")
},
constraints: table =>
{
table.PrimaryKey("PK_Bookmarks", x => x.Id);
});
})
.Annotation("MySql:CharSet", "utf8mb4");
migrationBuilder.CreateTable(
name: "WorkflowDefinitions",
schema: "Elsa",
columns: table => new
{
Id = table.Column<string>(type: "varchar(255)", nullable: false),
DefinitionId = table.Column<string>(type: "varchar(255)", nullable: false),
TenantId = table.Column<string>(type: "varchar(255)", nullable: true),
Name = table.Column<string>(type: "varchar(255)", nullable: true),
DisplayName = table.Column<string>(type: "longtext", nullable: true),
Description = table.Column<string>(type: "longtext", nullable: true),
Id = table.Column<string>(type: "varchar(255)", nullable: false)
.Annotation("MySql:CharSet", "utf8mb4"),
DefinitionId = table.Column<string>(type: "varchar(255)", nullable: false)
.Annotation("MySql:CharSet", "utf8mb4"),
TenantId = table.Column<string>(type: "varchar(255)", nullable: true)
.Annotation("MySql:CharSet", "utf8mb4"),
Name = table.Column<string>(type: "varchar(255)", nullable: true)
.Annotation("MySql:CharSet", "utf8mb4"),
DisplayName = table.Column<string>(type: "longtext", nullable: true)
.Annotation("MySql:CharSet", "utf8mb4"),
Description = table.Column<string>(type: "longtext", nullable: true)
.Annotation("MySql:CharSet", "utf8mb4"),
Version = table.Column<int>(type: "int", nullable: false),
IsSingleton = table.Column<bool>(type: "tinyint(1)", nullable: false),
PersistenceBehavior = table.Column<int>(type: "int", nullable: false),
DeleteCompletedInstances = table.Column<bool>(type: "tinyint(1)", nullable: false),
IsPublished = table.Column<bool>(type: "tinyint(1)", nullable: false),
IsLatest = table.Column<bool>(type: "tinyint(1)", nullable: false),
Tag = table.Column<string>(type: "varchar(255)", nullable: true),
Tag = table.Column<string>(type: "varchar(255)", nullable: true)
.Annotation("MySql:CharSet", "utf8mb4"),
Data = table.Column<string>(type: "longtext", nullable: true)
.Annotation("MySql:CharSet", "utf8mb4")
},
constraints: table =>
{
table.PrimaryKey("PK_WorkflowDefinitions", x => x.Id);
});
})
.Annotation("MySql:CharSet", "utf8mb4");
migrationBuilder.CreateTable(
name: "WorkflowExecutionLogRecords",
schema: "Elsa",
columns: table => new
{
Id = table.Column<string>(type: "varchar(255)", nullable: false),
TenantId = table.Column<string>(type: "varchar(255)", nullable: true),
WorkflowInstanceId = table.Column<string>(type: "varchar(255)", nullable: false),
ActivityId = table.Column<string>(type: "varchar(255)", nullable: false),
ActivityType = table.Column<string>(type: "varchar(255)", nullable: false),
Id = table.Column<string>(type: "varchar(255)", nullable: false)
.Annotation("MySql:CharSet", "utf8mb4"),
TenantId = table.Column<string>(type: "varchar(255)", nullable: true)
.Annotation("MySql:CharSet", "utf8mb4"),
WorkflowInstanceId = table.Column<string>(type: "varchar(255)", nullable: false)
.Annotation("MySql:CharSet", "utf8mb4"),
ActivityId = table.Column<string>(type: "varchar(255)", nullable: false)
.Annotation("MySql:CharSet", "utf8mb4"),
ActivityType = table.Column<string>(type: "varchar(255)", nullable: false)
.Annotation("MySql:CharSet", "utf8mb4"),
Timestamp = table.Column<DateTimeOffset>(type: "datetime(6)", nullable: false),
EventName = table.Column<string>(type: "longtext", nullable: true),
Message = table.Column<string>(type: "longtext", nullable: true),
Source = table.Column<string>(type: "longtext", nullable: true),
EventName = table.Column<string>(type: "longtext", nullable: true)
.Annotation("MySql:CharSet", "utf8mb4"),
Message = table.Column<string>(type: "longtext", nullable: true)
.Annotation("MySql:CharSet", "utf8mb4"),
Source = table.Column<string>(type: "longtext", nullable: true)
.Annotation("MySql:CharSet", "utf8mb4"),
Data = table.Column<string>(type: "longtext", nullable: true)
.Annotation("MySql:CharSet", "utf8mb4")
},
constraints: table =>
{
table.PrimaryKey("PK_WorkflowExecutionLogRecords", x => x.Id);
});
})
.Annotation("MySql:CharSet", "utf8mb4");
migrationBuilder.CreateTable(
name: "WorkflowInstances",
schema: "Elsa",
columns: table => new
{
Id = table.Column<string>(type: "varchar(255)", nullable: false),
DefinitionId = table.Column<string>(type: "varchar(255)", nullable: false),
TenantId = table.Column<string>(type: "varchar(255)", nullable: true),
Id = table.Column<string>(type: "varchar(255)", nullable: false)
.Annotation("MySql:CharSet", "utf8mb4"),
DefinitionId = table.Column<string>(type: "varchar(255)", nullable: false)
.Annotation("MySql:CharSet", "utf8mb4"),
TenantId = table.Column<string>(type: "varchar(255)", nullable: true)
.Annotation("MySql:CharSet", "utf8mb4"),
Version = table.Column<int>(type: "int", nullable: false),
WorkflowStatus = table.Column<int>(type: "int", nullable: false),
CorrelationId = table.Column<string>(type: "varchar(255)", nullable: true),
ContextType = table.Column<string>(type: "varchar(255)", nullable: true),
ContextId = table.Column<string>(type: "varchar(255)", nullable: true),
Name = table.Column<string>(type: "varchar(255)", nullable: true),
CorrelationId = table.Column<string>(type: "varchar(255)", nullable: true)
.Annotation("MySql:CharSet", "utf8mb4"),
ContextType = table.Column<string>(type: "varchar(255)", nullable: true)
.Annotation("MySql:CharSet", "utf8mb4"),
ContextId = table.Column<string>(type: "varchar(255)", nullable: true)
.Annotation("MySql:CharSet", "utf8mb4"),
Name = table.Column<string>(type: "varchar(255)", nullable: true)
.Annotation("MySql:CharSet", "utf8mb4"),
CreatedAt = table.Column<DateTimeOffset>(type: "datetime(6)", nullable: false),
LastExecutedAt = table.Column<DateTimeOffset>(type: "datetime(6)", nullable: true),
FinishedAt = table.Column<DateTimeOffset>(type: "datetime(6)", nullable: true),
CancelledAt = table.Column<DateTimeOffset>(type: "datetime(6)", nullable: true),
FaultedAt = table.Column<DateTimeOffset>(type: "datetime(6)", nullable: true),
Data = table.Column<string>(type: "longtext", nullable: true)
.Annotation("MySql:CharSet", "utf8mb4")
},
constraints: table =>
{
table.PrimaryKey("PK_WorkflowInstances", x => x.Id);
});
})
.Annotation("MySql:CharSet", "utf8mb4");
migrationBuilder.CreateIndex(
name: "IX_Bookmark_ActivityId",

View file

@ -18,7 +18,9 @@ namespace Elsa.Persistence.EntityFramework.MySql
builder.UseMySql(
connectionString,
serverVersion != null ? ServerVersion.Parse(serverVersion) : ServerVersion.AutoDetect(connectionString),
db => db.MigrationsAssembly(typeof(MySqlElsaContextFactory).Assembly.GetName().Name));
db => db
.MigrationsAssembly(typeof(MySqlElsaContextFactory).Assembly.GetName().Name)
.MigrationsHistoryTable(ElsaContext.MigrationsHistoryTable, ElsaContext.ElsaSchema));
return new ElsaContext(builder.Options);
}

View file

@ -1,3 +1,4 @@
using Elsa.Persistence.EntityFramework.Core;
using Microsoft.EntityFrameworkCore;
namespace Elsa.Persistence.EntityFramework.PostgreSql
@ -8,6 +9,8 @@ namespace Elsa.Persistence.EntityFramework.PostgreSql
/// Configures the context to use PostgreSql.
/// </summary>
public static DbContextOptionsBuilder UsePostgreSql(this DbContextOptionsBuilder builder, string connectionString) =>
builder.UseNpgsql(connectionString, db => db.MigrationsAssembly(typeof(PostgreSqlElsaContextFactory).Assembly.GetName().Name));
builder.UseNpgsql(connectionString, db => db
.MigrationsAssembly(typeof(PostgreSqlElsaContextFactory).Assembly.GetName().Name)
.MigrationsHistoryTable(ElsaContext.MigrationsHistoryTable, ElsaContext.ElsaSchema));
}
}

View file

@ -10,7 +10,7 @@ using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
namespace Elsa.Persistence.EntityFramework.PostgreSql.Migrations
{
[DbContext(typeof(ElsaContext))]
[Migration("20210506160122_Initial")]
[Migration("20210511074312_Initial")]
partial class Initial
{
protected override void BuildTargetModel(ModelBuilder modelBuilder)

View file

@ -11,9 +11,12 @@ namespace Elsa.Persistence.EntityFramework.PostgreSql
{
var builder = new DbContextOptionsBuilder<ElsaContext>();
var connectionString = args.Any() ? args[0] : "Server=127.0.0.1;Port=5432;Database=elsa;User Id=postgres;Password=password;";
builder.UseNpgsql(
connectionString,
db => db.MigrationsAssembly(typeof(PostgreSqlElsaContextFactory).Assembly.GetName().Name));
db => db.MigrationsAssembly(typeof(PostgreSqlElsaContextFactory).Assembly.GetName().Name)
.MigrationsHistoryTable(ElsaContext.MigrationsHistoryTable, ElsaContext.ElsaSchema));
return new ElsaContext(builder.Options);
}
}

View file

@ -1,3 +1,4 @@
using Elsa.Persistence.EntityFramework.Core;
using Microsoft.EntityFrameworkCore;
namespace Elsa.Persistence.EntityFramework.SqlServer
@ -8,6 +9,8 @@ namespace Elsa.Persistence.EntityFramework.SqlServer
/// Configures the context to use SqlServer.
/// </summary>
public static DbContextOptionsBuilder UseSqlServer(this DbContextOptionsBuilder builder, string connectionString) =>
builder.UseSqlServer(connectionString, db => db.MigrationsAssembly(typeof(SqlServerElsaContextFactory).Assembly.GetName().Name));
builder.UseSqlServer(connectionString, db => db
.MigrationsAssembly(typeof(SqlServerElsaContextFactory).Assembly.GetName().Name)
.MigrationsHistoryTable(ElsaContext.MigrationsHistoryTable, ElsaContext.ElsaSchema));
}
}

View file

@ -10,7 +10,7 @@ using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
namespace Elsa.Persistence.EntityFramework.SqlServer.Migrations
{
[DbContext(typeof(ElsaContext))]
[Migration("20210506160211_Initial")]
[Migration("20210511074331_Initial")]
partial class Initial
{
protected override void BuildTargetModel(ModelBuilder modelBuilder)

View file

@ -12,7 +12,9 @@ namespace Elsa.Persistence.EntityFramework.SqlServer
{
var builder = new DbContextOptionsBuilder<ElsaContext>();
var connectionString = args.Any() ? args[0] : throw new InvalidOperationException("Please specify a connection string. E.g. dotnet ef database update -- \"Server=Local;Database=elsa\"");
builder.UseSqlServer(connectionString, db => db.MigrationsAssembly(typeof(SqlServerElsaContextFactory).Assembly.GetName().Name));
builder.UseSqlServer(connectionString, db => db
.MigrationsAssembly(typeof(SqlServerElsaContextFactory).Assembly.GetName().Name)
.MigrationsHistoryTable(ElsaContext.MigrationsHistoryTable, ElsaContext.ElsaSchema));
return new ElsaContext(builder.Options);
}
}

View file

@ -1,9 +1,12 @@
using Microsoft.EntityFrameworkCore;
using Elsa.Persistence.EntityFramework.Core;
using Microsoft.EntityFrameworkCore;
namespace Elsa.Persistence.EntityFramework.Sqlite
{
public static class DbContextOptionsBuilderExtensions
{
public static DbContextOptionsBuilder UseSqlite(this DbContextOptionsBuilder builder) => builder.UseSqlite("Data Source=elsa.sqlite.db;Cache=Shared;", db => db.MigrationsAssembly(typeof(SqliteElsaContextFactory).Assembly.GetName().Name));
public static DbContextOptionsBuilder UseSqlite(this DbContextOptionsBuilder builder) => builder.UseSqlite("Data Source=elsa.sqlite.db;Cache=Shared;", db => db
.MigrationsAssembly(typeof(SqliteElsaContextFactory).Assembly.GetName().Name)
.MigrationsHistoryTable(ElsaContext.MigrationsHistoryTable, ElsaContext.ElsaSchema));
}
}

View file

@ -9,7 +9,7 @@ using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
namespace Elsa.Persistence.EntityFramework.Sqlite.Migrations
{
[DbContext(typeof(ElsaContext))]
[Migration("20210506160129_Initial")]
[Migration("20210511074322_Initial")]
partial class Initial
{
protected override void BuildTargetModel(ModelBuilder modelBuilder)

View file

@ -11,7 +11,11 @@ namespace Elsa.Persistence.EntityFramework.Sqlite
{
var builder = new DbContextOptionsBuilder<ElsaContext>();
var connectionString = args.Any() ? args[0] : "Data Source=elsa.db;Cache=Shared";
builder.UseSqlite(connectionString, db => db.MigrationsAssembly(typeof(SqliteElsaContextFactory).Assembly.GetName().Name));
builder.UseSqlite(connectionString, db => db
.MigrationsAssembly(typeof(SqliteElsaContextFactory).Assembly.GetName().Name)
.MigrationsHistoryTable(ElsaContext.MigrationsHistoryTable, ElsaContext.ElsaSchema));
return new ElsaContext(builder.Options);
}
}