|
1 | 1 | using System; |
2 | | -using JasperFx.Core; |
| 2 | +using System.Linq; |
3 | 3 | using JasperFx.Core.Reflection; |
4 | 4 | using Marten; |
5 | 5 | using Marten.Schema; |
| 6 | +using Marten.Storage; |
| 7 | +using Marten.Storage.Metadata; |
6 | 8 | using Marten.Testing.Harness; |
7 | 9 | using Shouldly; |
8 | 10 | using Xunit; |
@@ -66,6 +68,75 @@ public void should_allow_foreign_key_on_id_field() |
66 | 68 | .ShouldContain(x => x.ColumnNames[0] == "id"); |
67 | 69 | } |
68 | 70 |
|
| 71 | + [Fact] |
| 72 | + public void should_not_include_tenant_id_in_foreign_key_from_tenanted_doc_to_not_tenanted_doc() |
| 73 | + { |
| 74 | + var store = StoreOptions(_ => |
| 75 | + { |
| 76 | + _.MultiTenantedWithSingleServer(ConnectionSource.ConnectionString, c => |
| 77 | + { |
| 78 | + c.WithTenants("tenant1").InDatabaseNamed("postgres"); |
| 79 | + }); |
| 80 | + |
| 81 | + _.Schema.For<Foo>() |
| 82 | + .SingleTenanted() |
| 83 | + .Identity(x => x.FooId); |
| 84 | + _.Schema.For<Bar>() |
| 85 | + .MultiTenanted() |
| 86 | + .Identity(x => x.BarId) |
| 87 | + .ForeignKey<Foo>(x => x.FooId); |
| 88 | + }); |
| 89 | + |
| 90 | + var mapping = store.Options.Storage.MappingFor(typeof(Bar)); |
| 91 | + new DocumentTable(mapping).ForeignKeys.Single().ColumnNames.ShouldNotContain(TenantIdColumn.Name); |
| 92 | + } |
| 93 | + |
| 94 | + [Fact] |
| 95 | + public void should_include_tenant_id_in_foreign_key_between_tenanted_docs() |
| 96 | + { |
| 97 | + var store = StoreOptions(_ => |
| 98 | + { |
| 99 | + _.MultiTenantedWithSingleServer(ConnectionSource.ConnectionString, c => |
| 100 | + { |
| 101 | + c.WithTenants("tenant1").InDatabaseNamed("postgres"); |
| 102 | + }); |
| 103 | + |
| 104 | + _.Schema.For<Foo>() |
| 105 | + .MultiTenanted() |
| 106 | + .Identity(x => x.FooId); |
| 107 | + _.Schema.For<Bar>() |
| 108 | + .MultiTenanted() |
| 109 | + .Identity(x => x.BarId) |
| 110 | + .ForeignKey<Foo>(x => x.FooId); |
| 111 | + }); |
| 112 | + |
| 113 | + var mapping = store.Options.Storage.MappingFor(typeof(Bar)); |
| 114 | + new DocumentTable(mapping).ForeignKeys.Single().ColumnNames.ShouldContain(TenantIdColumn.Name); |
| 115 | + } |
| 116 | + |
| 117 | + [Fact] |
| 118 | + public void should_not_include_tenant_id_between_single_tenanted_docs() |
| 119 | + { |
| 120 | + var store = StoreOptions(_ => |
| 121 | + { |
| 122 | + _.MultiTenantedWithSingleServer(ConnectionSource.ConnectionString, c => |
| 123 | + { |
| 124 | + c.WithTenants("tenant1").InDatabaseNamed("postgres"); |
| 125 | + }); |
| 126 | + |
| 127 | + _.Schema.For<Foo>() |
| 128 | + .SingleTenanted() |
| 129 | + .Identity(x => x.FooId); |
| 130 | + _.Schema.For<Bar>() |
| 131 | + .SingleTenanted() |
| 132 | + .Identity(x => x.BarId) |
| 133 | + .ForeignKey<Foo>(x => x.FooId); |
| 134 | + }); |
| 135 | + |
| 136 | + var mapping = store.Options.Storage.MappingFor(typeof(Bar)); |
| 137 | + new DocumentTable(mapping).ForeignKeys.Single().ColumnNames.ShouldNotContain(TenantIdColumn.Name); |
| 138 | + } |
| 139 | + |
69 | 140 | #region sample_issue-with-fk-attribute |
70 | 141 | public class Issue |
71 | 142 | { |
@@ -138,4 +209,10 @@ public class FooExtra |
138 | 209 | { |
139 | 210 | public Guid FooId { get; set; } |
140 | 211 | } |
| 212 | + |
| 213 | + public class Bar |
| 214 | + { |
| 215 | + public Guid BarId { get; set; } |
| 216 | + public Guid FooId { get; set; } |
| 217 | + } |
141 | 218 | } |
0 commit comments