Skip to content

Commit 935b5ad

Browse files
author
gauffininteractive
committed
Fixed merge issues.
2 parents 10efe5b + 902b322 commit 935b5ad

File tree

25 files changed

+679
-60
lines changed

25 files changed

+679
-60
lines changed

src/Server/OneTrueError.Api.Client.Tests/TryTheClient.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ namespace OneTrueError.Api.Client.Tests
1010
#if DEBUG
1111
public class TryTheClient
1212
{
13-
[Fact]
13+
//[Fact]
1414
public async Task Test()
1515
{
1616
var client = new OneTrueApiClient();

src/Server/OneTrueError.App/Core/Accounts/IAccountRepository.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public interface IAccountRepository
2020
Task CreateAsync(Account account);
2121

2222
/// <summary>
23-
/// find by using the actiovation key
23+
/// find by using the activation key
2424
/// </summary>
2525
/// <param name="activationKey"></param>
2626
/// <returns>account if found; otherwise <c>null</c>.</returns>

src/Server/OneTrueError.App/Core/ApiKeys/Events/ApplicationDeletedHandler.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Linq;
23
using System.Threading.Tasks;
34
using DotNetCqs;
45
using Griffin.Container;
@@ -16,7 +17,7 @@ public class ApplicationDeletedHandler : IApplicationEventSubscriber<Application
1617
private readonly IApiKeyRepository _repository;
1718

1819
/// <summary>
19-
/// Creates a new instance of <see cref="ApplicationDeletedHandler" />.
20+
/// Creates a new instance of <see cref="ApplicationDeletedHandler"/>.
2021
/// </summary>
2122
/// <param name="repository">repos</param>
2223
public ApplicationDeletedHandler(IApiKeyRepository repository)
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using System;
2+
using System.Threading.Tasks;
3+
using DotNetCqs;
4+
using Griffin.Container;
5+
using Griffin.Data;
6+
using OneTrueError.Api.Core.Applications.Events;
7+
8+
namespace OneTrueError.App.Core.Notifications.EventHandlers
9+
{
10+
/// <summary>
11+
/// Will delete all reports for the given application
12+
/// </summary>
13+
[Component(RegisterAsSelf = true)]
14+
public class ApplicationDeletedHandler : IApplicationEventSubscriber<ApplicationDeleted>
15+
{
16+
private IAdoNetUnitOfWork _uow;
17+
18+
/// <summary>
19+
/// Creates a new instance of <see cref="ApplicationDeletedHandler"/>.
20+
/// </summary>
21+
public ApplicationDeletedHandler(IAdoNetUnitOfWork uow)
22+
{
23+
if (uow == null) throw new ArgumentNullException("uow");
24+
_uow = uow;
25+
}
26+
27+
public Task HandleAsync(ApplicationDeleted e)
28+
{
29+
_uow.ExecuteNonQuery("DELETE FROM UserNotificationSettings WHERE ApplicationId = @id", new { id = e.ApplicationId });
30+
return Task.FromResult<object>(null);
31+
}
32+
}
33+
}

src/Server/OneTrueError.App/OneTrueError.App.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@
8787
<Compile Include="Core\Applications\CommandHandlers\DeleteApplicationHandler.cs" />
8888
<Compile Include="Core\Applications\EventHandlers\UpdateTeamOnInvitationAccepted.cs" />
8989
<Compile Include="Core\Applications\UserApplication.cs" />
90+
<Compile Include="Core\Notifications\EventHandlers\ApplicationDeletedHandler.cs" />
9091
<Compile Include="Core\Incidents\Jobs\DeleteEmptyIncidents.cs" />
9192
<Compile Include="Core\Notifications\EventHandlers\CheckForFeedbackNotificationsToSend.cs" />
9293
<Compile Include="Core\Reports\Config\ReportConfig.cs" />
@@ -274,6 +275,8 @@
274275
<EmbeddedResource Include="Modules\Messaging\Templating\Layout\Template.html" />
275276
</ItemGroup>
276277
<ItemGroup>
278+
<Folder Include="Core\Accounts\Events\" />
279+
<Folder Include="Core\ErrorReports\Events\" />
277280
<Folder Include="Modules\ReportSpikes\EventHandlers\" />
278281
</ItemGroup>
279282
<ItemGroup>

src/Server/OneTrueError.Data.Common/IDatabaseUtilities.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,16 @@ namespace OneTrueError.Infrastructure
44
{
55
public interface ISetupDatabaseTools
66
{
7+
/// <summary>
8+
/// Check if the current DB schema is out of date compared to the embedded schema resources.
9+
/// </summary>
10+
bool CanSchemaBeUpgraded();
11+
12+
/// <summary>
13+
/// Update DB schema to latest version.
14+
/// </summary>
15+
void UpgradeDatabaseSchema();
16+
717
/// <summary>
818
/// Used to check if the given connection string actually works
919
/// </summary>

src/Server/OneTrueError.SqlServer.Tests/ConnectionFactory.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.Configuration;
2+
using System.Data;
23
using System.Data.SqlClient;
34
using Griffin.Data;
45

@@ -13,5 +14,13 @@ public static IAdoNetUnitOfWork Create()
1314
connection.Open();
1415
return new AdoNetUnitOfWork(connection, true);
1516
}
17+
18+
public static IDbConnection CreateConnection()
19+
{
20+
var connection = new SqlConnection();
21+
connection.ConnectionString = ConfigurationManager.ConnectionStrings["Db"].ConnectionString;
22+
connection.Open();
23+
return connection;
24+
}
1625
}
1726
}

src/Server/OneTrueError.SqlServer.Tests/OneTrueError.SqlServer.Tests.csproj

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,8 @@
9393
<Compile Include="Core\ApiKeys\Queries\ListApiKeysHandlerTests.cs" />
9494
<Compile Include="Modules\Geolocation\ErrorOriginRepositoryTests.cs" />
9595
<Compile Include="Properties\AssemblyInfo.cs" />
96+
<Compile Include="SchemaManagerTests.cs" />
97+
<Compile Include="TestTools.cs" />
9698
</ItemGroup>
9799
<ItemGroup>
98100
<None Include="app.config" />
@@ -107,6 +109,10 @@
107109
<Project>{5EF42A74-9323-49FA-A1F6-974D6DE77202}</Project>
108110
<Name>OneTrueError.App</Name>
109111
</ProjectReference>
112+
<ProjectReference Include="..\OneTrueError.Data.Common\OneTrueError.Infrastructure.csproj">
113+
<Project>{A78A50DA-C9D7-47F2-8528-D7EE39D91924}</Project>
114+
<Name>OneTrueError.Infrastructure</Name>
115+
</ProjectReference>
110116
<ProjectReference Include="..\OneTrueError.SqlServer\OneTrueError.SqlServer.csproj">
111117
<Project>{B967BEEA-CDDD-4A83-A4F2-1C946099ED51}</Project>
112118
<Name>OneTrueError.SqlServer</Name>
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
using FluentAssertions;
7+
using Griffin.Data.Mapper;
8+
using Xunit;
9+
10+
namespace OneTrueError.SqlServer.Tests
11+
{
12+
public class SchemaManagerTests
13+
{
14+
public const int LatestVersion = 3;
15+
16+
public SchemaManagerTests()
17+
{
18+
var sut = new SchemaManager(ConnectionFactory.CreateConnection);
19+
EnsureSchemaTable();
20+
}
21+
22+
[Fact]
23+
public void should_report_upgradable_if_schema_version_is_less()
24+
{
25+
SetVersion(1);
26+
27+
var sut = new SchemaManager(ConnectionFactory.CreateConnection);
28+
var actual = sut.CanSchemaBeUpgraded();
29+
30+
actual.Should().BeTrue();
31+
}
32+
33+
[Fact]
34+
public void should_not_report_upgradable_if_schema_version_is_same()
35+
{
36+
SetVersion(3);
37+
38+
var sut = new SchemaManager(ConnectionFactory.CreateConnection);
39+
var actual = sut.CanSchemaBeUpgraded();
40+
41+
actual.Should().BeFalse();
42+
}
43+
44+
[Fact]
45+
public void should_report_upgradable_if_schema_table_is_missing()
46+
{
47+
DropSchemaTable();
48+
49+
var sut = new SchemaManager(ConnectionFactory.CreateConnection);
50+
var actual = sut.CanSchemaBeUpgraded();
51+
52+
actual.Should().BeTrue();
53+
}
54+
55+
[Fact]
56+
public void should_be_able_to_upgrade_schema()
57+
{
58+
59+
var sut = new SchemaManager(ConnectionFactory.CreateConnection);
60+
sut.UpgradeDatabaseSchema();
61+
62+
63+
}
64+
65+
66+
[Fact]
67+
public void should_be_able_to_upgrade_database()
68+
{
69+
using (var tools = new TestTools())
70+
{
71+
tools.CreateDatabase();
72+
73+
var sut = new SchemaManager(tools.CreateConnection);
74+
sut.UpgradeDatabaseSchema();
75+
}
76+
}
77+
78+
79+
private void SetVersion(int version)
80+
{
81+
using (var con = SqlServerTools.OpenConnection())
82+
{
83+
con.ExecuteNonQuery("UPDATE DatabaseSchema SET Version = @version", new {version = version});
84+
}
85+
}
86+
87+
private void EnsureSchemaTable()
88+
{
89+
using (var con = SqlServerTools.OpenConnection())
90+
{
91+
con.ExecuteNonQuery(@"
92+
IF OBJECT_ID(N'dbo.[DatabaseSchema]', N'U') IS NULL
93+
BEGIN
94+
CREATE TABLE [dbo].[DatabaseSchema] (
95+
[Version] int not null default 1
96+
);
97+
INSERT INTO DatabaseSchema VALUES(1);
98+
END
99+
");
100+
}
101+
}
102+
103+
private void DropSchemaTable()
104+
{
105+
using (var con = SqlServerTools.OpenConnection())
106+
{
107+
con.ExecuteNonQuery(@"DROP TABLE [dbo].[DatabaseSchema]");
108+
}
109+
}
110+
}
111+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Data;
4+
using System.Linq;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
using Griffin.Data.Mapper;
8+
using OneTrueError.Infrastructure;
9+
10+
namespace OneTrueError.SqlServer.Tests
11+
{
12+
public class TestTools : IDisposable
13+
{
14+
private string _dbName;
15+
16+
public void CreateDatabase()
17+
{
18+
using (var con = ConnectionFactory.CreateConnection())
19+
{
20+
_dbName = "T" + Guid.NewGuid().ToString("N");
21+
con.ExecuteNonQuery("CREATE Database " + _dbName);
22+
con.ChangeDatabase(_dbName);
23+
var schemaTool = new SchemaManager(() => con);
24+
schemaTool.CreateInitialStructure();
25+
}
26+
}
27+
28+
public IDbConnection CreateConnection()
29+
{
30+
var connection = ConnectionFactory.CreateConnection();
31+
connection.ChangeDatabase(_dbName);
32+
return connection;
33+
}
34+
35+
public void Dispose()
36+
{
37+
if (_dbName == null)
38+
return;
39+
40+
using (var con = ConnectionFactory.CreateConnection())
41+
{
42+
var sql =
43+
string.Format("alter database {0} set single_user with rollback immediate; DROP Database {0}",
44+
_dbName);
45+
con.ExecuteNonQuery(sql);
46+
}
47+
}
48+
}
49+
}

0 commit comments

Comments
 (0)