-
-
Notifications
You must be signed in to change notification settings - Fork 75
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Report WrongExpectedVersion properly in SQL Server scripts (#376)
* Update check_stream * Added a test * Added try-catch to AppendEvents (SQL Server)
- Loading branch information
1 parent
f13666a
commit 9f05a4e
Showing
5 changed files
with
85 additions
and
26 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
56 changes: 35 additions & 21 deletions
56
src/SqlServer/src/Eventuous.SqlServer/Scripts/3_CheckStream.sql
This file contains 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 |
---|---|---|
@@ -1,29 +1,43 @@ | ||
CREATE OR ALTER PROCEDURE __schema__.check_stream | ||
@stream_name NVARCHAR(850), | ||
@expected_version int, | ||
@current_version INT OUTPUT, | ||
@stream_id INT OUTPUT | ||
AS | ||
CREATE OR ALTER PROCEDURE __schema__.check_stream @stream_name NVARCHAR(850), | ||
@expected_version int, | ||
@current_version INT OUTPUT, | ||
@stream_id INT OUTPUT | ||
AS | ||
BEGIN | ||
DECLARE @customErrorMessage NVARCHAR(200) | ||
|
||
SELECT @current_version = [Version], @stream_id =StreamId | ||
FROM __schema__.Streams | ||
SELECT @current_version = [Version], @stream_id = StreamId | ||
FROM [__schema__].Streams | ||
WHERE StreamName = @stream_name | ||
|
||
IF @stream_id is null | ||
BEGIN | ||
IF @expected_version = -2 -- Any | ||
OR @expected_version = -1 -- NoStream | ||
IF @stream_id is null | ||
BEGIN | ||
INSERT INTO __schema__.Streams (StreamName, Version) VALUES (@stream_name, -1); | ||
SELECT @current_version = Version, @stream_id = StreamId | ||
FROM __schema__.Streams | ||
WHERE StreamName = @stream_name | ||
IF @expected_version = -2 -- Any | ||
OR @expected_version = -1 -- NoStream | ||
BEGIN | ||
BEGIN TRY | ||
INSERT INTO [__schema__].Streams (StreamName, Version) VALUES (@stream_name, -1); | ||
SELECT @current_version = Version, @stream_id = StreamId | ||
FROM [__schema__].Streams | ||
WHERE StreamName = @stream_name | ||
END TRY | ||
BEGIN CATCH | ||
IF (ERROR_NUMBER() = 2627 OR ERROR_NUMBER() = 2601) AND (SELECT CHARINDEX(N'UQ_StreamName', ERROR_MESSAGE())) > 0 | ||
BEGIN | ||
SELECT @customErrorMessage = FORMATMESSAGE(N'WrongExpectedVersion %i, stream already exists', @expected_version); | ||
THROW 50000, @customErrorMessage, 1; | ||
END | ||
ELSE | ||
THROW | ||
END CATCH | ||
END | ||
ELSE | ||
THROW 50001, N'StreamNotFound', 1; | ||
END | ||
ELSE | ||
THROW 50001, 'StreamNotFound', 1; | ||
END | ||
ELSE IF @expected_version != -2 and @expected_version != @current_version | ||
THROW 50000, 'WrongExpectedVersion %, current version %', 1; | ||
|
||
IF @expected_version != -2 and @expected_version != @current_version | ||
BEGIN | ||
SELECT @customErrorMessage = FORMATMESSAGE(N'WrongExpectedVersion %i, current version %i', @expected_version, @current_version); | ||
THROW 50000, @customErrorMessage, 1; | ||
END | ||
END |
This file contains 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