From 757927e02a9545f240f0e0020773dba1c799aa2a Mon Sep 17 00:00:00 2001 From: Matt Gibson Date: Wed, 1 Dec 2021 14:34:56 -0600 Subject: [PATCH] Null org, org user ids, and friendly name to indicate invalid, unused sponsorship state (#1738) * Null org, org user ids, and friendly name to indicate invalid, unused sponsorship state * Match EF queries to MSSQL sprocs --- .../EntityFramework/OrganizationRepository.cs | 1 + .../OrganizationUserRepository.cs | 2 + ...NullOrganizationSponsorshipOnOrgDelete.sql | 80 +++++++++++++++++++ 3 files changed, 83 insertions(+) create mode 100644 util/Migrator/DbScripts/2021-11-30_00_NullOrganizationSponsorshipOnOrgDelete.sql diff --git a/src/Core/Repositories/EntityFramework/OrganizationRepository.cs b/src/Core/Repositories/EntityFramework/OrganizationRepository.cs index 76f86fa1fd..31f31269f2 100644 --- a/src/Core/Repositories/EntityFramework/OrganizationRepository.cs +++ b/src/Core/Repositories/EntityFramework/OrganizationRepository.cs @@ -113,6 +113,7 @@ namespace Bit.Core.Repositories.EntityFramework { sponsorship.SponsoredOrganizationId = UpdatedOrgId(sponsorship.SponsoredOrganizationId); sponsorship.SponsoringOrganizationId = UpdatedOrgId(sponsorship.SponsoringOrganizationId); + sponsorship.FriendlyName = null; } dbContext.Remove(orgEntity); diff --git a/src/Core/Repositories/EntityFramework/OrganizationUserRepository.cs b/src/Core/Repositories/EntityFramework/OrganizationUserRepository.cs index 7e991994d7..0c6f495075 100644 --- a/src/Core/Repositories/EntityFramework/OrganizationUserRepository.cs +++ b/src/Core/Repositories/EntityFramework/OrganizationUserRepository.cs @@ -80,6 +80,7 @@ namespace Bit.Core.Repositories.EntityFramework foreach (var sponsorship in sponsorships) { sponsorship.SponsoringOrganizationUserId = null; + sponsorship.FriendlyName = null; } dbContext.Remove(orgUser); @@ -99,6 +100,7 @@ namespace Bit.Core.Repositories.EntityFramework foreach (var sponsorship in sponsorships) { sponsorship.SponsoringOrganizationUserId = null; + sponsorship.FriendlyName = null; } dbContext.RemoveRange(entities); diff --git a/util/Migrator/DbScripts/2021-11-30_00_NullOrganizationSponsorshipOnOrgDelete.sql b/util/Migrator/DbScripts/2021-11-30_00_NullOrganizationSponsorshipOnOrgDelete.sql new file mode 100644 index 0000000000..67d152bb3b --- /dev/null +++ b/util/Migrator/DbScripts/2021-11-30_00_NullOrganizationSponsorshipOnOrgDelete.sql @@ -0,0 +1,80 @@ +-- OrganizationSponsorship_OrganizationDeleted +IF OBJECT_ID('[dbo].[OrganizationSponsorship_OrganizationDeleted]') IS NOT NULL +BEGIN + DROP PROCEDURE [dbo].[OrganizationSponsorship_OrganizationDeleted] +END +GO + +CREATE PROCEDURE [dbo].[OrganizationSponsorship_OrganizationDeleted] + @OrganizationId UNIQUEIDENTIFIER +AS +BEGIN + SET NOCOUNT ON + + UPDATE + [dbo].[OrganizationSponsorship] + SET + [SponsoringOrganizationId] = NULL, + [FriendlyName] = NULL + WHERE + [SponsoringOrganizationId] = @OrganizationId + + UPDATE + [dbo].[OrganizationSponsorship] + SET + [SponsoredOrganizationId] = NULL, + [FriendlyName] = NULL + WHERE + [SponsoredOrganizationId] = @OrganizationId +END +GO + +-- OrganizationSponsorship_OrganizationUserDeleted +IF OBJECT_ID('[dbo].[OrganizationSponsorship_OrganizationUserDeleted]') IS NOT NULL +BEGIN + DROP PROCEDURE [dbo].[OrganizationSponsorship_OrganizationUserDeleted] +END +GO + +CREATE PROCEDURE [dbo].[OrganizationSponsorship_OrganizationUserDeleted] + @OrganizationUserId UNIQUEIDENTIFIER +AS +BEGIN + SET NOCOUNT ON + + UPDATE + OS + SET + [SponsoringOrganizationUserId] = NULL, + [FriendlyName] = NULL + FROM + [dbo].[OrganizationSponsorship] OS + WHERE + [SponsoringOrganizationUserId] = @OrganizationUserId +END +GO + +-- OrganizationSponsorship_OrganizationUsersDeleted +IF OBJECT_ID('[dbo].[OrganizationSponsorship_OrganizationUsersDeleted]') IS NOT NULL +BEGIN + DROP PROCEDURE [dbo].[OrganizationSponsorship_OrganizationUsersDeleted] +END +GO + +CREATE PROCEDURE [dbo].[OrganizationSponsorship_OrganizationUsersDeleted] + @SponsoringOrganizationUserIds [dbo].[GuidIdArray] READONLY +AS +BEGIN + SET NOCOUNT ON + + UPDATE + OS + SET + [SponsoringOrganizationUserId] = NULL, + [FriendlyName] = NULL + FROM + [dbo].[OrganizationSponsorship] OS + INNER JOIN + @SponsoringOrganizationUserIds I ON I.Id = OS.SponsoringOrganizationUserId +END +GO