Skip to content

Commit

Permalink
Updated TableCreationScript for MsSqlServerEventStore, updated unit t…
Browse files Browse the repository at this point in the history
…ests and version update query is now explicit.
  • Loading branch information
pjvds committed Jun 20, 2010
1 parent 58e9fbc commit 6fc6036
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ public class MsSqlServerEventStoreTests
[Serializable]
public class CustomerCreatedEvent : SourcedEvent
{
protected CustomerCreatedEvent()
{
}

public CustomerCreatedEvent(Guid eventIdentifier, Guid aggregateRootId, long eventSequence, DateTime eventTimeStamp, string name, int age)
: base(eventIdentifier, aggregateRootId, eventSequence, eventTimeStamp)
{
Expand Down Expand Up @@ -47,6 +51,11 @@ public Guid CustomerId
public string NewName
{ get; set; }

protected CustomerNameChanged()
{

}

public CustomerNameChanged(Guid eventIdentifier, Guid aggregateRootId, long eventSequence, DateTime eventTimeStamp, string newName)
: base(eventIdentifier, aggregateRootId, eventSequence, eventTimeStamp)
{
Expand Down Expand Up @@ -147,8 +156,7 @@ public void Retrieving_all_events_should_return_the_same_as_added()
var targetStore = new MsSqlServerEventStore(DEFAULT_CONNECTION);
var id = Guid.NewGuid();

int sequenceCounter = 0;

int sequenceCounter = 1;
var events = new SourcedEvent[]
{
new CustomerCreatedEvent(Guid.NewGuid(), id, sequenceCounter++, DateTime.UtcNow, "Foo",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ public class MsSqlServerEventStore : IEventStore, ISnapshotStore

private const String SelectVersionQuery = "SELECT [Version] FROM [EventSources] WHERE [Id] = @id";

private const String UpdateEventSourceVersionQuery = "UPDATE [EventSources] SET [Version] = (SELECT Count(*) FROM [Events] WHERE [EventSourceId] = @Id) WHERE [Id] = @id";
private const String UpdateEventSourceVersionQuery = "UPDATE [EventSources] SET [Version] = @NewVersion WHERE [Id] = @id";

private const String InsertSnapshot = "DELETE FROM [Snapshots] WHERE [EventSourceId]=@EventSourceId; INSERT INTO [Snapshots]([EventSourceId], [Version], [SnapshotType], [SnapshotData]) VALUES (@EventSourceId, @Version, @SnapshotType, @SnapshotData)";
private const String InsertSnapshot = "DELETE FROM [Snapshots] WHERE [EventSourceId]=@EventSourceId; INSERT INTO [Snapshots]([EventSourceId], [Timestamp], [Version], [Type], [Data]) VALUES (@EventSourceId, GETDATE(), @Version, @Type, @Data)";

private const String SelectLatestSnapshot = "SELECT TOP 1 * FROM [Snapshots] WHERE [EventSourceId]=@EventSourceId ORDER BY Version DESC";
#endregion
Expand Down Expand Up @@ -179,8 +179,8 @@ public void SaveShapshot(ISnapshot snapshot)
command.Transaction = transaction;
command.Parameters.AddWithValue("EventSourceId", snapshot.EventSourceId);
command.Parameters.AddWithValue("Version", snapshot.EventSourceVersion);
command.Parameters.AddWithValue("SnapshotType", snapshot.GetType().AssemblyQualifiedName);
command.Parameters.AddWithValue("SnapshotData", data);
command.Parameters.AddWithValue("Type", snapshot.GetType().AssemblyQualifiedName);
command.Parameters.AddWithValue("Data", data);
command.ExecuteNonQuery();
}
}
Expand Down Expand Up @@ -219,8 +219,7 @@ public ISnapshot GetSnapshot(Guid eventSourceId)
{
if (reader.Read())
{
var version = (long) reader["Version"];
var snapshotData = (byte[]) reader["SnapshotData"];
var snapshotData = (byte[]) reader["Data"];
using (var buffer = new MemoryStream(snapshotData))
{
var formatter = new BinaryFormatter();
Expand Down Expand Up @@ -280,6 +279,7 @@ private void UpdateEventSourceVersion(IEventSource eventSource, SqlTransaction t
{
command.Transaction = transaction;
command.Parameters.AddWithValue("Id", eventSource.EventSourceId);
command.Parameters.AddWithValue("NewVersion", eventSource.Version);
command.ExecuteNonQuery();
}
}
Expand Down
29 changes: 19 additions & 10 deletions Framework/src/Ncqrs/Eventing/Storage/SQL/TableCreationScript.sql
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
CREATE TABLE
[dbo].[Events]
(
[Id] [uniqueidentifier] NOT NULL, [EventSourceId] [uniqueidentifier] NOT NULL, [Sequence] [bigint],
[TimeStamp] [datetime] NOT NULL, [Data] [varbinary](max) NOT NULL, [Name] [varchar](max) NOT NULL
)
ON [PRIMARY];
GO;
CREATE TABLE [dbo].[EventSources]([Id] [uniqueidentifier] NOT NULL, [Type] [nvarchar](255) NOT NULL, [Version] [int] NOT NULL) ON [PRIMARY];
CREATE TABLE [dbo].[Snaptshots]([EventSourceId] [uniqueidentifier] NOT NuLL, [Version] [bigint], [TimeStamp] [datetime] NOt NULL, [Data] [varbinary](max) NOT NULL) ON [PRIMARY]
CREATE TABLE [dbo].[Events]
(
[Id] [uniqueidentifier] NOT NULL, [EventSourceId] [uniqueidentifier] NOT NULL, [Sequence] [bigint],
[TimeStamp] [datetime] NOT NULL, [Data] [varbinary](max) NOT NULL, [Name] [varchar](max) NOT NULL
) ON [PRIMARY]
GO

CREATE TABLE [dbo].[EventSources]
(
[Id] [uniqueidentifier] NOT NULL, [Type] [nvarchar](255) NOT NULL, [Version] [int] NOT NULL
) ON [PRIMARY]
GO

CREATE TABLE [dbo].[Snapshots]
(
[EventSourceId] [uniqueidentifier] NOT NULL, [Version] [bigint], [TimeStamp] [datetime] NOT NULL,
[Type] varchar(255) NOT NULL, [Data] [varbinary](max) NOT NULL
) ON [PRIMARY]
GO

0 comments on commit 6fc6036

Please sign in to comment.