Skip to content

Commit

Permalink
[Prefix] Merge prefix reservation DB schema and service changes into …
Browse files Browse the repository at this point in the history
…DEV (#4590)

Merge DEV changes and add prefix reservation db schema, service changes.
  • Loading branch information
shishirx34 committed Aug 24, 2017
1 parent 4771a58 commit 450fc6c
Show file tree
Hide file tree
Showing 25 changed files with 1,551 additions and 93 deletions.
18 changes: 18 additions & 0 deletions src/NuGetGallery.Core/Entities/EntitiesContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public EntitiesContext(string connectionString, bool readOnly)
public IDbSet<Scope> Scopes { get; set; }
public IDbSet<User> Users { get; set; }
public IDbSet<UserSecurityPolicy> UserSecurityPolicies { get; set; }
public IDbSet<ReservedNamespace> ReservedNamespaces { get; set; }

IDbSet<T> IEntitiesContext.Set<T>()
{
Expand Down Expand Up @@ -127,6 +128,23 @@ protected override void OnModelCreating(DbModelBuilder modelBuilder)
.HasForeignKey(p => p.UserKey)
.WillCascadeOnDelete(true);

modelBuilder.Entity<ReservedNamespace>()
.HasKey(p => p.Key);

modelBuilder.Entity<ReservedNamespace>()
.HasMany<PackageRegistration>(rn => rn.PackageRegistrations)
.WithMany(pr => pr.ReservedNamespaces)
.Map(prrn => prrn.ToTable("ReservedNamespaceRegistrations")
.MapLeftKey("ReservedNamespaceKey")
.MapRightKey("PackageRegistrationKey"));

modelBuilder.Entity<ReservedNamespace>()
.HasMany<User>(pr => pr.Owners)
.WithMany(u => u.ReservedNamespaces)
.Map(c => c.ToTable("ReservedNamespaceOwners")
.MapLeftKey("ReservedNamespaceKey")
.MapRightKey("UserKey"));

modelBuilder.Entity<UserSecurityPolicy>()
.HasKey(p => p.Key);

Expand Down
1 change: 1 addition & 0 deletions src/NuGetGallery.Core/Entities/IEntitiesContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public interface IEntitiesContext
IDbSet<Scope> Scopes { get; set; }
IDbSet<User> Users { get; set; }
IDbSet<UserSecurityPolicy> UserSecurityPolicies { get; set; }
IDbSet<ReservedNamespace> ReservedNamespaces { get; set; }

Task<int> SaveChangesAsync();
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1716:IdentifiersShouldNotMatchKeywords", MessageId = "Set", Justification="This is to match the EF terminology.")]
Expand Down
6 changes: 6 additions & 0 deletions src/NuGetGallery.Core/Entities/PackageRegistration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,21 @@ public PackageRegistration()
{
Owners = new HashSet<User>();
Packages = new HashSet<Package>();
ReservedNamespaces = new HashSet<ReservedNamespace>();
}

[StringLength(CoreConstants.MaxPackageIdLength)]
[Required]
public string Id { get; set; }

public int DownloadCount { get; set; }

public bool IsVerified { get; set; }

public virtual ICollection<User> Owners { get; set; }
public virtual ICollection<Package> Packages { get; set; }
public virtual ICollection<ReservedNamespace> ReservedNamespaces { get; set; }

public int Key { get; set; }
}
}
39 changes: 39 additions & 0 deletions src/NuGetGallery.Core/Entities/ReservedNamespace.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;

namespace NuGetGallery
{
public class ReservedNamespace : IEntity
{
public ReservedNamespace()
: this(value: null, isSharedNamespace: false, isPrefix: false)
{
}

public ReservedNamespace(string value, bool isSharedNamespace, bool isPrefix)
{
Value = value;
IsSharedNamespace = isSharedNamespace;
IsPrefix = isPrefix;
PackageRegistrations = new HashSet<PackageRegistration>();
Owners = new HashSet<User>();
}

[StringLength(CoreConstants.MaxPackageIdLength)]
[Required]
public string Value { get; set; }

public bool IsSharedNamespace { get; set; }

public bool IsPrefix { get; set; }

public virtual ICollection<PackageRegistration> PackageRegistrations { get; set; }
public virtual ICollection<User> Owners { get; set; }

[Key]
public int Key { get; set; }
}
}
23 changes: 23 additions & 0 deletions src/NuGetGallery.Core/Entities/SuspendDbExecutionStrategy.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;

namespace NuGetGallery
{
/// <summary>
/// Define the execution strategy for the EntitiesConfiguration for connection resiliency and retries
/// </summary>
public class SuspendDbExecutionStrategy : IDisposable
{
public SuspendDbExecutionStrategy()
{
EntitiesConfiguration.SuspendExecutionStrategy = true;
}

public void Dispose()
{
EntitiesConfiguration.SuspendExecutionStrategy = false;
}
}
}
4 changes: 4 additions & 0 deletions src/NuGetGallery.Core/Entities/User.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public User(string username)
{
Credentials = new List<Credential>();
SecurityPolicies = new List<UserSecurityPolicy>();
ReservedNamespaces = new HashSet<ReservedNamespace>();
Roles = new List<Role>();
Username = username;
}
Expand All @@ -36,8 +37,11 @@ public User(string username)
public string Username { get; set; }

public virtual ICollection<Role> Roles { get; set; }

