Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename DbScripts_future and DbScripts_data_migrations #3192

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions util/Migrator/DbScripts_finalization/2023-06-FutureMigration.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
-- Remove Column
IF COL_LENGTH('[dbo].[ApiKey]', 'ClientSecret') IS NOT NULL
BEGIN
ALTER TABLE
[dbo].[ApiKey]
DROP COLUMN
[ClientSecret]
END
GO

-- Refresh views
IF OBJECT_ID('[dbo].[ApiKeyDetailsView]') IS NOT NULL
BEGIN
EXECUTE sp_refreshview N'[dbo].[ApiKeyDetailsView]';
END
GO

IF OBJECT_ID('[dbo].[ApiKeyView]') IS NOT NULL
BEGIN
EXECUTE sp_refreshview N'[dbo].[ApiKeyView]';
END
GO

CREATE OR ALTER PROCEDURE [dbo].[ApiKey_Create]
@Id UNIQUEIDENTIFIER OUTPUT,
@ServiceAccountId UNIQUEIDENTIFIER,
@Name VARCHAR(200),
@ClientSecretHash VARCHAR(128),
@Scope NVARCHAR(4000),
@EncryptedPayload NVARCHAR(4000),
@Key VARCHAR(MAX),
@ExpireAt DATETIME2(7),
@CreationDate DATETIME2(7),
@RevisionDate DATETIME2(7)
AS
BEGIN
SET NOCOUNT ON

INSERT INTO [dbo].[ApiKey]
(
[Id],
[ServiceAccountId],
[Name],
[ClientSecretHash],
[Scope],
[EncryptedPayload],
[Key],
[ExpireAt],
[CreationDate],
[RevisionDate]
)
VALUES
(
@Id,
@ServiceAccountId,
@Name,
@ClientSecretHash,
@Scope,
@EncryptedPayload,
@Key,
@ExpireAt,
@CreationDate,
@RevisionDate
)
END
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
This is the data migration script for the client secret hash updates.
The initial migration util/Migrator/DbScripts/2023-05-16_00_ClientSecretHash.sql should be run prior.
The final migration is in util/Migrator/DbScripts_future/2023-06-FutureMigration.sql.
*/
IF COL_LENGTH('[dbo].[ApiKey]', 'ClientSecretHash') IS NOT NULL AND COL_LENGTH('[dbo].[ApiKey]', 'ClientSecret') IS NOT NULL
BEGIN

-- Add index
IF NOT EXISTS(SELECT name FROM sys.indexes WHERE name = 'IX_ApiKey_ClientSecretHash')
BEGIN
CREATE NONCLUSTERED INDEX [IX_ApiKey_ClientSecretHash]
ON [dbo].[ApiKey]([ClientSecretHash] ASC)
WITH (ONLINE = ON)
END

-- Data Migration
DECLARE @BatchSize INT = 10000
WHILE @BatchSize > 0
BEGIN
BEGIN TRANSACTION Migrate_ClientSecretHash

UPDATE TOP(@BatchSize) [dbo].[ApiKey]
SET ClientSecretHash = (
SELECT CAST(N'' AS XML).value('xs:base64Binary(sql:column("HASH"))', 'VARCHAR(128)')
FROM (
SELECT HASHBYTES('SHA2_256', [ClientSecret]) AS HASH
) SRC
)
WHERE [ClientSecretHash] IS NULL

SET @BatchSize = @@ROWCOUNT

COMMIT TRANSACTION Migrate_ClientSecretHash
END

-- Drop index
DROP INDEX IF EXISTS [IX_ApiKey_ClientSecretHash]
ON [dbo].[ApiKey];

END
GO
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
-- Reseller Providers were being created with a NULL value in the [Name] column.
-- This script will populate them with the value from [BusinessName] which was already required.
UPDATE [dbo].[Provider]
SET [Name] = [BusinessName]
WHERE [Name] IS NULL
Loading