Skip to content

Commit

Permalink
Merge pull request #1067 from eventflow/v1-sqlite-conversion
Browse files Browse the repository at this point in the history
New: SQLite support for v1
  • Loading branch information
rasmus authored Dec 24, 2024
2 parents 01261e3 + 4adec92 commit a13f9ec
Show file tree
Hide file tree
Showing 16 changed files with 152 additions and 75 deletions.
17 changes: 17 additions & 0 deletions EventFlow.sln
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EventFlow.Hangfire", "Sourc
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EventFlow.Hangfire.Tests", "Source\EventFlow.Hangfire.Tests\EventFlow.Hangfire.Tests.csproj", "{B4247230-5289-4D17-BB0F-CB2FDBABA988}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "SQLite", "SQLite", "{74EFCDE2-CB0F-49B1-9CEC-BE748EB1FBF7}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EventFlow.SQLite", "Source\EventFlow.SQLite\EventFlow.SQLite.csproj", "{D2B5B5CA-57C2-4354-ADB7-47A6D81AD521}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EventFlow.SQLite.Tests", "Source\EventFlow.SQLite.Tests\EventFlow.SQLite.Tests.csproj", "{8FAC191C-340D-47C6-B8AE-3D57783749C4}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -131,6 +137,14 @@ Global
{B4247230-5289-4D17-BB0F-CB2FDBABA988}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B4247230-5289-4D17-BB0F-CB2FDBABA988}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B4247230-5289-4D17-BB0F-CB2FDBABA988}.Release|Any CPU.Build.0 = Release|Any CPU
{D2B5B5CA-57C2-4354-ADB7-47A6D81AD521}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D2B5B5CA-57C2-4354-ADB7-47A6D81AD521}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D2B5B5CA-57C2-4354-ADB7-47A6D81AD521}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D2B5B5CA-57C2-4354-ADB7-47A6D81AD521}.Release|Any CPU.Build.0 = Release|Any CPU
{8FAC191C-340D-47C6-B8AE-3D57783749C4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{8FAC191C-340D-47C6-B8AE-3D57783749C4}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8FAC191C-340D-47C6-B8AE-3D57783749C4}.Release|Any CPU.ActiveCfg = Release|Any CPU
{8FAC191C-340D-47C6-B8AE-3D57783749C4}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -158,6 +172,9 @@ Global
{2E9CFB92-E8E5-4466-8410-CCB5BA5CB6D1} = {5EE323DE-E69B-451A-8AC3-22DD6A004FBA}
{8BC99846-DDDB-40E7-B062-308BF8A1239F} = {2E9CFB92-E8E5-4466-8410-CCB5BA5CB6D1}
{B4247230-5289-4D17-BB0F-CB2FDBABA988} = {2E9CFB92-E8E5-4466-8410-CCB5BA5CB6D1}
{74EFCDE2-CB0F-49B1-9CEC-BE748EB1FBF7} = {88359036-4F35-487C-BF2C-4F31C7BC92D8}
{D2B5B5CA-57C2-4354-ADB7-47A6D81AD521} = {74EFCDE2-CB0F-49B1-9CEC-BE748EB1FBF7}
{8FAC191C-340D-47C6-B8AE-3D57783749C4} = {74EFCDE2-CB0F-49B1-9CEC-BE748EB1FBF7}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {17607E2C-4E8E-45A2-85BD-0A5808E1C0F3}
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ The following list key characteristics of each version as well as its related br
- 💚 `EventFlow.Redis`
- 🟠 `EventFlow.RabbitMQ`
- 🟢 `EventFlow.Sql`
- 🟠 `EventFlow.SQLite`
- 🟢 `EventFlow.SQLite`
- 🟢 `EventFlow.TestHelpers`

### Branches
Expand Down
1 change: 1 addition & 0 deletions RELEASE_NOTES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
### New in 1.1.1 (working version, not released yet)

* New: NuGet `EventFlow.SQLite` is now released as part of v1 and enables support for SQLite
* Fix: Invoking `UseEventPersistence` now removes any previously registered event persistence. This
fixes a service ordering issue in the following event store configurations
- MongoDB
Expand Down
14 changes: 6 additions & 8 deletions Source/EventFlow.SQLite.Tests/EventFlow.SQLite.Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
<Project Sdk="Microsoft.NET.Sdk">
<Import Project="../Common.props" />
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<TargetFrameworks>netcoreapp3.1;net6.0;net8.0</TargetFrameworks>
<IsPackable>False</IsPackable>
</PropertyGroup>

