Skip to content

Commit

Permalink
Use a json object as stored procedure input
Browse files Browse the repository at this point in the history
  • Loading branch information
addisonbeck committed May 13, 2024
1 parent a8e53d8 commit cea0589
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 83 deletions.
35 changes: 0 additions & 35 deletions src/Infrastructure.Dapper/Auth/Helpers/AuthRequestHelpers.cs

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
using System.Data;
using System.Text.Json;
using Bit.Core.Auth.Entities;
using Bit.Core.Auth.Models.Data;
using Bit.Core.Repositories;
using Bit.Core.Settings;
using Bit.Infrastructure.Dapper.Auth.Helpers;
using Bit.Infrastructure.Dapper.Repositories;
using Dapper;
using Microsoft.Data.SqlClient;
Expand Down Expand Up @@ -83,12 +83,11 @@ public async Task UpdateManyAsync(IEnumerable<AuthRequest> authRequests)
return;

Check warning on line 83 in src/Infrastructure.Dapper/Auth/Repositories/AuthRequestRepository.cs

View check run for this annotation

Codecov / codecov/patch

src/Infrastructure.Dapper/Auth/Repositories/AuthRequestRepository.cs#L82-L83

Added lines #L82 - L83 were not covered by tests
}

var authRequestsTVP = authRequests.ToDataTable();
using (var connection = new SqlConnection(ConnectionString))
{
var results = await connection.ExecuteAsync(
$"[dbo].[AuthRequest_UpdateMany]",
new { AuthRequestsInput = authRequestsTVP },
new { jsonData = JsonSerializer.Serialize(authRequests) },
commandType: CommandType.StoredProcedure);
}
}

Check warning on line 93 in src/Infrastructure.Dapper/Auth/Repositories/AuthRequestRepository.cs

View check run for this annotation

Codecov / codecov/patch

src/Infrastructure.Dapper/Auth/Repositories/AuthRequestRepository.cs#L86-L93

Added lines #L86 - L93 were not covered by tests
Expand Down
31 changes: 24 additions & 7 deletions src/Sql/dbo/Stored Procedures/AuthRequest_UpdateMany.sql
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
CREATE PROCEDURE [dbo].[AuthRequest_UpdateMany]
@AuthRequestsInput [dbo].[AuthRequestType] READONLY
CREATE OR ALTER PROCEDURE AuthRequest_UpdateMany
@jsonData NVARCHAR(MAX)
AS
BEGIN
SET NOCOUNT ON

UPDATE
AR
UPDATE AR
SET
[Id] = ARI.[Id],
[UserId] = ARI.[UserId],
Expand All @@ -26,5 +23,25 @@ BEGIN
FROM
[dbo].[AuthRequest] AR
INNER JOIN
@AuthRequestsInput ARI ON AR.Id = ARI.Id
OPENJSON(@jsonData)
WITH (
Id INT '$.Id',
UserId INT '$.UserId',
Type NVARCHAR(50) '$.Type',
RequestDeviceIdentifier NVARCHAR(100) '$.RequestDeviceIdentifier',
RequestDeviceType NVARCHAR(50) '$.RequestDeviceType',
RequestIpAddress NVARCHAR(50) '$.RequestIpAddress',
ResponseDeviceId INT '$.ResponseDeviceId',
AccessCode NVARCHAR(50) '$.AccessCode',
PublicKey NVARCHAR(MAX) '$.PublicKey',
Key NVARCHAR(MAX) '$.Key',
MasterPasswordHash NVARCHAR(MAX) '$.MasterPasswordHash',
Approved BIT '$.Approved',
CreationDate DATETIME2 '$.CreationDate',
ResponseDate DATETIME2 '$.ResponseDate',
AuthenticationDate DATETIME2 '$.AuthenticationDate',
OrganizationId INT '$.OrganizationId'
) AS ARI
WHERE
AR.Id = ARI.Id;
END
62 changes: 24 additions & 38 deletions util/Migrator/DbScripts/2024-05-05_00_UpdateManyAuthRequests.sql
Original file line number Diff line number Diff line change
@@ -1,42 +1,8 @@
IF NOT EXISTS (
SELECT
*
FROM
sys.types
WHERE
[Name] = 'AuthRequestType' AND
is_user_defined = 1
)
BEGIN
CREATE TYPE [dbo].[AuthRequestType] AS TABLE(
[Id] UNIQUEIDENTIFIER,
[UserId] UNIQUEIDENTIFIER,
[Type] SMALLINT,
[RequestDeviceIdentifier] NVARCHAR(50),
[RequestDeviceType] SMALLINT,
[RequestIpAddress] VARCHAR(50),
[ResponseDeviceId] UNIQUEIDENTIFIER,
[AccessCode] VARCHAR(25),
[PublicKey] VARCHAR(MAX),
[Key] VARCHAR(MAX),
[MasterPasswordHash] VARCHAR(MAX),
[Approved] BIT,
[CreationDate] DATETIME2 (7),
[ResponseDate] DATETIME2 (7),
[AuthenticationDate] DATETIME2 (7),
[OrganizationId] UNIQUEIDENTIFIER
)
END
GO

CREATE OR ALTER PROCEDURE [dbo].[AuthRequest_UpdateMany]
@AuthRequestsInput [dbo].[AuthRequestType] READONLY
CREATE OR ALTER PROCEDURE AuthRequest_UpdateMany
@jsonData NVARCHAR(MAX)
AS
BEGIN
SET NOCOUNT ON

UPDATE
AR
UPDATE AR
SET
[Id] = ARI.[Id],
[UserId] = ARI.[UserId],
Expand All @@ -57,5 +23,25 @@ BEGIN
FROM
[dbo].[AuthRequest] AR
INNER JOIN
@AuthRequestsInput ARI ON AR.Id = ARI.Id
OPENJSON(@jsonData)
WITH (
Id INT '$.Id',
UserId INT '$.UserId',
Type NVARCHAR(50) '$.Type',
RequestDeviceIdentifier NVARCHAR(100) '$.RequestDeviceIdentifier',
RequestDeviceType NVARCHAR(50) '$.RequestDeviceType',
RequestIpAddress NVARCHAR(50) '$.RequestIpAddress',
ResponseDeviceId INT '$.ResponseDeviceId',
AccessCode NVARCHAR(50) '$.AccessCode',
PublicKey NVARCHAR(MAX) '$.PublicKey',
Key NVARCHAR(MAX) '$.Key',
MasterPasswordHash NVARCHAR(MAX) '$.MasterPasswordHash',
Approved BIT '$.Approved',
CreationDate DATETIME2 '$.CreationDate',
ResponseDate DATETIME2 '$.ResponseDate',
AuthenticationDate DATETIME2 '$.AuthenticationDate',
OrganizationId INT '$.OrganizationId'
) AS ARI
WHERE
AR.Id = ARI.Id;
END

0 comments on commit cea0589

Please sign in to comment.