From b97a1a9ed2dd5a84cd87d018634fe65da736b01f Mon Sep 17 00:00:00 2001 From: Matt Bishop Date: Tue, 16 Jan 2024 09:08:55 -0500 Subject: [PATCH] [PM-5519] [PM-5526] [PM-5624] [PM-5600] More Grant SQL fixes (#3668) * SQLite scripts to apply autoincrementing Id key * Drop erroneous Id column if created --- .../GrantEntityTypeConfiguration.cs | 1 + .../20231214162533_GrantIdWithIndexes.cs | 17 +++- .../2023-12-04_00_Down_GrantIndexes.sql | 45 +++++++++ .../2023-12-04_00_Up_GrantIndexes.sql | 46 +++++++++ .../20231214162537_GrantIdWithIndexes.cs | 97 ++----------------- util/SqliteMigrations/SqliteMigrations.csproj | 5 + 6 files changed, 119 insertions(+), 92 deletions(-) create mode 100644 util/SqliteMigrations/HelperScripts/2023-12-04_00_Down_GrantIndexes.sql create mode 100644 util/SqliteMigrations/HelperScripts/2023-12-04_00_Up_GrantIndexes.sql diff --git a/src/Infrastructure.EntityFramework/Auth/Configurations/GrantEntityTypeConfiguration.cs b/src/Infrastructure.EntityFramework/Auth/Configurations/GrantEntityTypeConfiguration.cs index 599be37a84..77d8d1eb9f 100644 --- a/src/Infrastructure.EntityFramework/Auth/Configurations/GrantEntityTypeConfiguration.cs +++ b/src/Infrastructure.EntityFramework/Auth/Configurations/GrantEntityTypeConfiguration.cs @@ -10,6 +10,7 @@ public class GrantEntityTypeConfiguration : IEntityTypeConfiguration { builder .HasKey(s => s.Id) + .HasName("PK_Grant") .IsClustered(); builder diff --git a/util/MySqlMigrations/Migrations/20231214162533_GrantIdWithIndexes.cs b/util/MySqlMigrations/Migrations/20231214162533_GrantIdWithIndexes.cs index c9fd5638b0..1e4c178ade 100644 --- a/util/MySqlMigrations/Migrations/20231214162533_GrantIdWithIndexes.cs +++ b/util/MySqlMigrations/Migrations/20231214162533_GrantIdWithIndexes.cs @@ -72,7 +72,22 @@ public partial class GrantIdWithIndexes : Migration .Annotation("MySql:CharSet", "utf8mb4") .OldAnnotation("MySql:CharSet", "utf8mb4"); - migrationBuilder.Sql("ALTER TABLE `Grant` ADD COLUMN `Id` INT AUTO_INCREMENT UNIQUE;"); + migrationBuilder.Sql(@" + DROP PROCEDURE IF EXISTS GrantSchemaChange; + + CREATE PROCEDURE GrantSchemaChange() + BEGIN + IF EXISTS (SELECT 1 FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'Grant' AND COLUMN_NAME = 'Id') THEN + ALTER TABLE `Grant` DROP COLUMN `Id`; + END IF; + + ALTER TABLE `Grant` ADD COLUMN `Id` INT AUTO_INCREMENT UNIQUE; + END; + + CALL GrantSchemaChange(); + + DROP PROCEDURE GrantSchemaChange;" + ); migrationBuilder.AddPrimaryKey( name: "PK_Grant", diff --git a/util/SqliteMigrations/HelperScripts/2023-12-04_00_Down_GrantIndexes.sql b/util/SqliteMigrations/HelperScripts/2023-12-04_00_Down_GrantIndexes.sql new file mode 100644 index 0000000000..5ea59ef753 --- /dev/null +++ b/util/SqliteMigrations/HelperScripts/2023-12-04_00_Down_GrantIndexes.sql @@ -0,0 +1,45 @@ +ALTER TABLE +"Grant" RENAME TO "Old_Grant"; + +CREATE TABLE "Grant" +( + "Key" TEXT NOT NULL CONSTRAINT "PK_Grant" PRIMARY KEY, + "Type" TEXT NULL, + "SubjectId" TEXT NULL, + "SessionId" TEXT NULL, + "ClientId" TEXT NULL, + "Description" TEXT NULL, + "CreationDate" TEXT NOT NULL, + "ExpirationDate" TEXT NULL, + "ConsumedDate" TEXT NULL, + "Data" TEXT NULL +); + +INSERT INTO +"Grant" + ( + "Key", + "Type", + "SubjectId", + "SessionId", + "ClientId", + "Description", + "CreationDate", + "ExpirationDate", + "ConsumedDate", + "Data" + ) +SELECT + "Key", + "Type", + "SubjectId", + "SessionId", + "ClientId", + "Description", + "CreationDate", + "ExpirationDate", + "ConsumedDate", + "Data" +FROM "Old_Grant"; + +DROP TABLE "Old_Grant"; diff --git a/util/SqliteMigrations/HelperScripts/2023-12-04_00_Up_GrantIndexes.sql b/util/SqliteMigrations/HelperScripts/2023-12-04_00_Up_GrantIndexes.sql new file mode 100644 index 0000000000..028214df67 --- /dev/null +++ b/util/SqliteMigrations/HelperScripts/2023-12-04_00_Up_GrantIndexes.sql @@ -0,0 +1,46 @@ +ALTER TABLE +"Grant" RENAME TO "Old_Grant"; + +CREATE TABLE "Grant" +( + "Id" INTEGER PRIMARY KEY AUTOINCREMENT, + "Key" TEXT NOT NULL, + "Type" TEXT NOT NULL, + "SubjectId" TEXT NULL, + "SessionId" TEXT NULL, + "ClientId" TEXT NOT NULL, + "Description" TEXT NULL, + "CreationDate" TEXT NOT NULL, + "ExpirationDate" TEXT NULL, + "ConsumedDate" TEXT NULL, + "Data" TEXT NOT NULL +); + +INSERT INTO +"Grant" + ( + "Key", + "Type", + "SubjectId", + "SessionId", + "ClientId", + "Description", + "CreationDate", + "ExpirationDate", + "ConsumedDate", + "Data" + ) +SELECT + "Key", + "Type", + "SubjectId", + "SessionId", + "ClientId", + "Description", + "CreationDate", + "ExpirationDate", + "ConsumedDate", + "Data" +FROM "Old_Grant"; + +DROP TABLE "Old_Grant"; diff --git a/util/SqliteMigrations/Migrations/20231214162537_GrantIdWithIndexes.cs b/util/SqliteMigrations/Migrations/20231214162537_GrantIdWithIndexes.cs index 1409fd055d..74f8d9b608 100644 --- a/util/SqliteMigrations/Migrations/20231214162537_GrantIdWithIndexes.cs +++ b/util/SqliteMigrations/Migrations/20231214162537_GrantIdWithIndexes.cs @@ -1,4 +1,5 @@ -using Microsoft.EntityFrameworkCore.Migrations; +using Bit.EfShared; +using Microsoft.EntityFrameworkCore.Migrations; #nullable disable @@ -7,59 +8,12 @@ namespace Bit.SqliteMigrations.Migrations; /// public partial class GrantIdWithIndexes : Migration { + private const string _scriptLocationTemplate = "2023-12-04_00_{0}_GrantIndexes.sql"; + /// protected override void Up(MigrationBuilder migrationBuilder) { - migrationBuilder.DropPrimaryKey( - name: "PK_Grant", - table: "Grant"); - - migrationBuilder.AlterColumn( - name: "Type", - table: "Grant", - type: "TEXT", - maxLength: 50, - nullable: false, - defaultValue: "", - oldClrType: typeof(string), - oldType: "TEXT", - oldMaxLength: 50, - oldNullable: true); - - migrationBuilder.AlterColumn( - name: "Data", - table: "Grant", - type: "TEXT", - nullable: false, - defaultValue: "", - oldClrType: typeof(string), - oldType: "TEXT", - oldNullable: true); - - migrationBuilder.AlterColumn( - name: "ClientId", - table: "Grant", - type: "TEXT", - maxLength: 200, - nullable: false, - defaultValue: "", - oldClrType: typeof(string), - oldType: "TEXT", - oldMaxLength: 200, - oldNullable: true); - - migrationBuilder.AddColumn( - name: "Id", - table: "Grant", - type: "INTEGER", - nullable: false, - defaultValue: 0) - .Annotation("Sqlite:Autoincrement", true); - - migrationBuilder.AddPrimaryKey( - name: "PK_Grant", - table: "Grant", - column: "Id"); + migrationBuilder.SqlResource(_scriptLocationTemplate); migrationBuilder.CreateIndex( name: "IX_Grant_Key", @@ -71,49 +25,10 @@ public partial class GrantIdWithIndexes : Migration /// protected override void Down(MigrationBuilder migrationBuilder) { - migrationBuilder.DropPrimaryKey( - name: "PK_Grant", - table: "Grant"); + migrationBuilder.SqlResource(_scriptLocationTemplate); migrationBuilder.DropIndex( name: "IX_Grant_Key", table: "Grant"); - - migrationBuilder.DropColumn( - name: "Id", - table: "Grant"); - - migrationBuilder.AlterColumn( - name: "Type", - table: "Grant", - type: "TEXT", - maxLength: 50, - nullable: true, - oldClrType: typeof(string), - oldType: "TEXT", - oldMaxLength: 50); - - migrationBuilder.AlterColumn( - name: "Data", - table: "Grant", - type: "TEXT", - nullable: true, - oldClrType: typeof(string), - oldType: "TEXT"); - - migrationBuilder.AlterColumn( - name: "ClientId", - table: "Grant", - type: "TEXT", - maxLength: 200, - nullable: true, - oldClrType: typeof(string), - oldType: "TEXT", - oldMaxLength: 200); - - migrationBuilder.AddPrimaryKey( - name: "PK_Grant", - table: "Grant", - column: "Key"); } } diff --git a/util/SqliteMigrations/SqliteMigrations.csproj b/util/SqliteMigrations/SqliteMigrations.csproj index 6973cdee90..ba322375c4 100644 --- a/util/SqliteMigrations/SqliteMigrations.csproj +++ b/util/SqliteMigrations/SqliteMigrations.csproj @@ -22,4 +22,9 @@ + + + + +