Skip to content

Commit 7850675

Browse files
author
daniel
committed
added migrations for EntityFramework based providers
1 parent de4f79e commit 7850675

21 files changed

+852
-34
lines changed

src/providers/WorkflowCore.Persistence.EntityFramework/Services/EntityFrameworkPersistenceProvider.cs

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,19 @@ namespace WorkflowCore.Persistence.EntityFramework.Services
1212
{
1313
public abstract class EntityFrameworkPersistenceProvider : DbContext, IPersistenceProvider
1414
{
15+
protected readonly bool _canCreateDB;
16+
protected readonly bool _canMigrateDB;
17+
18+
public EntityFrameworkPersistenceProvider(bool canCreateDB, bool canMigrateDB)
19+
{
20+
_canCreateDB = canCreateDB;
21+
_canMigrateDB = canMigrateDB;
22+
}
23+
1524
protected abstract void ConfigureWorkflowStorage(EntityTypeBuilder<PersistedWorkflow> builder);
1625
protected abstract void ConfigureSubscriptionStorage(EntityTypeBuilder<PersistedSubscription> builder);
1726
protected abstract void ConfigurePublicationStorage(EntityTypeBuilder<PersistedPublication> builder);
18-
public abstract void EnsureStoreExists();
19-
27+
2028
protected override void OnModelCreating(ModelBuilder modelBuilder)
2129
{
2230
base.OnModelCreating(modelBuilder);
@@ -145,5 +153,20 @@ public async Task RemoveUnpublishedEvent(Guid id)
145153
Set<PersistedPublication>().Remove(existing);
146154
SaveChanges();
147155
}
156+
157+
public virtual void EnsureStoreExists()
158+
{
159+
if (_canCreateDB && !_canMigrateDB)
160+
{
161+
Database.EnsureCreated();
162+
return;
163+
}
164+
165+
if (_canMigrateDB)
166+
{
167+
Database.Migrate();
168+
return;
169+
}
170+
}
148171
}
149172
}