public bool EmailAllowed { get; set; }

public virtual ICollection<ReservedNamespace> ReservedNamespaces { get; set; }

[DefaultValue(true)]
public bool NotifyPackagePushed { get; set; }

Expand Down
2 changes: 2 additions & 0 deletions src/NuGetGallery.Core/NuGetGallery.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -178,11 +178,13 @@
<Compile Include="Entities\PackageLicense.cs" />
<Compile Include="Entities\PackageLicenseReport.cs" />
<Compile Include="Entities\PackageOwnerRequest.cs" />
<Compile Include="Entities\ReservedNamespace.cs" />
<Compile Include="Entities\PackageRegistration.cs" />
<Compile Include="Entities\PackageType.cs" />
<Compile Include="Entities\ReadOnlyModeException.cs" />
<Compile Include="Entities\Role.cs" />
<Compile Include="Entities\Scope.cs" />
<Compile Include="Entities\SuspendDbExecutionStrategy.cs" />
<Compile Include="Entities\UserSecurityPolicy.cs" />
<Compile Include="Entities\User.cs" />
<Compile Include="ICloudStorageStatusDependency.cs" />
Expand Down
10 changes: 10 additions & 0 deletions src/NuGetGallery/App_Start/DefaultDependenciesModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,11 @@ protected override void Load(ContainerBuilder builder)
.As<IEntityRepository<User>>()
.InstancePerLifetimeScope();

builder.RegisterType<EntityRepository<ReservedNamespace>>()
.AsSelf()
.As<IEntityRepository<ReservedNamespace>>()
.InstancePerLifetimeScope();

builder.RegisterType<EntityRepository<CuratedFeed>>()
.AsSelf()
.As<IEntityRepository<CuratedFeed>>()
Expand Down Expand Up @@ -186,6 +191,11 @@ protected override void Load(ContainerBuilder builder)
.As<ISecurityPolicyService>()
.InstancePerLifetimeScope();

builder.RegisterType<ReservedNamespaceService>()
.AsSelf()
.As<IReservedNamespaceService>()
.InstancePerLifetimeScope();

builder.RegisterType<SecurePushSubscription>()
.SingleInstance();

Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

66 changes: 66 additions & 0 deletions src/NuGetGallery/Migrations/201708241907124_PrefixReservation.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
namespace NuGetGallery.Migrations
{
using System;
using System.Data.Entity.Migrations;

public partial class PrefixReservation : DbMigration
{
public override void Up()
{
CreateTable(
"dbo.ReservedNamespaces",
c => new
{
Key = c.Int(nullable: false, identity: true),
Value = c.String(nullable: false, maxLength: 128),
IsSharedNamespace = c.Boolean(nullable: false),
IsPrefix = c.Boolean(nullable: false),
})
.PrimaryKey(t => t.Key);

CreateTable(
"dbo.ReservedNamespaceOwners",
c => new
{
ReservedNamespaceKey = c.Int(nullable: false),
UserKey = c.Int(nullable: false),
})
.PrimaryKey(t => new { t.ReservedNamespaceKey, t.UserKey })
.ForeignKey("dbo.ReservedNamespaces", t => t.ReservedNamespaceKey, cascadeDelete: true)
.ForeignKey("dbo.Users", t => t.UserKey, cascadeDelete: true)
.Index(t => t.ReservedNamespaceKey)
.Index(t => t.UserKey);

CreateTable(
"dbo.ReservedNamespaceRegistrations",
c => new
{
ReservedNamespaceKey = c.Int(nullable: false),
PackageRegistrationKey = c.Int(nullable: false),
})
.PrimaryKey(t => new { t.ReservedNamespaceKey, t.PackageRegistrationKey })
.ForeignKey("dbo.ReservedNamespaces", t => t.ReservedNamespaceKey, cascadeDelete: true)
.ForeignKey("dbo.PackageRegistrations", t => t.PackageRegistrationKey, cascadeDelete: true)
.Index(t => t.ReservedNamespaceKey)
.Index(t => t.PackageRegistrationKey);

AddColumn("dbo.PackageRegistrations", "IsVerified", c => c.Boolean(nullable: false));
}

public override void Down()
{
DropForeignKey("dbo.ReservedNamespaceRegistrations", "PackageRegistrationKey", "dbo.PackageRegistrations");
DropForeignKey("dbo.ReservedNamespaceRegistrations", "ReservedNamespaceKey", "dbo.ReservedNamespaces");
DropForeignKey("dbo.ReservedNamespaceOwners", "UserKey", "dbo.Users");
DropForeignKey("dbo.ReservedNamespaceOwners", "ReservedNamespaceKey", "dbo.ReservedNamespaces");
DropIndex("dbo.ReservedNamespaceRegistrations", new[] { "PackageRegistrationKey" });
DropIndex("dbo.ReservedNamespaceRegistrations", new[] { "ReservedNamespaceKey" });
DropIndex("dbo.ReservedNamespaceOwners", new[] { "UserKey" });
DropIndex("dbo.ReservedNamespaceOwners", new[] { "ReservedNamespaceKey" });
DropColumn("dbo.PackageRegistrations", "IsVerified");
DropTable("dbo.ReservedNamespaceRegistrations");
DropTable("dbo.ReservedNamespaceOwners");
DropTable("dbo.ReservedNamespaces");
}
}
}
Loading

0 comments on commit 450fc6c

Please sign in to comment.