From db4d7aa60932d867b5c18a42ac3ca567a68dca32 Mon Sep 17 00:00:00 2001 From: Matt Bishop Date: Thu, 11 Jan 2024 16:06:29 -0500 Subject: [PATCH] [PM-5519] [PM-5526] [PM-5624] [PM-5600] Tweak EF settings for MySQL grant auto-increment (#3662) * Tweak EF settings for grant auto-increment * Go back to zero generated default as that doesn't matter * Explicit value generation callout * Go with custom SQL for direct automatic increment * Proper column creation * Lint --- .../Configurations/GrantEntityTypeConfiguration.cs | 4 ++++ src/Sql/Auth/dbo/Tables/Grant.sql | 2 +- .../Migrations/20231214162533_GrantIdWithIndexes.cs | 11 ++--------- .../Migrations/DatabaseContextModelSnapshot.cs | 4 +++- .../Migrations/DatabaseContextModelSnapshot.cs | 3 ++- 5 files changed, 12 insertions(+), 12 deletions(-) diff --git a/src/Infrastructure.EntityFramework/Auth/Configurations/GrantEntityTypeConfiguration.cs b/src/Infrastructure.EntityFramework/Auth/Configurations/GrantEntityTypeConfiguration.cs index 06c5eac0a1..599be37a84 100644 --- a/src/Infrastructure.EntityFramework/Auth/Configurations/GrantEntityTypeConfiguration.cs +++ b/src/Infrastructure.EntityFramework/Auth/Configurations/GrantEntityTypeConfiguration.cs @@ -12,6 +12,10 @@ public class GrantEntityTypeConfiguration : IEntityTypeConfiguration .HasKey(s => s.Id) .IsClustered(); + builder + .Property(s => s.Id) + .UseIdentityColumn(); + builder .HasIndex(s => s.Key) .IsUnique(true); diff --git a/src/Sql/Auth/dbo/Tables/Grant.sql b/src/Sql/Auth/dbo/Tables/Grant.sql index 55d1b9b34a..2036f11e78 100644 --- a/src/Sql/Auth/dbo/Tables/Grant.sql +++ b/src/Sql/Auth/dbo/Tables/Grant.sql @@ -1,6 +1,6 @@ CREATE TABLE [dbo].[Grant] ( - [Id] INT NOT NULL IDENTITY, + [Id] INT NOT NULL IDENTITY(1,1), [Key] NVARCHAR (200) NOT NULL, [Type] NVARCHAR (50) NOT NULL, [SubjectId] NVARCHAR (200) NULL, diff --git a/util/MySqlMigrations/Migrations/20231214162533_GrantIdWithIndexes.cs b/util/MySqlMigrations/Migrations/20231214162533_GrantIdWithIndexes.cs index 6842184118..c9fd5638b0 100644 --- a/util/MySqlMigrations/Migrations/20231214162533_GrantIdWithIndexes.cs +++ b/util/MySqlMigrations/Migrations/20231214162533_GrantIdWithIndexes.cs @@ -1,5 +1,4 @@ -using Microsoft.EntityFrameworkCore.Metadata; -using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Migrations; #nullable disable @@ -73,13 +72,7 @@ public partial class GrantIdWithIndexes : Migration .Annotation("MySql:CharSet", "utf8mb4") .OldAnnotation("MySql:CharSet", "utf8mb4"); - migrationBuilder.AddColumn( - name: "Id", - table: "Grant", - type: "int", - nullable: false, - defaultValue: 0) - .Annotation("MySql:ValueGenerationStrategy", MySqlValueGenerationStrategy.IdentityColumn); + migrationBuilder.Sql("ALTER TABLE `Grant` ADD COLUMN `Id` INT AUTO_INCREMENT UNIQUE;"); migrationBuilder.AddPrimaryKey( name: "PK_Grant", diff --git a/util/MySqlMigrations/Migrations/DatabaseContextModelSnapshot.cs b/util/MySqlMigrations/Migrations/DatabaseContextModelSnapshot.cs index 17af8ed277..93b5f3653f 100644 --- a/util/MySqlMigrations/Migrations/DatabaseContextModelSnapshot.cs +++ b/util/MySqlMigrations/Migrations/DatabaseContextModelSnapshot.cs @@ -3,6 +3,7 @@ using System; using Bit.Infrastructure.EntityFramework.Repositories; using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; using Microsoft.EntityFrameworkCore.Storage.ValueConversion; #nullable disable @@ -488,7 +489,8 @@ namespace Bit.MySqlMigrations.Migrations { b.Property("Id") .ValueGeneratedOnAdd() - .HasColumnType("int"); + .HasColumnType("int") + .HasAnnotation("MySql:ValueGenerationStrategy", MySqlValueGenerationStrategy.IdentityColumn); b.Property("ClientId") .IsRequired() diff --git a/util/SqliteMigrations/Migrations/DatabaseContextModelSnapshot.cs b/util/SqliteMigrations/Migrations/DatabaseContextModelSnapshot.cs index 36004bf718..569f9809a3 100644 --- a/util/SqliteMigrations/Migrations/DatabaseContextModelSnapshot.cs +++ b/util/SqliteMigrations/Migrations/DatabaseContextModelSnapshot.cs @@ -486,7 +486,8 @@ namespace Bit.SqliteMigrations.Migrations { b.Property("Id") .ValueGeneratedOnAdd() - .HasColumnType("INTEGER"); + .HasColumnType("INTEGER") + .HasAnnotation("Sqlite:Autoincrement", true); b.Property("ClientId") .IsRequired()