Skip to content

Commit

Permalink
You can use generic types for Events because why not. Closes GH-1069
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremydmiller committed Aug 23, 2018
1 parent b40bc61 commit 72170cc
Show file tree
Hide file tree
Showing 9 changed files with 191 additions and 47 deletions.
2 changes: 1 addition & 1 deletion src/Marten.CommandLine/Marten.CommandLine.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<Description>Command line tooling for managing Marten development</Description>
<VersionPrefix>2.9.0</VersionPrefix>
<Authors>Jeremy D. Miller;Tim Cools;Jeff Doolittle</Authors>
<TargetFrameworks>net46;netstandard1.3;netstandard2.0</TargetFrameworks>
<TargetFrameworks>net46;netstandard2.0</TargetFrameworks>
<AssemblyName>Marten.CommandLine</AssemblyName>
<PackageId>Marten.CommandLine</PackageId>
<PackageIconUrl>http://jasperfx.github.io/marten/content/images/emblem.png</PackageIconUrl>
Expand Down
12 changes: 7 additions & 5 deletions src/Marten.Testing/Acceptance/computed_indexes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -121,11 +121,13 @@ public void specifying_an_index_type_should_create_the_index_with_that_type()
var data = Target.GenerateRandomData(100).ToArray();
theStore.BulkInsert(data.ToArray());

theStore.Tenancy.Default.DbObjects.AllIndexes()
var ddl = theStore.Tenancy.Default.DbObjects.AllIndexes()
.Where(x => x.Name == "mt_doc_target_idx_number")
.Select(x => x.DDL.ToLower())
.First()
.ShouldContain("mt_doc_target_idx_number on mt_doc_target using brin");
.First();

ddl.ShouldContain("mt_doc_target_idx_number on");
ddl.ShouldContain("mt_doc_target using brin");
}

[Fact]
Expand All @@ -149,7 +151,7 @@ public void create_multi_property_index()
.DDL
.ToLower();

ddl.ShouldContain("index mt_doc_target_idx_user_idflag on mt_doc_target");
ddl.ShouldContain("index mt_doc_target_idx_user_idflag on");
ddl.ShouldContain("((((data ->> 'userid'::text))::uuid), (((data ->> 'flag'::text))::boolean))");
}

Expand All @@ -168,7 +170,7 @@ public void creating_index_using_date_should_work()
.Where(x => x.Name == "mt_doc_target_idx_date")
.Select(x => x.DDL.ToLower())
.First()
.ShouldContain("mt_doc_target_idx_date on mt_doc_target");
.ShouldContain("mt_doc_target_idx_date on");
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
using System;
using System.Linq;
using Shouldly;
using Xunit;

namespace Marten.Testing.Bugs
{
public class Bug_1069_using_generic_event_types_because_why_not : IntegratedFixture
{
public class Envelope<T>
{
public T Value { get; set; }
public Guid ExecutingUserId { get; set; }
}

public class Created
{
public Guid Id { get; set; }
}

public class Updated
{
public String UpdateValue { get; set; }
}

[Fact]
public void try_to_save_then_load_events()
{
var streamId = Guid.NewGuid();
var event1 = new Envelope<Created>{Value = new Created{Id = Guid.NewGuid()}};
var event2 = new Envelope<Updated>{Value = new Updated{UpdateValue = "something"}};

using (var session = theStore.LightweightSession())
{
session.Events.StartStream(streamId, event1, event2);
session.SaveChanges();
}

using (var session = theStore.LightweightSession())
{


var events = session.Events.FetchStream(streamId);
events.Select(x => x.Data.GetType())
.ShouldHaveTheSameElementsAs(typeof(Envelope<Created>), typeof(Envelope<Updated>));
}
}

[Fact]
public void try_to_save_then_load_events_across_stores()
{
var streamId = Guid.NewGuid();
var event1 = new Envelope<Created>{Value = new Created{Id = Guid.NewGuid()}};
var event2 = new Envelope<Updated>{Value = new Updated{UpdateValue = "something"}};

using (var session = theStore.LightweightSession())
{
session.Events.StartStream(streamId, event1, event2);
session.SaveChanges();
}

var store2 = DocumentStore.For(_ =>
{
_.Connection(ConnectionSource.ConnectionString);
_.AutoCreateSchemaObjects = AutoCreate.All;
});

using (var session = store2.LightweightSession())
{
var events = session.Events.FetchStream(streamId);
events.Select(x => x.Data.GetType())
.ShouldHaveTheSameElementsAs(typeof(Envelope<Created>), typeof(Envelope<Updated>));
}
}
}
}
15 changes: 2 additions & 13 deletions src/Marten.Testing/Marten.Testing.csproj
Original file line number Diff line number Diff line change
@@ -1,22 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net46;netcoreapp2.0;netcoreapp2.1</TargetFrameworks>
<AssemblyName>Marten.Testing</AssemblyName>
<PackageId>Marten.Testing</PackageId>
<GenerateRuntimeConfigurationFiles>true</GenerateRuntimeConfigurationFiles>
<GenerateAssemblyTitleAttribute>false</GenerateAssemblyTitleAttribute>
</PropertyGroup>

<PropertyGroup Condition="'$(OS)' != 'Windows_NT' AND '$(TargetFramework)'== 'net46'">
<FrameworkPathOverride>$(NuGetPackageRoot)microsoft.targetingpack.netframework.v4.6/1.0.1/lib/net46/</FrameworkPathOverride>
<RestoreAdditionalProjectSources>https://dotnet.myget.org/F/dotnet-core/api/v3/index.json</RestoreAdditionalProjectSources>
</PropertyGroup>

