Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions src/CoreTests/Partitioning/partitioning_and_foreign_keys.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
using System.Linq;
using System.Threading.Tasks;
using Marten.Schema;
using Marten.Storage;
using Marten.Testing.Documents;
using Marten.Testing.Harness;
using Shouldly;
Expand Down Expand Up @@ -63,4 +66,35 @@ await Should.ThrowAsync<InvalidForeignKeyException>(async () =>
await theStore.Storage.ApplyAllConfiguredChangesToDatabaseAsync();
});
}

[Fact]
public async Task partitioned_by_tenant_id_to_partitioned_to_tenant_id_and_tenant_id_is_sorted_first()
{
StoreOptions(opts =>
{
opts.Schema.For<Issue>()
.ForeignKey<User>(x => x.AssigneeId);

opts.Schema.For<User>();

opts.Policies.AllDocumentsAreMultiTenantedWithPartitioning(partitioning =>
{
partitioning.ByHash("one", "two", "three");
});
});

// Just smoke test that it works
await theStore.Storage.ApplyAllConfiguredChangesToDatabaseAsync();

await theStore.Storage.Database.AssertDatabaseMatchesConfigurationAsync();

var mapping = (DocumentMapping)theStore.Options.Storage.FindMapping(typeof(Issue));
var table = new DocumentTable(mapping);

var fk = table.ForeignKeys.Single();
fk.ColumnNames.ShouldBe(["tenant_id", "assignee_id"]);
fk.LinkedNames.ShouldBe(["tenant_id", "id"]);

fk.Name.ShouldBe("mt_doc_issue_tenant_id_assignee_id_fkey");
}
}
2 changes: 1 addition & 1 deletion src/Marten.CommandLine/Marten.CommandLine.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
<PackageReference Include="JasperFx.CodeGeneration.Commands" Version="3.7.1" />
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="8.0.0" PrivateAssets="All"/>
<PackageReference Include="Oakton" Version="6.3.0" />
<PackageReference Include="Weasel.CommandLine" Version="7.13.1" />
<PackageReference Include="Weasel.CommandLine" Version="7.13.2" />
</ItemGroup>
<Import Project="../../Analysis.Build.props"/>
</Project>
2 changes: 1 addition & 1 deletion src/Marten/Marten.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
<PackageReference Include="Polly.Core" Version="8.5.0" />
<PackageReference Include="System.Threading.Tasks.Dataflow" Version="8.0.0" />
<PackageReference Include="System.Text.Json" Version="8.0.5" />
<PackageReference Include="Weasel.Postgresql" Version="7.13.1" />
<PackageReference Include="Weasel.Postgresql" Version="7.13.2" />
</ItemGroup>

<!--SourceLink specific settings-->
Expand Down
18 changes: 18 additions & 0 deletions src/Marten/Schema/DocumentMapping.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
using Marten.Schema.Indexing.FullText;
using Marten.Schema.Indexing.Unique;
using Marten.Storage;
using Marten.Storage.Metadata;
using NpgsqlTypes;
using Weasel.Core;
using Weasel.Postgresql;
Expand Down Expand Up @@ -961,3 +962,20 @@ public DocumentCodeGen(DocumentMapping mapping)
public string AccessId { get; }
public string ParameterValue { get; }
}

internal static class ForeignKeyExtensions
{
public static void TryMoveTenantIdFirst(this ForeignKey foreignKey, DocumentMapping mapping)
{
// Guard clause, do nothing if this document is not tenanted
if (mapping.TenancyStyle == TenancyStyle.Single) return;

foreignKey.ColumnNames = new string[] { TenantIdColumn.Name }
.Concat(foreignKey.ColumnNames.Where(x => x != TenantIdColumn.Name)).ToArray();

foreignKey.LinkedNames = new string[] { TenantIdColumn.Name }
.Concat(foreignKey.LinkedNames.Where(x => x != TenantIdColumn.Name)).ToArray();

foreignKey.Name = $"{mapping.TableName.Name}_{foreignKey.ColumnNames.Join("_")}_fkey";
}
}
7 changes: 7 additions & 0 deletions src/Marten/Storage/DocumentTable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,13 @@ public DocumentTable(DocumentMapping mapping): base(mapping.TableName)
}

Indexes.AddRange(mapping.Indexes);

// tenant_id should always be first
foreach (var foreignKey in mapping.ForeignKeys)
{
foreignKey.TryMoveTenantIdFirst(mapping);
}

ForeignKeys.AddRange(mapping.ForeignKeys);

Partitioning = mapping.Partitioning;
Expand Down