1
0
mirror of https://github.com/bitwarden/server.git synced 2025-01-22 21:51:22 +01:00

[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
This commit is contained in:
Matt Bishop 2024-01-16 09:08:55 -05:00 committed by GitHub
parent c12c09897b
commit b97a1a9ed2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 119 additions and 92 deletions

View File

@ -10,6 +10,7 @@ public class GrantEntityTypeConfiguration : IEntityTypeConfiguration<Grant>
{
builder
.HasKey(s => s.Id)
.HasName("PK_Grant")
.IsClustered();
builder

View File

@ -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",

View File

@ -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";

View File

@ -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";

View File

@ -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;
/// <inheritdoc />
public partial class GrantIdWithIndexes : Migration
{
private const string _scriptLocationTemplate = "2023-12-04_00_{0}_GrantIndexes.sql";
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropPrimaryKey(
name: "PK_Grant",
table: "Grant");
migrationBuilder.AlterColumn<string>(
name: "Type",
table: "Grant",
type: "TEXT",
maxLength: 50,
nullable: false,
defaultValue: "",
oldClrType: typeof(string),
oldType: "TEXT",
oldMaxLength: 50,
oldNullable: true);
migrationBuilder.AlterColumn<string>(
name: "Data",
table: "Grant",
type: "TEXT",
nullable: false,
defaultValue: "",
oldClrType: typeof(string),
oldType: "TEXT",
oldNullable: true);
migrationBuilder.AlterColumn<string>(
name: "ClientId",
table: "Grant",
type: "TEXT",
maxLength: 200,
nullable: false,
defaultValue: "",
oldClrType: typeof(string),
oldType: "TEXT",
oldMaxLength: 200,
oldNullable: true);
migrationBuilder.AddColumn<int>(
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
/// <inheritdoc />
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<string>(
name: "Type",
table: "Grant",
type: "TEXT",
maxLength: 50,
nullable: true,
oldClrType: typeof(string),
oldType: "TEXT",
oldMaxLength: 50);
migrationBuilder.AlterColumn<string>(
name: "Data",
table: "Grant",
type: "TEXT",
nullable: true,
oldClrType: typeof(string),
oldType: "TEXT");
migrationBuilder.AlterColumn<string>(
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");
}
}

View File

@ -22,4 +22,9 @@
<Compile Include="..\EfShared\MigrationBuilderExtensions.cs" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="HelperScripts\2023-12-04_00_Up_GrantIndexes.sql" />
<EmbeddedResource Include="HelperScripts\2023-12-04_00_Down_GrantIndexes.sql" />
</ItemGroup>
</Project>