Skip to content

Commit

Permalink
Merge pull request #793 from DuendeSoftware/brock/ef-grants-pk
Browse files Browse the repository at this point in the history
add identity column to persisted grants table
  • Loading branch information
brockallen authored Mar 9, 2022
2 parents 5d64bcf + 54e6b9e commit 3584414
Show file tree
Hide file tree
Showing 9 changed files with 55 additions and 20 deletions.
2 changes: 1 addition & 1 deletion migrations/IdentityServerDb/Migrations/ConfigurationDb.sql
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ CREATE UNIQUE INDEX [IX_IdentityResources_Name] ON [IdentityResources] ([Name]);
GO

INSERT INTO [__EFMigrationsHistory] ([MigrationId], [ProductVersion])
VALUES (N'20220217183132_Configuration', N'6.0.0');
VALUES (N'20220309202515_Configuration', N'6.0.0');
GO

COMMIT;
Expand Down

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

10 changes: 7 additions & 3 deletions migrations/IdentityServerDb/Migrations/PersistedGrantDb.sql
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ CREATE TABLE [Keys] (
GO

CREATE TABLE [PersistedGrants] (
[Key] nvarchar(200) NOT NULL,
[Id] int NOT NULL IDENTITY,
[Key] nvarchar(200) NULL,
[Type] nvarchar(50) NOT NULL,
[SubjectId] nvarchar(200) NULL,
[SessionId] nvarchar(100) NULL,
Expand All @@ -49,7 +50,7 @@ CREATE TABLE [PersistedGrants] (
[Expiration] datetime2 NULL,
[ConsumedTime] datetime2 NULL,
[Data] nvarchar(max) NOT NULL,
CONSTRAINT [PK_PersistedGrants] PRIMARY KEY ([Key])
CONSTRAINT [PK_PersistedGrants] PRIMARY KEY ([Id])
);
GO

Expand Down Expand Up @@ -83,6 +84,9 @@ GO
CREATE INDEX [IX_PersistedGrants_Expiration] ON [PersistedGrants] ([Expiration]);
GO

CREATE UNIQUE INDEX [IX_PersistedGrants_Key] ON [PersistedGrants] ([Key]) WHERE [Key] IS NOT NULL;
GO

CREATE INDEX [IX_PersistedGrants_SubjectId_ClientId_Type] ON [PersistedGrants] ([SubjectId], [ClientId], [Type]);
GO

Expand All @@ -93,7 +97,7 @@ CREATE UNIQUE INDEX [IX_ServerSideSessions_Key] ON [ServerSideSessions] ([Key]);
GO

INSERT INTO [__EFMigrationsHistory] ([MigrationId], [ProductVersion])
VALUES (N'20220217183126_Grants', N'6.0.0');
VALUES (N'20220309202509_Grants', N'6.0.0');
GO

COMMIT;
Expand Down

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

Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ protected override void Up(MigrationBuilder migrationBuilder)
name: "PersistedGrants",
columns: table => new
{
Key = table.Column<string>(type: "nvarchar(200)", maxLength: 200, nullable: false),
Id = table.Column<int>(type: "int", nullable: false)
.Annotation("SqlServer:Identity", "1, 1"),
Key = table.Column<string>(type: "nvarchar(200)", maxLength: 200, nullable: true),
Type = table.Column<string>(type: "nvarchar(50)", maxLength: 50, nullable: false),
SubjectId = table.Column<string>(type: "nvarchar(200)", maxLength: 200, nullable: true),
SessionId = table.Column<string>(type: "nvarchar(100)", maxLength: 100, nullable: true),
Expand All @@ -63,7 +65,7 @@ protected override void Up(MigrationBuilder migrationBuilder)
},
constraints: table =>
{
table.PrimaryKey("PK_PersistedGrants", x => x.Key);
table.PrimaryKey("PK_PersistedGrants", x => x.Id);
});

migrationBuilder.CreateTable(
Expand Down Expand Up @@ -113,6 +115,13 @@ protected override void Up(MigrationBuilder migrationBuilder)
table: "PersistedGrants",
column: "Expiration");

migrationBuilder.CreateIndex(
name: "IX_PersistedGrants_Key",
table: "PersistedGrants",
column: "Key",
unique: true,
filter: "[Key] IS NOT NULL");

migrationBuilder.CreateIndex(
name: "IX_PersistedGrants_SubjectId_ClientId_Type",
table: "PersistedGrants",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,11 @@ protected override void BuildModel(ModelBuilder modelBuilder)

modelBuilder.Entity("Duende.IdentityServer.EntityFramework.Entities.PersistedGrant", b =>
{
b.Property<string>("Key")
.HasMaxLength(200)
.HasColumnType("nvarchar(200)");
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"), 1L, 1);
b.Property<string>("ClientId")
.IsRequired()
Expand All @@ -137,6 +139,10 @@ protected override void BuildModel(ModelBuilder modelBuilder)
b.Property<DateTime?>("Expiration")
.HasColumnType("datetime2");
b.Property<string>("Key")
.HasMaxLength(200)
.HasColumnType("nvarchar(200)");
b.Property<string>("SessionId")
.HasMaxLength(100)
.HasColumnType("nvarchar(100)");
Expand All @@ -150,12 +156,16 @@ protected override void BuildModel(ModelBuilder modelBuilder)
.HasMaxLength(50)
.HasColumnType("nvarchar(50)");
b.HasKey("Key");
b.HasKey("Id");
b.HasIndex("ConsumedTime");
b.HasIndex("Expiration");
b.HasIndex("Key")
.IsUnique()
.HasFilter("[Key] IS NOT NULL");
b.HasIndex("SubjectId", "ClientId", "Type");
b.HasIndex("SubjectId", "SessionId", "Type");
Expand Down
3 changes: 2 additions & 1 deletion src/EntityFramework.Storage/Entities/PersistedGrant.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) Duende Software. All rights reserved.
// Copyright (c) Duende Software. All rights reserved.
// See LICENSE in the project root for license information.


Expand All @@ -10,6 +10,7 @@ namespace Duende.IdentityServer.EntityFramework.Entities;

public class PersistedGrant
{
public int Id { get; set; }
public string Key { get; set; }
public string Type { get; set; }
public string SubjectId { get; set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ public static void ConfigurePersistedGrantContext(this ModelBuilder modelBuilder
{
grant.ToTable(storeOptions.PersistedGrants);
grant.Property(x => x.Key).HasMaxLength(200).ValueGeneratedNever();
grant.Property(x => x.Key).HasMaxLength(200);
grant.Property(x => x.Type).HasMaxLength(50).IsRequired();
grant.Property(x => x.SubjectId).HasMaxLength(200);
grant.Property(x => x.SessionId).HasMaxLength(100);
Expand All @@ -159,8 +159,9 @@ public static void ConfigurePersistedGrantContext(this ModelBuilder modelBuilder
// apparently anything over 4K converts to nvarchar(max) on SqlServer
grant.Property(x => x.Data).HasMaxLength(50000).IsRequired();
grant.HasKey(x => x.Key);
grant.HasKey(x => x.Id);
grant.HasIndex(x => x.Key).IsUnique();
grant.HasIndex(x => new { x.SubjectId, x.ClientId, x.Type });
grant.HasIndex(x => new { x.SubjectId, x.SessionId, x.Type });
grant.HasIndex(x => x.Expiration);
Expand Down

0 comments on commit 3584414

Please sign in to comment.