1
0
mirror of https://github.com/bitwarden/server.git synced 2025-05-22 03:54:33 -05:00

[PM-4767] Update Grant_Save procedure (#3641)

* modify grant_save sql script to migration and Auth SQL scripts to not use merge

* Update formatting for sql files

* Fix formatting for sql files

* Format using Prettier

* Rename 2024-01-03_00_FixGrantSave.sql to 2024-02-12_00_FixGrantSave.sql

---------

Co-authored-by: Matt Bishop <mbishop@bitwarden.com>
This commit is contained in:
rkac-bw 2024-02-14 09:48:58 -07:00 committed by GitHub
parent 4830a352e8
commit 744d21ec5e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 97 additions and 63 deletions

View File

@ -1,6 +1,6 @@
CREATE PROCEDURE [dbo].[Grant_Save] CREATE PROCEDURE [dbo].[Grant_Save]
@Key NVARCHAR(200), @Key NVARCHAR(200),
@Type NVARCHAR(50), @Type NVARCHAR(50),
@SubjectId NVARCHAR(200), @SubjectId NVARCHAR(200),
@SessionId NVARCHAR(100), @SessionId NVARCHAR(100),
@ClientId NVARCHAR(200), @ClientId NVARCHAR(200),
@ -13,53 +13,26 @@ AS
BEGIN BEGIN
SET NOCOUNT ON SET NOCOUNT ON
MERGE -- First, try to update the existing row
[dbo].[Grant] AS [Target] UPDATE [dbo].[Grant]
USING SET
( [Type] = @Type,
VALUES [SubjectId] = @SubjectId,
( [SessionId] = @SessionId,
@Key, [ClientId] = @ClientId,
@Type, [Description] = @Description,
@SubjectId, [CreationDate] = @CreationDate,
@SessionId, [ExpirationDate] = @ExpirationDate,
@ClientId, [ConsumedDate] = @ConsumedDate,
@Description, [Data] = @Data
@CreationDate, WHERE
@ExpirationDate, [Key] = @Key
@ConsumedDate,
@Data -- If no row was updated, insert a new one
) IF @@ROWCOUNT = 0
) AS [Source] BEGIN
( INSERT INTO [dbo].[Grant]
[Key], (
[Type],
[SubjectId],
[SessionId],
[ClientId],
[Description],
[CreationDate],
[ExpirationDate],
[ConsumedDate],
[Data]
)
ON
[Target].[Key] = [Source].[Key]
WHEN MATCHED THEN
UPDATE
SET
[Type] = [Source].[Type],
[SubjectId] = [Source].[SubjectId],
[SessionId] = [Source].[SessionId],
[ClientId] = [Source].[ClientId],
[Description] = [Source].[Description],
[CreationDate] = [Source].[CreationDate],
[ExpirationDate] = [Source].[ExpirationDate],
[ConsumedDate] = [Source].[ConsumedDate],
[Data] = [Source].[Data]
WHEN NOT MATCHED THEN
INSERT
(
[Key], [Key],
[Type], [Type],
[SubjectId], [SubjectId],
@ -70,19 +43,19 @@ BEGIN
[ExpirationDate], [ExpirationDate],
[ConsumedDate], [ConsumedDate],
[Data] [Data]
) )
VALUES VALUES
( (
[Source].[Key], @Key,
[Source].[Type], @Type,
[Source].[SubjectId], @SubjectId,
[Source].[SessionId], @SessionId,
[Source].[ClientId], @ClientId,
[Source].[Description], @Description,
[Source].[CreationDate], @CreationDate,
[Source].[ExpirationDate], @ExpirationDate,
[Source].[ConsumedDate], @ConsumedDate,
[Source].[Data] @Data
) )
; END
END END

View File

@ -0,0 +1,61 @@
CREATE OR ALTER PROCEDURE [dbo].[Grant_Save]
@Key NVARCHAR(200),
@Type NVARCHAR(50),
@SubjectId NVARCHAR(200),
@SessionId NVARCHAR(100),
@ClientId NVARCHAR(200),
@Description NVARCHAR(200),
@CreationDate DATETIME2,
@ExpirationDate DATETIME2,
@ConsumedDate DATETIME2,
@Data NVARCHAR(MAX)
AS
BEGIN
SET NOCOUNT ON
-- First, try to update the existing row
UPDATE [dbo].[Grant]
SET
[Type] = @Type,
[SubjectId] = @SubjectId,
[SessionId] = @SessionId,
[ClientId] = @ClientId,
[Description] = @Description,
[CreationDate] = @CreationDate,
[ExpirationDate] = @ExpirationDate,
[ConsumedDate] = @ConsumedDate,
[Data] = @Data
WHERE
[Key] = @Key
-- If no row was updated, insert a new one
IF @@ROWCOUNT = 0
BEGIN
INSERT INTO [dbo].[Grant]
(
[Key],
[Type],
[SubjectId],
[SessionId],
[ClientId],
[Description],
[CreationDate],
[ExpirationDate],
[ConsumedDate],
[Data]
)
VALUES
(
@Key,
@Type,
@SubjectId,
@SessionId,
@ClientId,
@Description,
@CreationDate,
@ExpirationDate,
@ConsumedDate,
@Data
)
END
END