1
0
mirror of https://github.com/bitwarden/server.git synced 2024-11-22 12:15:36 +01:00

sql tuning and migrations

This commit is contained in:
Kyle Spearrin 2017-04-13 13:16:15 -04:00
parent bf18a5905d
commit 5a24b6624d
10 changed files with 83 additions and 13 deletions

View File

@ -69,6 +69,7 @@
<ItemGroup>
<Build Include="dbo\Tables\Device.sql" />
<Build Include="dbo\Tables\Cipher.sql" />
<Build Include="dbo\Tables\Organization.sql" />
<Build Include="dbo\Tables\FolderCipher.sql" />
<Build Include="dbo\Tables\Grant.sql" />
<Build Include="dbo\Tables\Favorite.sql" />
@ -76,7 +77,6 @@
<Build Include="dbo\Tables\User.sql" />
<Build Include="dbo\Tables\GroupUser.sql" />
<Build Include="dbo\Tables\Folder.sql" />
<Build Include="dbo\Tables\Organization.sql" />
<Build Include="dbo\Tables\OrganizationUser.sql" />
<Build Include="dbo\Tables\Subvault.sql" />
<Build Include="dbo\Tables\SubvaultCipher.sql" />
@ -84,10 +84,10 @@
<Build Include="dbo\Tables\SubvaultUser.sql" />
<Build Include="dbo\Views\SubvaultUserView.sql" />
<Build Include="dbo\Views\DeviceView.sql" />
<Build Include="dbo\Views\OrganizationView.sql" />
<Build Include="dbo\Views\FolderView.sql" />
<Build Include="dbo\Views\CipherView.sql" />
<Build Include="dbo\Views\OrganizationUserView.sql" />
<Build Include="dbo\Views\OrganizationView.sql" />
<Build Include="dbo\Views\UserView.sql" />
<Build Include="dbo\Views\SubvaultUserSubvaultDetailsView.sql" />
<Build Include="dbo\Views\GrantView.sql" />
@ -106,7 +106,9 @@
<Build Include="dbo\Stored Procedures\Cipher_DeleteById.sql" />
<Build Include="dbo\Stored Procedures\Favorite_Create.sql" />
<Build Include="dbo\Stored Procedures\Cipher_ReadById.sql" />
<Build Include="dbo\Stored Procedures\OrganizationUser_ReadCountByOrganizationId.sql" />
<Build Include="dbo\Stored Procedures\Favorite_Delete.sql" />
<Build Include="dbo\Stored Procedures\Subvault_ReadCountByOrganizationId.sql" />
<Build Include="dbo\Stored Procedures\Folder_Create.sql" />
<Build Include="dbo\Stored Procedures\Folder_DeleteById.sql" />
<Build Include="dbo\Stored Procedures\Folder_ReadById.sql" />
@ -155,7 +157,9 @@
<Build Include="dbo\Stored Procedures\SubvaultUser_Create.sql" />
<Build Include="dbo\Stored Procedures\CipherDetails_ReadByIdUserId.sql" />
<Build Include="dbo\Stored Procedures\SubvaultUser_DeleteById.sql" />
<Build Include="dbo\Stored Procedures\OrganizationUser_ReadByOrganizationIdUserId.sql" />
<Build Include="dbo\Stored Procedures\SubvaultUser_ReadById.sql" />
<Build Include="dbo\Stored Procedures\SubvaultCipher_UpdateSubvaults.sql" />
<Build Include="dbo\Stored Procedures\SubvaultUser_Update.sql" />
<Build Include="dbo\Stored Procedures\CipherFullDetails_ReadByIdUserId.sql" />
<Build Include="dbo\Stored Procedures\SubvaultUser_ReadCanEditByCipherIdUserId.sql" />
@ -177,9 +181,5 @@
<Build Include="dbo\Stored Procedures\OrganizationUserUserDetails_ReadById.sql" />
<Build Include="dbo\Stored Procedures\OrganizationUserUserDetails_ReadByOrganizationId.sql" />
<Build Include="dbo\User Defined Types\GuidIdArray.sql" />
<Build Include="dbo\Stored Procedures\OrganizationUser_ReadCountByOrganizationId.sql" />
<Build Include="dbo\Stored Procedures\Subvault_ReadCountByOrganizationId.sql" />
<Build Include="dbo\Stored Procedures\OrganizationUser_ReadByOrganizationIdUserId.sql" />
<Build Include="dbo\Stored Procedures\SubvaultCipher_UpdateSubvaults.sql" />
</ItemGroup>
</Project>

View File

