Skip to content

Commit ae115fa

Browse files
author
Mark Hall
authored
Merge pull request #21 from episerver/bugfix/20-fix-schema-updater
Use last migration instead of first
2 parents d5c607d + 80ed87f commit ae115fa

File tree

6 files changed

+43
-7
lines changed

6 files changed

+43
-7
lines changed

.github/workflows/ci.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,9 @@ on:
55
branches:
66
- 'main'
77
- 'develop'
8-
- 'netcore'
98
pull_request:
109
branches:
1110
- '*'
12-
- '!main'
1311
jobs:
1412
build_test_pack:
1513
name: Build, test & pack

build/version.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<!-- This file may be overwritten by automation. Only values allowed here are VersionPrefix and VersionSuffix. -->
22
<Project>
33
<PropertyGroup>
4-
<VersionPrefix>3.1.0</VersionPrefix>
4+
<VersionPrefix>3.1.1</VersionPrefix>
55
<VersionSuffix></VersionSuffix>
66
</PropertyGroup>
77
</Project>

src/EPiServer.Marketing.KPI/Dal/BaseRepository.cs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using Microsoft.EntityFrameworkCore.Migrations;
99
using Microsoft.EntityFrameworkCore.Infrastructure;
1010
using EPiServer.ServiceLocation;
11+
using Microsoft.Data.SqlClient;
1112

1213
namespace EPiServer.Marketing.KPI.Dal
1314
{
@@ -127,7 +128,22 @@ public void Delete<T>(T instance) where T : class
127128

128129
public string GetDatabaseVersion(string contextKey)
129130
{
130-
return HistoryRepository.GetAppliedMigrations().Select(r => r.MigrationId).FirstOrDefault();
131+
string result = null;
132+
using (var connection = new SqlConnection(DatabaseContext.Database.GetConnectionString()))
133+
{
134+
connection.Open();
135+
var command = new SqlCommand("Select top 1 MigrationId from __MigrationHistory where ContextKey = @contextKey order by MigrationId desc ", connection);
136+
command.Parameters.Add(new SqlParameter("@contextKey", contextKey));
137+
try
138+
{
139+
result = (string)command.ExecuteScalar();
140+
}
141+
catch (Exception)
142+
{
143+
return null;
144+
}
145+
}
146+
return result;
131147
}
132148

133149
/// <summary>

src/EPiServer.Marketing.KPI/Dal/DataAccess/KpiDataAccess.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,10 @@ public long GetDatabaseVersion(string schema, string contextKey)
156156
private long GetDatabaseVersionHelper(IRepository repo, string contextKey)
157157
{
158158
var lastMigration = repo.GetDatabaseVersion(contextKey);
159-
159+
if (lastMigration == null)
160+
{
161+
return 0;
162+
}
160163
// we are only interested in the numerical part of the key (i.e. 201609091719244_Initial)
161164
var version = lastMigration.Split('_')[0];
162165

src/EPiServer.Marketing.Testing.Dal/BaseRepository.cs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using Microsoft.EntityFrameworkCore.Migrations;
99
using Microsoft.EntityFrameworkCore.Infrastructure;
1010
using EPiServer.ServiceLocation;
11+
using Microsoft.Data.SqlClient;
1112

1213
namespace EPiServer.Marketing.Testing.Dal
1314
{
@@ -137,7 +138,22 @@ public void Delete<T>(T instance) where T : class
137138

138139
public string GetDatabaseVersion(string contextKey)
139140
{
140-
return HistoryRepository.GetAppliedMigrations().Select(r => r.MigrationId).FirstOrDefault();
141+
string result = null;
142+
using(var connection = new SqlConnection(DatabaseContext.Database.GetConnectionString()))
143+
{
144+
connection.Open();
145+
var command = new SqlCommand("Select top 1 MigrationId from __MigrationHistory where ContextKey = @contextKey order by MigrationId desc ", connection);
146+
command.Parameters.Add(new SqlParameter("@contextKey", contextKey));
147+
try
148+
{
149+
result = (string)command.ExecuteScalar();
150+
}
151+
catch(Exception)
152+
{
153+
return null;
154+
}
155+
}
156+
return result;
141157
}
142158

143159
/// <summary>

src/EPiServer.Marketing.Testing.Dal/DataAccess/TestingDataAccess.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,10 @@ public long GetDatabaseVersion(string schema, string contextKey)
108108
private long GetDatabaseVersionHelper(IRepository repo, string contextKey)
109109
{
110110
var lastMigration = repo.GetDatabaseVersion(contextKey);
111-
111+
if (lastMigration == null)
112+
{
113+
return 0;
114+
}
112115
// we are only interested in the numerical part of the key (i.e. 201609091719244_Initial)
113116
var version = lastMigration.Split('_')[0];
114117

0 commit comments

Comments
 (0)