1
0
mirror of https://github.com/bitwarden/server.git synced 2025-04-06 21:48:12 -05:00
bitwarden/util/Setup/DbScripts/2018-02-28_00_LoginUris.sql
2018-03-01 23:40:37 -05:00

66 lines
1.3 KiB
Transact-SQL

/*
{
"Name":"2.xx",
"Uri":"2.yy",
"Username":"2.zz",
"Password":"2.aa"
}
=>
{
"Name":"2.xx",
"Username":"2.zz",
"Password":"2.aa",
"Uris": [
{
"Uri": "2.yy",
"Match": 0
}
]
}
*/
SET NOCOUNT ON
DECLARE @UriPath VARCHAR(50) = '$.Uri'
DECLARE @UrisPath VARCHAR(50) = '$.Uris'
DECLARE @BatchSize INT = 1000
-- Step 1: Add new Uris property with data from Uri
WHILE @BatchSize > 0
BEGIN
UPDATE TOP (@BatchSize)
[Cipher]
SET
[Data] = JSON_MODIFY(
[Data],
@UrisPath,
JSON_QUERY(
'[{"Uri":"' + JSON_VALUE([Data], @UriPath) + '"}]',
'$'
)
)
WHERE
JSON_VALUE([Data], @UriPath) IS NOT NULL
AND JSON_QUERY([Data], @UrisPath) IS NULL
SET @BatchSize = @@ROWCOUNT
RAISERROR('Updated %d ciphers with Uris', 0, 1, @BatchSize) WITH NOWAIT
END
-- Step 2: Remove old Uri Property
SET @BatchSize = 1000
WHILE @BatchSize > 0
BEGIN
UPDATE TOP (@BatchSize)
[Cipher]
SET
[Data] = JSON_MODIFY([Data], @UriPath, NULL)
WHERE
JSON_VALUE([Data], @UriPath) IS NOT NULL
AND JSON_QUERY([Data], @UrisPath) IS NOT NULL
SET @BatchSize = @@ROWCOUNT
RAISERROR('Updated %d ciphers with Uri removal', 0, 1, @BatchSize) WITH NOWAIT
END