<ItemGroup Condition="'$(OS)' != 'Windows_NT' AND '$(TargetFramework)'== 'net46'">
<PackageReference Include="Microsoft.TargetingPack.NETFramework.v4.6" Version="1.0.1" ExcludeAssets="All" PrivateAssets="All" />
<PackageReference Include="Microsoft.TargetingPack.NETFramework.v4.6" Version="1.0.1" ExcludeAssets="All" PrivateAssets="All" />
</ItemGroup>

<ItemGroup>
<None Update="**/*.js;connection.txt">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
Expand All @@ -26,12 +22,10 @@
</None>
<None Include="App.config" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Marten\Marten.csproj" />
<ProjectReference Include="..\Marten.Testing.OtherAssembly\Marten.Testing.OtherAssembly.csproj" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Jil" Version="2.15.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.3.0" />
Expand All @@ -41,15 +35,12 @@
<PackageReference Include="Shouldly" Version="2.8.0" />
<PackageReference Include="structuremap" Version="4.5.2" />
</ItemGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'netcoreapp1.0' or '$(TargetFramework)' == 'netcoreapp2.0' or '$(TargetFramework)' == 'netcoreapp2.1'">
<PackageReference Include="Microsoft.Extensions.PlatformAbstractions" Version="1.1.0" />
</ItemGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'netcoreapp1.0' or '$(TargetFramework)' == 'netcoreapp2.0' or '$(TargetFramework)' == 'netcoreapp2.1'">
<Compile Remove="**\Fixtures\**\*.cs;**\Github\**\*.cs;StorytellerHarness.cs" />
</ItemGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'net46' ">
<PackageReference Include="HtmlTags" Version="3.0.0.186" />
<PackageReference Include="Microsoft.NETCore.Platforms" Version="1.1.0" />
Expand All @@ -60,9 +51,7 @@
<Reference Include="System.Transactions" />
<Reference Include="System.Threading.Tasks" />
</ItemGroup>

<ItemGroup>
<Service Include="{82a7f48d-3b50-4b1e-b82e-3ada8210c358}" />
</ItemGroup>

</Project>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,8 @@ public void can_revert_indexes_that_changed()
var patch = store.Schema.ToPatch();

patch.RollbackDDL.ShouldContain("drop index public.mt_doc_user_idx_user_name;");
patch.RollbackDDL.ShouldContain("CREATE INDEX mt_doc_user_idx_user_name ON mt_doc_user USING btree (user_name);");
patch.RollbackDDL.ShouldContain("CREATE INDEX mt_doc_user_idx_user_name ON");
patch.RollbackDDL.ShouldContain("mt_doc_user USING btree (user_name);");

}
}
Expand Down
4 changes: 3 additions & 1 deletion src/Marten/Events/EventMapping.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ protected EventMapping(EventGraph parent, Type eventType)
_parent = parent;
DocumentType = eventType;

EventTypeName = DocumentType.Name.ToTableAlias();


EventTypeName = eventType.IsGenericType ? eventType.ShortNameInCode() : DocumentType.Name.ToTableAlias();
IdMember = DocumentType.GetProperty(nameof(IEvent.Id));

_inner = new DocumentMapping(eventType, parent.Options);
Expand Down
14 changes: 7 additions & 7 deletions src/Marten/Events/EventsTable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,24 @@ public EventsTable(EventGraph events) : base(new DbObjectName(events.DatabaseSch
AddColumn("stream_id", stringIdType, (events.TenancyStyle != TenancyStyle.Conjoined) ? $"REFERENCES {events.DatabaseSchemaName}.mt_streams ON DELETE CASCADE" : null);
AddColumn("version", "integer", "NOT NULL");
AddColumn("data", "jsonb", "NOT NULL");
AddColumn("type", "varchar(100)", "NOT NULL");
AddColumn("type", "varchar(500)", "NOT NULL");
AddColumn("timestamp", "timestamptz", "default (now()) NOT NULL");
AddColumn<TenantIdColumn>();
AddColumn(new DotNetTypeColumn { Directive = "NULL" });

if (events.TenancyStyle == TenancyStyle.Conjoined)
{
Constraints.Add($"FOREIGN KEY(stream_id, {TenantIdColumn.Name}) REFERENCES {events.DatabaseSchemaName}.mt_streams(id, {TenantIdColumn.Name})");
{
Constraints.Add($"FOREIGN KEY(stream_id, {TenantIdColumn.Name}) REFERENCES {events.DatabaseSchemaName}.mt_streams(id, {TenantIdColumn.Name})");
Constraints.Add($"CONSTRAINT pk_mt_events_stream_and_version UNIQUE(stream_id, {TenantIdColumn.Name}, version)");
}
else
{
Constraints.Add("CONSTRAINT pk_mt_events_stream_and_version UNIQUE(stream_id, version)");
else
{
Constraints.Add("CONSTRAINT pk_mt_events_stream_and_version UNIQUE(stream_id, version)");
}

Constraints.Add("CONSTRAINT pk_mt_events_id_unique UNIQUE(id)");
}
}


// ENDSAMPLE
}
2 changes: 1 addition & 1 deletion src/Marten/Marten.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<Description>Postgresql as a Document Db and Event Store for .Net Development</Description>
<VersionPrefix>2.9.0</VersionPrefix>
<Authors>Jeremy D. Miller;Tim Cools;Jeff Doolittle;James Hopper</Authors>
<TargetFrameworks>net46;netstandard1.3;netstandard2.0</TargetFrameworks>
<TargetFrameworks>net46;netstandard2.0</TargetFrameworks>
<AssemblyName>Marten</AssemblyName>
<PackageId>Marten</PackageId>
<PackageIconUrl>http://jasperfx.github.io/marten/content/images/emblem.png</PackageIconUrl>
Expand Down
Loading

0 comments on commit 72170cc

Please sign in to comment.