-
Notifications
You must be signed in to change notification settings - Fork 1.6k
PM-33527 Database SQL scripts #7616
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
Open
prograhamming
wants to merge
20
commits into
main
Choose a base branch
from
dirt/pm-33527/db-orphaned-event-logs
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
e2af154
PM-33527 initial commit
prograhamming 29fa4b3
Merge branch 'main' into dirt/pm-33527/db-orphaned-event-logs
prograhamming 403bfb1
PM-33527 applying the database migrations
prograhamming ec9a301
PM-33527 removing the auot generated migrations
prograhamming 480be67
PM-33527 fixing the model issue
prograhamming 8698565
Merge branch 'main' into dirt/pm-33527/db-orphaned-event-logs
prograhamming 1762c06
Merge branch 'main' into dirt/pm-33527/db-orphaned-event-logs
prograhamming a5e0aec
Merge branch 'main' into dirt/pm-33527/db-orphaned-event-logs
prograhamming 32dadb8
Merge branch 'main' into dirt/pm-33527/db-orphaned-event-logs
prograhamming 7819fe4
Merge branch 'main' into dirt/pm-33527/db-orphaned-event-logs
prograhamming 38a462c
PM-33527 addressing pr comments
prograhamming 110a21e
Merge branch 'dirt/pm-33527/db-orphaned-event-logs' of github.com:bitβ¦
prograhamming 7d053c9
Merge branch 'main' into dirt/pm-33527/db-orphaned-event-logs
prograhamming 133458e
PM-33527 removing code
prograhamming 9631a0f
PM-33527 fix CTE syntax error
prograhamming f16b3a2
PM-33527 fixing CTE format
prograhamming d4e1398
PM-33527 fixing creationDate issue
prograhamming 7ee8b24
PM-33527 fix the stored procedure
prograhamming 594c368
PM-33527 fixing all important some minor issues
prograhamming d87822d
PM-33527 fixing integration tests
prograhamming File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| ο»Ώusing Bit.Core.Entities; | ||
| using Bit.Core.Utilities; | ||
|
|
||
| namespace Bit.Core.Dirt.Entities; | ||
|
|
||
| public class OrganizationEventCleanup : ITableObject<Guid> | ||
| { | ||
| public Guid Id { get; set; } | ||
| public Guid OrganizationId { get; set; } | ||
| public DateTime CreationDate { get; set; } = DateTime.UtcNow; | ||
| public DateTime? RevisionDate { get; set; } | ||
| public DateTime? StartDate { get; set; } | ||
| public DateTime? CompletedDate { get; set; } | ||
| public long EventsDeletedCount { get; set; } | ||
| public int FailureCount { get; set; } | ||
| public string? LastError { get; set; } | ||
| public void SetNewId() => Id = CoreHelpers.GenerateComb(); | ||
| } |
12 changes: 12 additions & 0 deletions
12
src/Core/Dirt/Repositories/IOrganizationEventCleanupRepository.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| ο»Ώusing Bit.Core.Dirt.Entities; | ||
|
|
||
| namespace Bit.Core.Dirt.Repositories; | ||
|
|
||
| public interface IOrganizationEventCleanupRepository | ||
| { | ||
| Task CreateAsync(OrganizationEventCleanup cleanup); | ||
| Task<OrganizationEventCleanup?> ClaimNextPendingAsync(); | ||
| Task UpdateProgressAsync(Guid id, long delta); | ||
| Task UpdateErrorAsync(Guid id, string message); | ||
| Task UpdateCompletedAsync(Guid id); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
src/Infrastructure.Dapper/Dirt/Repositories/OrganizationEventCleanupRepository.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| ο»Ώusing System.Data; | ||
| using Bit.Core.Dirt.Entities; | ||
| using Bit.Core.Dirt.Repositories; | ||
| using Bit.Core.Settings; | ||
| using Bit.Infrastructure.Dapper.Repositories; | ||
| using Dapper; | ||
| using Microsoft.Data.SqlClient; | ||
|
|
||
| namespace Bit.Infrastructure.Dapper.Dirt.Repositories; | ||
|
|
||
| public class OrganizationEventCleanupRepository : BaseRepository, IOrganizationEventCleanupRepository | ||
| { | ||
| private const int LeaseDurationMinutes = 10; | ||
| private const int MaxFailureCount = 5; | ||
|
|
||
| public OrganizationEventCleanupRepository(GlobalSettings globalSettings) | ||
| : base(globalSettings.SqlServer.ConnectionString, globalSettings.SqlServer.ReadOnlyConnectionString) | ||
| { } | ||
|
prograhamming marked this conversation as resolved.
|
||
|
|
||
| public async Task CreateAsync(OrganizationEventCleanup cleanup) | ||
| { | ||
| cleanup.SetNewId(); | ||
| using var connection = new SqlConnection(ConnectionString); | ||
| await connection.ExecuteAsync( | ||
| "[dbo].[OrganizationEventCleanup_Create]", | ||
| new { cleanup.Id, cleanup.OrganizationId, cleanup.CreationDate }, | ||
| commandType: CommandType.StoredProcedure); | ||
| } | ||
|
prograhamming marked this conversation as resolved.
|
||
|
|
||
| public async Task<OrganizationEventCleanup?> ClaimNextPendingAsync() | ||
| { | ||
| var now = DateTime.UtcNow; | ||
| using var connection = new SqlConnection(ConnectionString); | ||
| return await connection.QuerySingleOrDefaultAsync<OrganizationEventCleanup>( | ||
| "[dbo].[OrganizationEventCleanup_ClaimNextPending]", | ||
| new { Now = now, StaleLeaseThreshold = now.AddMinutes(-LeaseDurationMinutes), MaxFailureCount }, | ||
| commandType: CommandType.StoredProcedure); | ||
| } | ||
|
|
||
| public async Task UpdateProgressAsync(Guid id, long delta) | ||
| { | ||
| using var connection = new SqlConnection(ConnectionString); | ||
| await connection.ExecuteAsync( | ||
| "[dbo].[OrganizationEventCleanup_UpdateProgress]", | ||
| new { Id = id, Delta = delta, Now = DateTime.UtcNow }, | ||
| commandType: CommandType.StoredProcedure); | ||
| } | ||
|
|
||
| public async Task UpdateErrorAsync(Guid id, string message) | ||
| { | ||
| using var connection = new SqlConnection(ConnectionString); | ||
| await connection.ExecuteAsync( | ||
| "[dbo].[OrganizationEventCleanup_UpdateError]", | ||
| new { Id = id, Message = message, Now = DateTime.UtcNow }, | ||
| commandType: CommandType.StoredProcedure); | ||
| } | ||
|
|
||
| public async Task UpdateCompletedAsync(Guid id) | ||
| { | ||
| using var connection = new SqlConnection(ConnectionString); | ||
| await connection.ExecuteAsync( | ||
| "[dbo].[OrganizationEventCleanup_UpdateCompleted]", | ||
| new { Id = id, Now = DateTime.UtcNow }, | ||
| commandType: CommandType.StoredProcedure); | ||
| } | ||
| } | ||
43 changes: 43 additions & 0 deletions
43
src/Sql/dbo/Dirt/Stored Procedures/OrganizationEventCleanup_ClaimNextPending.sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| CREATE PROCEDURE [dbo].[OrganizationEventCleanup_ClaimNextPending] | ||
| @Now DATETIME2(7), | ||
| @StaleLeaseThreshold DATETIME2(7), | ||
| @MaxFailureCount INT | ||
| AS | ||
| BEGIN | ||
| SET NOCOUNT ON | ||
|
|
||
| ;WITH [Pending] AS ( | ||
| SELECT TOP 1 | ||
| [Id], | ||
| [OrganizationId], | ||
| [CreationDate], | ||
| [RevisionDate], | ||
| [StartDate], | ||
| [CompletedDate], | ||
| [EventsDeletedCount], | ||
| [FailureCount], | ||
| [LastError] | ||
| FROM | ||
| [dbo].[OrganizationEventCleanup] WITH (UPDLOCK, READPAST) | ||
| WHERE | ||
| [CompletedDate] IS NULL | ||
|
prograhamming marked this conversation as resolved.
|
||
| AND ([StartDate] IS NULL OR [RevisionDate] < @StaleLeaseThreshold) | ||
| AND [FailureCount] < @MaxFailureCount | ||
| ORDER BY | ||
| [CreationDate] ASC | ||
| ) | ||
| UPDATE [Pending] | ||
| SET | ||
| [StartDate] = COALESCE([StartDate], @Now), | ||
| [RevisionDate] = @Now | ||
| OUTPUT | ||
| inserted.[Id], | ||
| inserted.[OrganizationId], | ||
| inserted.[CreationDate], | ||
| inserted.[RevisionDate], | ||
| inserted.[StartDate], | ||
| inserted.[CompletedDate], | ||
| inserted.[EventsDeletedCount], | ||
| inserted.[FailureCount], | ||
| inserted.[LastError] | ||
| END | ||
21 changes: 21 additions & 0 deletions
21
src/Sql/dbo/Dirt/Stored Procedures/OrganizationEventCleanup_Create.sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| CREATE PROCEDURE [dbo].[OrganizationEventCleanup_Create] | ||
| @Id UNIQUEIDENTIFIER, | ||
| @OrganizationId UNIQUEIDENTIFIER, | ||
| @CreationDate DATETIME2(7) | ||
| AS | ||
| BEGIN | ||
| SET NOCOUNT ON | ||
|
|
||
| INSERT INTO [dbo].[OrganizationEventCleanup] | ||
| ( | ||
| [Id], | ||
| [OrganizationId], | ||
| [CreationDate] | ||
| ) | ||
| VALUES | ||
| ( | ||
| @Id, | ||
| @OrganizationId, | ||
| @CreationDate | ||
| ) | ||
| END |
15 changes: 15 additions & 0 deletions
15
src/Sql/dbo/Dirt/Stored Procedures/OrganizationEventCleanup_UpdateCompleted.sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| CREATE PROCEDURE [dbo].[OrganizationEventCleanup_UpdateCompleted] | ||
| @Id UNIQUEIDENTIFIER, | ||
| @Now DATETIME2(7) | ||
| AS | ||
| BEGIN | ||
| SET NOCOUNT ON | ||
|
|
||
| UPDATE | ||
| [dbo].[OrganizationEventCleanup] | ||
| SET | ||
| [CompletedDate] = @Now, | ||
| [RevisionDate] = @Now | ||
| WHERE | ||
| [Id] = @Id | ||
| END |
17 changes: 17 additions & 0 deletions
17
src/Sql/dbo/Dirt/Stored Procedures/OrganizationEventCleanup_UpdateError.sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| CREATE PROCEDURE [dbo].[OrganizationEventCleanup_UpdateError] | ||
| @Id UNIQUEIDENTIFIER, | ||
| @Message NVARCHAR(MAX), | ||
| @Now DATETIME2(7) | ||
| AS | ||
| BEGIN | ||
| SET NOCOUNT ON | ||
|
|
||
| UPDATE | ||
| [dbo].[OrganizationEventCleanup] | ||
| SET | ||
| [FailureCount] = [FailureCount] + 1, | ||
| [LastError] = @Message, | ||
| [RevisionDate] = @Now | ||
| WHERE | ||
| [Id] = @Id | ||
| END |
16 changes: 16 additions & 0 deletions
16
src/Sql/dbo/Dirt/Stored Procedures/OrganizationEventCleanup_UpdateProgress.sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| CREATE PROCEDURE [dbo].[OrganizationEventCleanup_UpdateProgress] | ||
| @Id UNIQUEIDENTIFIER, | ||
| @Delta BIGINT, | ||
| @Now DATETIME2(7) | ||
| AS | ||
| BEGIN | ||
| SET NOCOUNT ON | ||
|
|
||
| UPDATE | ||
| [dbo].[OrganizationEventCleanup] | ||
| SET | ||
| [EventsDeletedCount] = [EventsDeletedCount] + @Delta, | ||
| [RevisionDate] = @Now | ||
| WHERE | ||
| [Id] = @Id | ||
| END |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| CREATE TABLE [dbo].[OrganizationEventCleanup] ( | ||
| [Id] UNIQUEIDENTIFIER NOT NULL, | ||
| [OrganizationId] UNIQUEIDENTIFIER NOT NULL, | ||
| [CreationDate] DATETIME2 (7) NOT NULL, | ||
| [RevisionDate] DATETIME2 (7) NULL, | ||
| [StartDate] DATETIME2 (7) NULL, | ||
| [CompletedDate] DATETIME2 (7) NULL, | ||
| [EventsDeletedCount] BIGINT NOT NULL CONSTRAINT [DF_OrganizationEventCleanup_EventsDeletedCount] DEFAULT (0), | ||
| [FailureCount] INT NOT NULL CONSTRAINT [DF_OrganizationEventCleanup_FailureCount] DEFAULT (0), | ||
| [LastError] NVARCHAR(MAX) NULL, | ||
| CONSTRAINT [PK_OrganizationEventCleanup] PRIMARY KEY CLUSTERED ([Id] ASC) | ||
| ); | ||
| GO | ||
|
|
||
| CREATE NONCLUSTERED INDEX [IX_OrganizationEventCleanup_CompletedDate_CreationDate] | ||
| ON [dbo].[OrganizationEventCleanup]([CompletedDate] ASC, [CreationDate] ASC); | ||
| GO |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.