src/providers/WorkflowCore.Persistence.EntityFramework/project.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
{
1+
{
22
"title": "Workflow Core EntityFramework Core Persistence Provider",
33
"authors": [ "Daniel Gerlag" ],
44
"version": "1.0.0-beta3",
55

66
"dependencies": {
77
"Microsoft.EntityFrameworkCore": "1.1.0",
8+
"Microsoft.EntityFrameworkCore.Relational": "1.1.0",
89
"NETStandard.Library": "1.6.1",
910
"Newtonsoft.Json": "9.0.1",
1011
"WorkflowCore": "1.0.0-*"
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using Microsoft.EntityFrameworkCore.Infrastructure;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
using System.Threading.Tasks;
6+
7+
namespace WorkflowCore.Persistence.PostgreSQL
8+
{
9+
public class MigrationContextFactory : IDbContextFactory<PostgresPersistenceProvider>
10+
{
11+
public PostgresPersistenceProvider Create(DbContextFactoryOptions options)
12+
{
13+
return new PostgresPersistenceProvider(@"Server=127.0.0.1;Port=5432;Database=workflow;User Id=postgres;Password=password;", true, true);
14+
}
15+
}
16+
}

src/providers/WorkflowCore.Persistence.PostgreSQL/Migrations/20161121200511_InitialDatabase.Designer.cs

Lines changed: 123 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using Microsoft.EntityFrameworkCore.Migrations;
4+
5+
namespace WorkflowCore.Persistence.PostgreSQL.Migrations
6+
{
7+
public partial class InitialDatabase : Migration
8+
{
9+
protected override void Up(MigrationBuilder migrationBuilder)
10+
{
11+
migrationBuilder.EnsureSchema(
12+
name: "wfc");
13+
14+
migrationBuilder.CreateTable(
15+
name: "UnpublishedEvent",
16+
schema: "wfc",
17+
columns: table => new
18+
{
19+
ClusterKey = table.Column<long>(nullable: false)
20+
.Annotation("Npgsql:ValueGeneratedOnAdd", true),
21+
EventData = table.Column<string>(nullable: true),
22+
EventKey = table.Column<string>(maxLength: 200, nullable: true),
23+
EventName = table.Column<string>(maxLength: 200, nullable: true),
24+
PublicationId = table.Column<Guid>(nullable: false),
25+
StepId = table.Column<int>(nullable: false),
26+
WorkflowId = table.Column<string>(maxLength: 200, nullable: true)
27+
},
28+
constraints: table =>
29+
{
30+
table.PrimaryKey("PK_UnpublishedEvent", x => x.ClusterKey);
31+
});
32+
33+
migrationBuilder.CreateTable(
34+
name: "Subscription",
35+
schema: "wfc",
36+
columns: table => new
37+
{
38+
ClusterKey = table.Column<long>(nullable: false)
39+
.Annotation("Npgsql:ValueGeneratedOnAdd", true),
40+
EventKey = table.Column<string>(maxLength: 200, nullable: true),
41+
EventName = table.Column<string>(maxLength: 200, nullable: true),
42+
StepId = table.Column<int>(nullable: false),
43+
SubscriptionId = table.Column<Guid>(maxLength: 200, nullable: false),
44+
WorkflowId = table.Column<string>(maxLength: 200, nullable: true)
45+
},
46+
constraints: table =>
47+
{
48+
table.PrimaryKey("PK_Subscription", x => x.ClusterKey);
49+
});
50+
51+
migrationBuilder.CreateTable(
52+
name: "Workflow",
53+
schema: "wfc",
54+
columns: table => new
55+
{
56+
ClusterKey = table.Column<long>(nullable: false)
57+
.Annotation("Npgsql:ValueGeneratedOnAdd", true),
58+
Data = table.Column<string>(nullable: true),
59+
Description = table.Column<string>(maxLength: 500, nullable: true),
60+
ExecutionPointers = table.Column<string>(nullable: true),
61+
InstanceId = table.Column<Guid>(maxLength: 200, nullable: false),
62+
NextExecution = table.Column<long>(nullable: true),
63+
Version = table.Column<int>(nullable: false),
64+
WorkflowDefinitionId = table.Column<string>(maxLength: 200, nullable: true)
65+
},
66+
constraints: table =>
67+
{
68+
table.PrimaryKey("PK_Workflow", x => x.ClusterKey);
69+
});
70+
71+
migrationBuilder.CreateIndex(
72+
name: "IX_UnpublishedEvent_PublicationId",
73+
schema: "wfc",
74+
table: "UnpublishedEvent",
75+
column: "PublicationId",
76+
unique: true);
77+
78+
migrationBuilder.CreateIndex(
79+
name: "IX_Subscription_EventKey",
80+
schema: "wfc",
81+
table: "Subscription",
82+
column: "EventKey");
83+
84+
migrationBuilder.CreateIndex(
85+
name: "IX_Subscription_EventName",
86+
schema: "wfc",
87+
table: "Subscription",
88+
column: "EventName");
89+
90+
migrationBuilder.CreateIndex(
91+
name: "IX_Subscription_SubscriptionId",
92+
schema: "wfc",
93+
table: "Subscription",
94+
column: "SubscriptionId",
95+
unique: true);
96+
97+
migrationBuilder.CreateIndex(
98+
name: "IX_Workflow_InstanceId",
99+
schema: "wfc",
100+
table: "Workflow",
101+
column: "InstanceId",
102+
unique: true);
103+
104+
migrationBuilder.CreateIndex(
105+
name: "IX_Workflow_NextExecution",
106+
schema: "wfc",
107+
table: "Workflow",
108+
column: "NextExecution");
109+
}
110+
111+
protected override void Down(MigrationBuilder migrationBuilder)
112+
{
113+
migrationBuilder.DropTable(
114+
name: "UnpublishedEvent",
115+
schema: "wfc");
116+
117+
migrationBuilder.DropTable(
118+
name: "Subscription",
119+
schema: "wfc");
120+
121+
migrationBuilder.DropTable(
122+
name: "Workflow",
123+
schema: "wfc");
124+
}
125+
}
126+
}

0 commit comments

Comments
 (0)