<ItemGroup>
<Folder Include="Properties\" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.6.1" />
<PackageReference Include="NUnit" Version="3.12.0" />
<PackageReference Include="nunit" Version="3.13.2" />
<PackageReference Include="NUnit3TestAdapter" Version="3.17.0" />
<PackageReference Include="System.Data.SQLite.Core" Version="1.0.113.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.6.1" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\EventFlow.SQLite\EventFlow.SQLite.csproj" />
<ProjectReference Include="..\EventFlow.TestHelpers\EventFlow.TestHelpers.csproj" />
</ItemGroup>

<ItemGroup>
<Service Include="{82a7f48d-3b50-4b1e-b82e-3ada8210c358}" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,14 @@
using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using EventFlow.Configuration;
using EventFlow.Core;
using EventFlow.Extensions;
using EventFlow.MetadataProviders;
using EventFlow.SQLite.Connections;
using EventFlow.SQLite.Extensions;
using EventFlow.TestHelpers;
using EventFlow.TestHelpers.Suites;
using Microsoft.Extensions.DependencyInjection;
using NUnit.Framework;

namespace EventFlow.SQLite.Tests.IntegrationTests.EventStores
Expand All @@ -41,19 +40,20 @@ public class SQLiteEventStoreTests : TestSuiteForEventStore
{
private string _databasePath;

protected override IRootResolver CreateRootResolver(IEventFlowOptions eventFlowOptions)
protected override IServiceProvider Configure(IEventFlowOptions eventFlowOptions)
{
_databasePath = Path.Combine(Path.GetTempPath(), $"{Guid.NewGuid():N}.sqlite");

using (File.Create(_databasePath)){ }

var resolver = eventFlowOptions
eventFlowOptions
.AddMetadataProvider<AddGuidMetadataProvider>()
.ConfigureSQLite(SQLiteConfiguration.New.SetConnectionString($"Data Source={_databasePath};Version=3;"))
.UseSQLiteEventStore()
.CreateResolver();
.UseSQLiteEventStore();

var connection = resolver.Resolve<ISQLiteConnection>();
var serviceProvider = base.Configure(eventFlowOptions);

var connection = serviceProvider.GetRequiredService<ISQLiteConnection>();
const string sqlCreateTable = @"
CREATE TABLE [EventFlow](
[GlobalSequenceNumber] [INTEGER] PRIMARY KEY ASC NOT NULL,
Expand All @@ -70,10 +70,10 @@ CREATE UNIQUE INDEX [IX_EventFlow_AggregateId_AggregateSequenceNumber] ON [Event
[AggregateId] ASC,
[AggregateSequenceNumber] ASC
)";
connection.ExecuteAsync(Label.Named("create-table"), CancellationToken.None, sqlCreateTable, null).Wait();
connection.ExecuteAsync(Label.Named("create-index"), CancellationToken.None, sqlCreateIndex, null).Wait();
connection.ExecuteAsync(Label.Named("create-table"), string.Empty, CancellationToken.None, sqlCreateTable, null).Wait();
connection.ExecuteAsync(Label.Named("create-index"), string.Empty, CancellationToken.None, sqlCreateIndex, null).Wait();

return resolver;
return serviceProvider;
}

[TearDown]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public async Task<IReadOnlyCollection<ThingyMessage>> ExecuteQueryAsync(ThingyGe
{
var readModels = await _sqLiteConnection.QueryAsync<SQLiteThingyMessageReadModel>(
Label.Named("sqlite-fetch-thingy-message-read-model"),
string.Empty,
cancellationToken,
"SELECT * FROM [ReadModel-ThingyMessage] WHERE ThingyId = @ThingyId",
new { ThingyId = query.ThingyId.Value })
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public async Task<Thingy> ExecuteQueryAsync(ThingyGetQuery query, CancellationTo
{
var readModels = await _sqLiteConnection.QueryAsync<SQLiteThingyReadModel>(
Label.Named("sqlite-fetch-test-read-model"),
string.Empty,
cancellationToken,
"SELECT * FROM [ReadModel-ThingyAggregate] WHERE AggregateId = @AggregateId",
new { AggregateId = query.ThingyId.Value })
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public SQLiteThingyGetVersionQueryHandler(
{
var readModels = await _sqLiteConnection.QueryAsync<SQLiteThingyReadModel>(
Label.Named("sqlite-fetch-test-read-model"),
string.Empty,
cancellationToken,
"SELECT * FROM [ReadModel-ThingyAggregate] WHERE AggregateId = @AggregateId",
new { AggregateId = query.ThingyId.Value })
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@

using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using EventFlow.Aggregates;
using EventFlow.ReadStores;
using EventFlow.Sql.ReadModels.Attributes;
Expand All @@ -43,19 +45,29 @@ public class SQLiteThingyMessageReadModel : IReadModel,

public string Message { get; set; }

public void Apply(IReadModelContext context, IDomainEvent<ThingyAggregate, ThingyId, ThingyMessageAddedEvent> domainEvent)
public Task ApplyAsync(
IReadModelContext context,
IDomainEvent<ThingyAggregate, ThingyId, ThingyMessageAddedEvent> domainEvent,
CancellationToken cancellationToken)
{
ThingyId = domainEvent.AggregateIdentity.Value;
Message = domainEvent.AggregateEvent.ThingyMessage.Message;

return Task.CompletedTask;
}

public void Apply(IReadModelContext context, IDomainEvent<ThingyAggregate, ThingyId, ThingyMessageHistoryAddedEvent> domainEvent)
public Task ApplyAsync(
IReadModelContext context,
IDomainEvent<ThingyAggregate, ThingyId, ThingyMessageHistoryAddedEvent> domainEvent,
CancellationToken cancellationToken)
{
ThingyId = domainEvent.AggregateIdentity.Value;

var messageId = new ThingyMessageId(context.ReadModelId);
var thingyMessage = domainEvent.AggregateEvent.ThingyMessages.Single(m => m.Id == messageId);
Message = thingyMessage.Message;

return Task.CompletedTask;
}

public ThingyMessage ToThingyMessage()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

using System.ComponentModel.DataAnnotations.Schema;
using System.Threading;
using System.Threading.Tasks;
using EventFlow.Aggregates;
using EventFlow.ReadStores;
using EventFlow.Sql.ReadModels.Attributes;
Expand All @@ -45,19 +47,34 @@ public class SQLiteThingyReadModel : IReadModel,
[SqlReadModelVersionColumn]
public int Version { get; set; }

public void Apply(IReadModelContext context, IDomainEvent<ThingyAggregate, ThingyId, ThingyPingEvent> domainEvent)
public Task ApplyAsync(
IReadModelContext context,
IDomainEvent<ThingyAggregate, ThingyId, ThingyPingEvent> domainEvent,
CancellationToken cancellationToken)
{
PingsReceived++;

return Task.CompletedTask;
}

public void Apply(IReadModelContext context, IDomainEvent<ThingyAggregate, ThingyId, ThingyDomainErrorAfterFirstEvent> domainEvent)
public Task ApplyAsync(
IReadModelContext context,
IDomainEvent<ThingyAggregate, ThingyId, ThingyDomainErrorAfterFirstEvent> domainEvent,
CancellationToken cancellationToken)
{
DomainErrorAfterFirstReceived = true;

return Task.CompletedTask;
}

public void Apply(IReadModelContext context, IDomainEvent<ThingyAggregate, ThingyId, ThingyDeletedEvent> domainEvent)
public Task ApplyAsync(
IReadModelContext context,
IDomainEvent<ThingyAggregate, ThingyId, ThingyDeletedEvent> domainEvent,
CancellationToken cancellationToken)
{
context.MarkForDeletion();

return Task.CompletedTask;
}

public Thingy ToThingy()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,16 @@
using System;
using System.IO;
using System.Threading;
using EventFlow.Configuration;
using EventFlow.Core;
using EventFlow.Extensions;
using EventFlow.SQLite.Connections;
using EventFlow.SQLite.Extensions;
using EventFlow.SQLite.Tests.IntegrationTests.ReadStores.QueryHandlers;
using EventFlow.SQLite.Tests.IntegrationTests.ReadStores.ReadModels;
using EventFlow.TestHelpers;
using EventFlow.TestHelpers.Aggregates;
using EventFlow.TestHelpers.Aggregates.Entities;
using EventFlow.TestHelpers.Suites;
using Microsoft.Extensions.DependencyInjection;
using NUnit.Framework;

namespace EventFlow.SQLite.Tests.IntegrationTests.ReadStores
Expand All @@ -45,24 +44,25 @@ public class SQLiteReadStoreTests : TestSuiteForReadModelStore

private string _databasePath;

protected override IRootResolver CreateRootResolver(IEventFlowOptions eventFlowOptions)
protected override IServiceProvider Configure(IEventFlowOptions eventFlowOptions)
{
_databasePath = Path.Combine(Path.GetTempPath(), $"{Guid.NewGuid():N}.sqlite");

using (File.Create(_databasePath)) { }

var resolver = eventFlowOptions
.RegisterServices(sr => sr.RegisterType(typeof(ThingyMessageLocator)))
eventFlowOptions
.RegisterServices(sr => sr.AddTransient(typeof(ThingyMessageLocator)))
.ConfigureSQLite(SQLiteConfiguration.New.SetConnectionString($"Data Source={_databasePath};Version=3;"))
.UseSQLiteReadModel<SQLiteThingyReadModel>()
.UseSQLiteReadModel<SQLiteThingyMessageReadModel, ThingyMessageLocator>()
.AddQueryHandlers(
typeof(SQLiteThingyGetQueryHandler),
typeof(SQLiteThingyGetVersionQueryHandler),
typeof(SQLiteThingyGetMessagesQueryHandler))
.CreateResolver();
typeof(SQLiteThingyGetMessagesQueryHandler));

var connection = resolver.Resolve<ISQLiteConnection>();
var serviceProvider = base.Configure(eventFlowOptions);

var connection = serviceProvider.GetRequiredService<ISQLiteConnection>();
const string sqlThingyAggregate = @"
CREATE TABLE [ReadModel-ThingyAggregate](
[Id] [INTEGER] PRIMARY KEY ASC,
Expand All @@ -79,10 +79,10 @@ [ThingyId] [nvarchar](64) NOT NULL,
[MessageId] [nvarchar](64) NOT NULL,
[Message] [nvarchar](512) NOT NULL
)";
connection.ExecuteAsync(Label.Named("create-table"), CancellationToken.None, sqlThingyAggregate, null).Wait();
connection.ExecuteAsync(Label.Named("create-table"), CancellationToken.None, sqlThingyMessage, null).Wait();
connection.ExecuteAsync(Label.Named("create-table"), string.Empty, CancellationToken.None, sqlThingyAggregate, null).Wait();
connection.ExecuteAsync(Label.Named("create-table"), string.Empty, CancellationToken.None, sqlThingyMessage, null).Wait();

return resolver;
return serviceProvider;
}

[TearDown]
Expand Down
6 changes: 3 additions & 3 deletions Source/EventFlow.SQLite/Connections/SQLiteConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,20 @@
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

using EventFlow.Core;
using EventFlow.Logs;
using EventFlow.Sql.Connections;
using EventFlow.SQLite.RetryStrategies;
using Microsoft.Extensions.Logging;

namespace EventFlow.SQLite.Connections
{
public class SQLiteConnection : SqlConnection<ISQLiteConfiguration, ISQLiteErrorRetryStrategy, ISQLiteConnectionFactory>, ISQLiteConnection
{
public SQLiteConnection(
ILog log,
ILogger<SQLiteConnection> logger,
ISQLiteConfiguration configuration,
ISQLiteConnectionFactory connectionFactory,
ITransientFaultHandler<ISQLiteErrorRetryStrategy> transientFaultHandler)
: base(log, configuration, connectionFactory, transientFaultHandler)
: base(logger, configuration, connectionFactory, transientFaultHandler)
{
}
}
Expand Down
11 changes: 4 additions & 7 deletions Source/EventFlow.SQLite/EventFlow.SQLite.csproj
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>netstandard2.0</TargetFrameworks>
<TargetFrameworks>netstandard2.1;netcoreapp3.1;net6.0;net8.0</TargetFrameworks>
<Title>EventFlow.SQLite</Title>
<Authors>Rasmus Mikkelsen</Authors>
<Company>Rasmus Mikkelsen</Company>
<Copyright>Copyright (c) Rasmus Mikkelsen 2015 - 2021</Copyright>
<Description>SQLite event store for EventFlow</Description>
<PackageTags>CQRS ES event sourcing SQLite</PackageTags>
<PackageReleaseNotes>UPDATED BY BUILD</PackageReleaseNotes>
<DocumentationFile>bin\$(Configuration)\$(TargetFramework)\EventFlow.SQLite.xml</DocumentationFile>
<IsPackable>true</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="dbup-sqlserver" Version="4.1.0" />
<PackageReference Include="dbup-sqlserver" Version="5.0.40" />
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0" PrivateAssets="All" />
<PackageReference Include="System.Data.SQLite.Core" Version="1.0.113.1" />
<PackageReference Include="System.Data.SQLite.Core" Version="1.0.119" />
</ItemGroup>

<ItemGroup>
Expand Down
Loading

0 comments on commit a13f9ec

Please sign in to comment.