@ -17,6 +17,11 @@ BEGIN
LEFT JOIN
[dbo].[Organization] O ON O.[Id] = C.[OrganizationId]
WHERE
(C.[UserId] IS NOT NULL AND C.[UserId] = @UserId)
OR (OU.[UserId] = @UserId AND OU.[Status] = 2 AND O.[Enabled] = 1) -- 2 = Confirmed
C.[UserId] = @UserId
OR (
C.[UserId] IS NULL
AND OU.[UserId] = @UserId
AND OU.[Status] = 2 -- 2 = Confirmed
AND O.[Enabled] = 1
)
END

View File

@ -48,4 +48,4 @@ BEGIN
@CreationDate,
@RevisionDate
)
END
END

View File

@ -34,4 +34,4 @@ BEGIN
[RevisionDate] = @RevisionDate
WHERE
[Id] = @Id
END
END

View File

@ -35,7 +35,6 @@ BEGIN
WHERE
[Id] = @UserId
END
GO
CREATE TRIGGER [dbo].[Cipher_Updated]
ON [dbo].[Cipher] AFTER UPDATE
@ -55,7 +54,6 @@ BEGIN
WHERE
[Id] = @UserId
END
GO
CREATE TRIGGER [dbo].[Cipher_Deleted]
ON [dbo].[Cipher] AFTER DELETE
@ -74,4 +72,4 @@ BEGIN
[AccountRevisionDate] = GETUTCDATE()
WHERE
[Id] = @UserId
END
END

View File

@ -6,3 +6,8 @@
CONSTRAINT [FK_FolderCipher_Folder] FOREIGN KEY ([FolderId]) REFERENCES [dbo].[Folder] ([Id])
);
GO
CREATE NONCLUSTERED INDEX [IX_FolderCipher_CipherId]
ON [dbo].[FolderCipher]([CipherId] ASC);

View File

@ -6,3 +6,8 @@
CONSTRAINT [FK_SubvaultCipher_Subvault] FOREIGN KEY ([SubvaultId]) REFERENCES [dbo].[Subvault] ([Id]) ON DELETE CASCADE
);
GO
CREATE NONCLUSTERED INDEX [IX_SubvaultCipher_CipherId]
ON [dbo].[SubvaultCipher]([CipherId] ASC);

View File

@ -10,3 +10,8 @@
CONSTRAINT [FK_SubvaultUser_Subvault] FOREIGN KEY ([SubvaultId]) REFERENCES [dbo].[Subvault] ([Id]) ON DELETE CASCADE
);
GO
CREATE NONCLUSTERED INDEX [IX_SubvaultUser_SubvaultId]
ON [dbo].[SubvaultUser]([SubvaultId] ASC);

View File

@ -0,0 +1,33 @@
-- Get rid of history table
DROP TABLE [History]
GO
-- Setup new tables
CREATE TABLE [dbo].[Favorite] (
[UserId] UNIQUEIDENTIFIER NOT NULL,
[CipherId] UNIQUEIDENTIFIER NOT NULL,
CONSTRAINT [PK_Favorite] PRIMARY KEY CLUSTERED ([UserId] ASC, [CipherId] ASC)
)
GO
CREATE TABLE [dbo].[Folder] (
[Id] UNIQUEIDENTIFIER NOT NULL,
[UserId] UNIQUEIDENTIFIER NOT NULL,
[Name] VARCHAR (MAX) NULL,
[CreationDate] DATETIME2 (7) NOT NULL,
[RevisionDate] DATETIME2 (7) NOT NULL,
CONSTRAINT [PK_Folder] PRIMARY KEY CLUSTERED ([Id] ASC)
)
GO
CREATE TABLE [dbo].[FolderCipher] (
[FolderId] UNIQUEIDENTIFIER NOT NULL,
[CipherId] UNIQUEIDENTIFIER NOT NULL,
CONSTRAINT [PK_FolderCipher] PRIMARY KEY CLUSTERED ([FolderId] ASC, [CipherId] ASC)
)
GO

View File

@ -1,3 +1,5 @@
-- Step 1, Run each statement individually
insert into folder
select Id, UserId, JSON_VALUE(Data,'$.Name') AS [Name], CreationDate, RevisionDate
from cipher
@ -12,3 +14,20 @@ insert into favorite
select UserId, [Id]
from cipher
where Favorite = 1
-- Step 2, drop each column
alter table cipher drop constraint [FK_Cipher_Folder]
go
alter table cipher drop column FolderId
go
alter table cipher drop column Favorite
go
-- Step 3, delete old folder ciphers
delete from cipher where [type] = 0