Skip to content

Commit acc6497

Browse files
Add index to UserSecurityPolicy (#7042)
* Add index to UserSecurityPolicy * Use fluent syntax for new index
1 parent 4c38bb4 commit acc6497

6 files changed

Lines changed: 228 additions & 1 deletion

File tree

src/NuGet.Services.Entities/UserSecurityPolicy.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public UserSecurityPolicy(string name, string subscription, string value = null)
3131
/// Policy key.
3232
/// </summary>
3333
public int Key { get; set; }
34-
34+
3535
/// <summary>
3636
/// User key.
3737
/// </summary>

src/NuGetGallery.Core/Entities/EntitiesContext.cs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ public class EntitiesContext
1515
: ObjectMaterializedInterceptingDbContext, IEntitiesContext
1616
{
1717
private const string CertificatesThumbprintIndex = "IX_Certificates_Thumbprint";
18+
private const string UserSecurityPolicyUserKeyNameSubscriptionIndex = "IX_UserSecurityPolicy_UserKeyNameSubscription";
1819

1920
static EntitiesContext()
2021
{
@@ -219,6 +220,50 @@ protected override void OnModelCreating(DbModelBuilder modelBuilder)
219220
modelBuilder.Entity<UserSecurityPolicy>()
220221
.HasKey(p => p.Key);
221222

223+
modelBuilder.Entity<UserSecurityPolicy>()
224+
.Property(e => e.UserKey)
225+
.IsRequired()
226+
.HasColumnAnnotation(
227+
IndexAnnotation.AnnotationName,
228+
new IndexAnnotation(new[]
229+
{
230+
new IndexAttribute(UserSecurityPolicyUserKeyNameSubscriptionIndex, order: 0)
231+
{
232+
IsUnique = true
233+
}
234+
})
235+
);
236+
237+
modelBuilder.Entity<UserSecurityPolicy>()
238+
.Property(e => e.Name)
239+
.HasMaxLength(256)
240+
.IsRequired()
241+
.HasColumnAnnotation(
242+
IndexAnnotation.AnnotationName,
243+
new IndexAnnotation(new[]
244+
{
245+
new IndexAttribute(UserSecurityPolicyUserKeyNameSubscriptionIndex, order: 1)
246+
{
247+
IsUnique = true
248+
}
249+
})
250+
);
251+
252+
modelBuilder.Entity<UserSecurityPolicy>()
253+
.Property(e => e.Subscription)
254+
.HasMaxLength(256)
255+
.IsRequired()
256+
.HasColumnAnnotation(
257+
IndexAnnotation.AnnotationName,
258+
new IndexAnnotation(new[]
259+
{
260+
new IndexAttribute(UserSecurityPolicyUserKeyNameSubscriptionIndex, order: 2)
261+
{
262+
IsUnique = true
263+
}
264+
})
265+
);
266+
222267
modelBuilder.Entity<EmailMessage>()
223268
.HasKey(em => em.Key);
224269

src/NuGetGallery/Migrations/201904081230004_AddIndexToUserSecurityPolicy.Designer.cs

Lines changed: 29 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
namespace NuGetGallery.Migrations
2+
{
3+
using System;
4+
using System.Data.Entity.Migrations;
5+
6+
public partial class AddIndexToUserSecurityPolicy : DbMigration
7+
{
8+
public override void Up()
9+
{
10+
DropIndex("dbo.UserSecurityPolicies", new[] { "UserKey" });
11+
CreateIndex("dbo.UserSecurityPolicies", new[] { "UserKey", "Name", "Subscription" }, unique: true, name: "IX_UserSecurityPolicy_UserKeyNameSubscription");
12+
}
13+
14+
public override void Down()
15+
{
16+
DropIndex("dbo.UserSecurityPolicies", "IX_UserSecurityPolicy_UserKeyNameSubscription");
17+
CreateIndex("dbo.UserSecurityPolicies", "UserKey");
18+
}
19+
}
20+
}

src/NuGetGallery/Migrations/201904081230004_AddIndexToUserSecurityPolicy.resx

Lines changed: 126 additions & 0 deletions
Large diffs are not rendered by default.

src/NuGetGallery/NuGetGallery.csproj

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,10 @@
228228
<Compile Include="Migrations\201903020136235_CvesCanBeEmpty.Designer.cs">
229229
<DependentUpon>201903020136235_CvesCanBeEmpty.cs</DependentUpon>
230230
</Compile>
231+
<Compile Include="Migrations\201904081230004_AddIndexToUserSecurityPolicy.cs" />
232+
<Compile Include="Migrations\201904081230004_AddIndexToUserSecurityPolicy.Designer.cs">
233+
<DependentUpon>201904081230004_AddIndexToUserSecurityPolicy.cs</DependentUpon>
234+
</Compile>
231235
<Compile Include="Queries\AutocompleteCveIdQueryResults.cs" />
232236
<Compile Include="Queries\AutocompleteCweIdQueryResults.cs" />
233237
<Compile Include="Queries\AutocompleteCveIdQueryResult.cs" />
@@ -1709,6 +1713,9 @@
17091713
<EmbeddedResource Include="Migrations\201903020136235_CvesCanBeEmpty.resx">
17101714
<DependentUpon>201903020136235_CvesCanBeEmpty.cs</DependentUpon>
17111715
</EmbeddedResource>
1716+
<EmbeddedResource Include="Migrations\201904081230004_AddIndexToUserSecurityPolicy.resx">
1717+
<DependentUpon>201904081230004_AddIndexToUserSecurityPolicy.cs</DependentUpon>
1718+
</EmbeddedResource>
17121719
<EmbeddedResource Include="OData\QueryAllowed\Data\apiv1packages.json" />
17131720
<EmbeddedResource Include="OData\QueryAllowed\Data\apiv1search.json" />
17141721
<EmbeddedResource Include="OData\QueryAllowed\Data\apiv2getupdates.json" />

0 commit comments

Comments
 (0)