Skip to content

Conversation

@nmummau
Copy link
Contributor

@nmummau nmummau commented Sep 22, 2025

Each commit has isolated changes to a SQL table or procedure with detailed changes
No functional changes; these changes were a result of me doing a deep review of the procedure/tables that Eventuous creates.

This fixes #428

Other changes do include preferences, but I feel will aid to this SQL being easier to maintain for the long term.

Summary by Bito

This pull request addresses issue #428 by implementing improvements across PostgreSQL and SQL Server components. The PostgreSQL update corrects stream data ordering, while SQL Server changes refactor schema definitions and stored procedures. Updates standardize data types, constraints, and error handling to enhance transaction safety, maintainability, and performance. These modifications improve the overall reliability and consistency of the event store implementation.

@nmummau nmummau changed the title Feature/my change SQL improvements, fix issue 428 Sep 22, 2025
@alexeyzimarev alexeyzimarev changed the title SQL improvements, fix issue 428 SQL improvements, fix issue #428 Sep 26, 2025
@nmummau nmummau force-pushed the feature/my-change branch 2 times, most recently from 2ace9de to bad1390 Compare October 7, 2025 02:33
@bito-code-review
Copy link
Contributor

bito-code-review bot commented Oct 7, 2025

Changelist by Bito

This pull request implements the following key changes.

Key Change Files Impacted
Bug Fix - Fix Ordering Column in Postgres Script

6_ReadStreamForwards.sql - Updated the ordering clause from m.global_position to m.stream_position to ensure correct stream read behavior.

Feature Improvement - Refactor SQL Server Schema and Procedures

1_Schema.sql - Standardized data types, constraints, and added optimization hints in the schema definitions.

2_AppendEvents.sql - Enhanced error handling, improved transaction safety, and standardized parameter types in the event appending procedure.

3_CheckStream.sql - Refined error messaging and transaction management in the stream checking procedure.

4_ReadAllForwards.sql - Improved query structure and type consistency in the read-all-forwards procedure.

5_ReadStreamBackwards.sql - Enhanced validation logic and control flow for backward stream reads.

6_ReadStreamForwards.sql - Streamlined conditional checks and ensured robust query execution in the stream forwards procedure.

7_ReadStreamSub.sql - Standardized parameter definitions and query ordering for the sub stream reading procedure.

8_TruncateStream.sql - Refined deletion conditions, improved error handling, and enforced parameter validations in the stream truncation procedure.

Copy link
Contributor

@bito-code-review bito-code-review bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review Agent Run #29d3df

Actionable Suggestions - 3
  • src/SqlServer/src/Eventuous.SqlServer/Scripts/1_Schema.sql - 2
  • src/SqlServer/src/Eventuous.SqlServer/Scripts/6_ReadStreamForwards.sql - 1
Review Details
  • Files reviewed - 8 · Commit Range: 67cf323..bad1390
    • src/SqlServer/src/Eventuous.SqlServer/Scripts/1_Schema.sql
    • src/SqlServer/src/Eventuous.SqlServer/Scripts/2_AppendEvents.sql
    • src/SqlServer/src/Eventuous.SqlServer/Scripts/3_CheckStream.sql
    • src/SqlServer/src/Eventuous.SqlServer/Scripts/4_ReadAllForwards.sql
    • src/SqlServer/src/Eventuous.SqlServer/Scripts/5_ReadStreamBackwards.sql
    • src/SqlServer/src/Eventuous.SqlServer/Scripts/6_ReadStreamForwards.sql
    • src/SqlServer/src/Eventuous.SqlServer/Scripts/7_ReadStreamSub.sql
    • src/SqlServer/src/Eventuous.SqlServer/Scripts/8_TruncateStream.sql
  • Files skipped - 0
  • Tools
    • Whispers (Secret Scanner) - ✔︎ Successful
    • Detect-secrets (Secret Scanner) - ✔︎ Successful

Bito Usage Guide

Commands

Type the following command in the pull request comment and save the comment.

  • /review - Manually triggers a full AI review.

  • /pause - Pauses automatic reviews on this pull request.

  • /resume - Resumes automatic reviews.

  • /resolve - Marks all Bito-posted review comments as resolved.

  • /abort - Cancels all in-progress reviews.

Refer to the documentation for additional commands.

Configuration

This repository uses Default Agent You can customize the agent settings here or contact your Bito workspace admin at [email protected].

Documentation & Help

AI Code Review powered by Bito Logo

@github-actions
Copy link

github-actions bot commented Oct 7, 2025

Test Results

 32 files  + 16   32 suites  +16   1h 0m 32s ⏱️ + 30m 38s
230 tests +  1  230 ✅ +  1  0 💤 ±0  0 ❌ ±0 
470 runs  +235  470 ✅ +235  0 💤 ±0  0 ❌ ±0 

Results for commit aec84c8. ± Comparison against base commit f4444af.

This pull request removes 1 and adds 2 tests. Note that renamed tests count towards both.
Eventuous.Tests.Subscriptions.SequenceTests ‑ ShouldReturnFirstBefore(CommitPositionSequence, CommitPosition { Position: 0, Sequence: 2, Timestamp: 2025-10-07T12:46:22.7317565+00:00 })
Eventuous.Tests.Subscriptions.SequenceTests ‑ ShouldReturnFirstBefore(CommitPositionSequence, CommitPosition { Position: 0, Sequence: 2, Timestamp: 2025-10-14T13:35:17.4206739+00:00 })
Eventuous.Tests.Subscriptions.SequenceTests ‑ ShouldReturnFirstBefore(CommitPositionSequence, CommitPosition { Position: 0, Sequence: 2, Timestamp: 2025-10-14T13:35:43.2952556+00:00 })

♻️ This comment has been updated with latest results.

@nmummau nmummau force-pushed the feature/my-change branch 5 times, most recently from 9f4b09e to 83e0f4f Compare October 8, 2025 01:28
@nmummau nmummau requested a review from alexeyzimarev October 8, 2025 01:34
@nmummau nmummau force-pushed the feature/my-change branch from 72826c2 to 49dddfe Compare October 8, 2025 01:36
- SET NOCOUNT ON
- Wrap keywords in brackets ([Version], [Messages])
- formatting: each column on it's own line. Best practice for maintainability of procedures
- proper casing of keywords (INT, AND)
- use alias to DELETE statement
- semicolons
@nmummau nmummau force-pushed the feature/my-change branch from 49dddfe to 69ec7ac Compare October 8, 2025 01:36
@nmummau
Copy link
Contributor Author

nmummau commented Oct 8, 2025

/review

Copy link
Contributor

@bito-code-review bito-code-review bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review Agent Run #f05148

Actionable Suggestions - 4
  • src/SqlServer/src/Eventuous.SqlServer/Scripts/1_Schema.sql - 3
  • src/SqlServer/src/Eventuous.SqlServer/Scripts/7_ReadStreamSub.sql - 1
Additional Suggestions - 1
  • src/SqlServer/src/Eventuous.SqlServer/Scripts/5_ReadStreamBackwards.sql - 1
    • Inconsistent table naming · Line 37-37
      The Messages table reference uses square brackets (__schema__.[Messages]) which creates inconsistency with the schema placeholder pattern used throughout the codebase. The __schema__ placeholder appears without brackets in __schema__.Streams and should follow the same pattern. This affects downstream consumers like `SqlServerStore.GetReadBackwardsCommand` which expects consistent naming conventions.
      Code suggestion
       @@ -37,1 +37,1 @@
      -    FROM __schema__.[Messages]
      +    FROM __schema__.Messages
Review Details
  • Files reviewed - 9 · Commit Range: 10f371d..69ec7ac
    • src/Postgres/src/Eventuous.Postgresql/Scripts/6_ReadStreamForwards.sql
    • src/SqlServer/src/Eventuous.SqlServer/Scripts/1_Schema.sql
    • src/SqlServer/src/Eventuous.SqlServer/Scripts/2_AppendEvents.sql
    • src/SqlServer/src/Eventuous.SqlServer/Scripts/3_CheckStream.sql
    • src/SqlServer/src/Eventuous.SqlServer/Scripts/4_ReadAllForwards.sql
    • src/SqlServer/src/Eventuous.SqlServer/Scripts/5_ReadStreamBackwards.sql
    • src/SqlServer/src/Eventuous.SqlServer/Scripts/6_ReadStreamForwards.sql
    • src/SqlServer/src/Eventuous.SqlServer/Scripts/7_ReadStreamSub.sql
    • src/SqlServer/src/Eventuous.SqlServer/Scripts/8_TruncateStream.sql
  • Files skipped - 0
  • Tools
    • Whispers (Secret Scanner) - ✔︎ Successful
    • Detect-secrets (Secret Scanner) - ✔︎ Successful

Bito Usage Guide

Commands

Type the following command in the pull request comment and save the comment.

  • /review - Manually triggers a full AI review.

  • /pause - Pauses automatic reviews on this pull request.

  • /resume - Resumes automatic reviews.

  • /resolve - Marks all Bito-posted review comments as resolved.

  • /abort - Cancels all in-progress reviews.

Refer to the documentation for additional commands.

Configuration

This repository uses Default Agent You can customize the agent settings here or contact your Bito workspace admin at [email protected].

Documentation & Help

AI Code Review powered by Bito Logo

nealmummau and others added 11 commits October 7, 2025 22:07
- SET NOCOUNT ON
- Wrap keywords in brackets ([Messages])
- formatting: each column on it's own line. Best practice for maintainability of procedures
- proper casing of keywords (INT, BIGINT)
- remove 'Messages.' from query since it was redundant
- semicolons
- SET NOCOUNT ON
- Wrap keywords in brackets ([Version], [Messages])
- formatting: each column on it's own line. Best practice for maintainability of procedures
- proper casing of keywords (INT)
- remove 'Messages.' from query since it was redundant
- semicolons
- SET NOCOUNT ON
- Wrap keywords in brackets ([Version], [Messages])
- formatting: each column on it's own line. Best practice for maintainability of procedures
- proper casing of keywords (INT)
- remove 'Messages.' from query since it was redundant
- semicolons
- SET NOCOUNT ON
- formatting: each column on it's own line. Best practice for maintainability of procedures
- use aliases
- semicolons
- SET NOCOUNT ON
- formatting: each column on it's own line. Best practice for maintainability of procedures
- proper casing of keywords (AND)
- semicolons
- SET NOCOUNT ON
- remove unused declared variables
- specify all parameters when calling [check_stream] procedure
- formatting: each column on it's own line. Best practice for maintainability of procedures
- proper casing of keywords (IF, ISNULL, NVARCHAR)
- use alias to UPDATE statement
- remove 'AS' keyword
- semicolons
- consistent casing of [Version] column
- explicitly specify NULLable columns
SQL Server schema change so that Messages.Created is NOT NULL. This is safe and accurate to do since append_events will always set this value if a NULL is provided to the procedure.
- Messages TABLE
    - JsonMetaData is NOT NULL
    - json columns are checked to ensure they contain json data
- StreamMessage TYPE
    - json_metadata is NOT NULL
@nmummau nmummau force-pushed the feature/my-change branch 4 times, most recently from 7184881 to 9426516 Compare October 8, 2025 17:00
At first when writing this change I assumed I would be out to fix issue Eventuous#429. Turns out that is not an issue as the client wraps this procedure call in a transaction. Thus, no transction is needed within the procedure itself. Here are some improvements, nonetheless.

- Changed @stream_name parameter to NVARCHAR(850) for consistency
- Introduced @Inserted table to capture inserted GlobalPosition values atomically
- Normalized optimistic concurrency errors (2627, 2601) to client-detectable messages
- Thrown error messages still StartsWith 'WrongExpectedVersion' but now also includes the full SQL error text for client tracing
- Removed redundant post-commit queries and error-parsing logic
…treams and Messages

- Added WITH (OPTIMIZE_FOR_SEQUENTIAL_KEY = ON) to clustered primary keys on __schema__.Streams (PK_Streams) and __schema__.Messages (PK_Events)
- Improves insert throughput and reduces latch contention on identity-based keys
- No changes to schema shape, constraints, or indexes beyond PK optimization

This improves concurrent insert performance under high write workloads by allowing SQL Server to better handle last-page insert contention on sequential identity keys.
See docs on OPTIMIZE_FOR_SEQUENTIAL_KEY: https://learn.microsoft.com/en-us/sql/t-sql/statements/alter-table-index-option-transact-sql?view=sql-server-ver17#optimize_for_sequential_key---on--off-
…ntuous

Added SET XACT_ABORT ON to all SQL stored procedures invoked through
GetStoredProcCommand() to ensure atomic rollback behavior when the client
transaction encounters an error. This prevents partial commits and
inconsistent transactional states during event append and stream operations.

Related to issue Eventuous#429
@nmummau nmummau force-pushed the feature/my-change branch from 6cc3ba9 to 1599c02 Compare October 8, 2025 17:22
@nmummau
Copy link
Contributor Author

nmummau commented Oct 8, 2025

/review

Copy link
Contributor

@bito-code-review bito-code-review bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review Agent Run #664c60

Actionable Suggestions - 5
  • src/SqlServer/src/Eventuous.SqlServer/Scripts/6_ReadStreamForwards.sql - 1
  • src/SqlServer/src/Eventuous.SqlServer/Scripts/2_AppendEvents.sql - 2
  • src/SqlServer/src/Eventuous.SqlServer/Scripts/1_Schema.sql - 1
  • src/SqlServer/src/Eventuous.SqlServer/Scripts/5_ReadStreamBackwards.sql - 1
Review Details
  • Files reviewed - 9 · Commit Range: 9e2b609..1599c02
    • src/Postgres/src/Eventuous.Postgresql/Scripts/6_ReadStreamForwards.sql
    • src/SqlServer/src/Eventuous.SqlServer/Scripts/1_Schema.sql
    • src/SqlServer/src/Eventuous.SqlServer/Scripts/2_AppendEvents.sql
    • src/SqlServer/src/Eventuous.SqlServer/Scripts/3_CheckStream.sql
    • src/SqlServer/src/Eventuous.SqlServer/Scripts/4_ReadAllForwards.sql
    • src/SqlServer/src/Eventuous.SqlServer/Scripts/5_ReadStreamBackwards.sql
    • src/SqlServer/src/Eventuous.SqlServer/Scripts/6_ReadStreamForwards.sql
    • src/SqlServer/src/Eventuous.SqlServer/Scripts/7_ReadStreamSub.sql
    • src/SqlServer/src/Eventuous.SqlServer/Scripts/8_TruncateStream.sql
  • Files skipped - 0
  • Tools
    • Whispers (Secret Scanner) - ✔︎ Successful
    • Detect-secrets (Secret Scanner) - ✔︎ Successful

Bito Usage Guide

Commands

Type the following command in the pull request comment and save the comment.

  • /review - Manually triggers a full AI review.

  • /pause - Pauses automatic reviews on this pull request.

  • /resume - Resumes automatic reviews.

  • /resolve - Marks all Bito-posted review comments as resolved.

  • /abort - Cancels all in-progress reviews.

Refer to the documentation for additional commands.

Configuration

This repository uses Default Agent You can customize the agent settings here or contact your Bito workspace admin at [email protected].

Documentation & Help

AI Code Review powered by Bito Logo

@bito-code-review
Copy link
Contributor

bito-code-review bot commented Oct 8, 2025

Code Review Agent Run #0f1763

Actionable Suggestions - 0
Review Details
  • Files reviewed - 9 · Commit Range: 9e2b609..1599c02
    • src/Postgres/src/Eventuous.Postgresql/Scripts/6_ReadStreamForwards.sql
    • src/SqlServer/src/Eventuous.SqlServer/Scripts/1_Schema.sql
    • src/SqlServer/src/Eventuous.SqlServer/Scripts/2_AppendEvents.sql
    • src/SqlServer/src/Eventuous.SqlServer/Scripts/3_CheckStream.sql
    • src/SqlServer/src/Eventuous.SqlServer/Scripts/4_ReadAllForwards.sql
    • src/SqlServer/src/Eventuous.SqlServer/Scripts/5_ReadStreamBackwards.sql
    • src/SqlServer/src/Eventuous.SqlServer/Scripts/6_ReadStreamForwards.sql
    • src/SqlServer/src/Eventuous.SqlServer/Scripts/7_ReadStreamSub.sql
    • src/SqlServer/src/Eventuous.SqlServer/Scripts/8_TruncateStream.sql
  • Files skipped - 0
  • Tools
    • Whispers (Secret Scanner) - ✔︎ Successful
    • Detect-secrets (Secret Scanner) - ✔︎ Successful

Bito Usage Guide

Commands

Type the following command in the pull request comment and save the comment.

  • /review - Manually triggers a full AI review.

  • /pause - Pauses automatic reviews on this pull request.

  • /resume - Resumes automatic reviews.

  • /resolve - Marks all Bito-posted review comments as resolved.

  • /abort - Cancels all in-progress reviews.

Refer to the documentation for additional commands.

Configuration

This repository uses Default Agent You can customize the agent settings here or contact your Bito workspace admin at [email protected].

Documentation & Help

AI Code Review powered by Bito Logo

@bito-code-review
Copy link
Contributor

bito-code-review bot commented Oct 9, 2025

Code Review Agent Run #07cb94

Actionable Suggestions - 0
Review Details
  • Files reviewed - 9 · Commit Range: 1599c02..9bf3bf0
    • src/Postgres/src/Eventuous.Postgresql/Scripts/6_ReadStreamForwards.sql
    • src/SqlServer/src/Eventuous.SqlServer/Scripts/1_Schema.sql
    • src/SqlServer/src/Eventuous.SqlServer/Scripts/2_AppendEvents.sql
    • src/SqlServer/src/Eventuous.SqlServer/Scripts/3_CheckStream.sql
    • src/SqlServer/src/Eventuous.SqlServer/Scripts/4_ReadAllForwards.sql
    • src/SqlServer/src/Eventuous.SqlServer/Scripts/5_ReadStreamBackwards.sql
    • src/SqlServer/src/Eventuous.SqlServer/Scripts/6_ReadStreamForwards.sql
    • src/SqlServer/src/Eventuous.SqlServer/Scripts/7_ReadStreamSub.sql
    • src/SqlServer/src/Eventuous.SqlServer/Scripts/8_TruncateStream.sql
  • Files skipped - 0
  • Tools
    • Whispers (Secret Scanner) - ✔︎ Successful
    • Detect-secrets (Secret Scanner) - ✔︎ Successful

Bito Usage Guide

Commands

Type the following command in the pull request comment and save the comment.

  • /review - Manually triggers a full AI review.

  • /pause - Pauses automatic reviews on this pull request.

  • /resume - Resumes automatic reviews.

  • /resolve - Marks all Bito-posted review comments as resolved.

  • /abort - Cancels all in-progress reviews.

Refer to the documentation for additional commands.

Configuration

This repository uses Default Agent You can customize the agent settings here or contact your Bito workspace admin at [email protected].

Documentation & Help

AI Code Review powered by Bito Logo

@bito-code-review
Copy link
Contributor

bito-code-review bot commented Oct 9, 2025

Interaction Diagram by Bito
sequenceDiagram
participant App as Application
participant Store as EventStore<br/>(PostgresStore/SqlServerStore)
participant Schema as Schema<br/>(MODIFIED)
participant DB as Database<br/>(Postgres/SqlServer)
participant Scripts as SQL Scripts<br/>(MODIFIED)
Note over Scripts: Enhanced ordering, constraints<br/>& concurrency control
App->>Store: ReadEvents/AppendEvents
Store->>Schema: Get SQL command
Schema->>Scripts: Execute stored procedure
Scripts->>DB: Query with improved logic
DB-->>Scripts: Results
Scripts-->>Schema: Response
Schema-->>Store: Data
Store-->>App: Events/AppendResult
Loading

Critical path: Application->EventStore->Schema (MODIFIED)->SQL Scripts (MODIFIED)->Database

Note: The SQL scripts for PostgreSQL and SQL Server event stores were enhanced with better ordering (stream_position vs global_position), improved constraints, JSON validation, and enhanced concurrency control. These changes improve data consistency and performance in the database layer.

@bito-code-review
Copy link
Contributor

bito-code-review bot commented Oct 9, 2025

Code Review Agent Run #aa2f1a

Actionable Suggestions - 0
Review Details
  • Files reviewed - 9 · Commit Range: 9bf3bf0..ea33765
    • src/Postgres/src/Eventuous.Postgresql/Scripts/6_ReadStreamForwards.sql
    • src/SqlServer/src/Eventuous.SqlServer/Scripts/1_Schema.sql
    • src/SqlServer/src/Eventuous.SqlServer/Scripts/2_AppendEvents.sql
    • src/SqlServer/src/Eventuous.SqlServer/Scripts/3_CheckStream.sql
    • src/SqlServer/src/Eventuous.SqlServer/Scripts/4_ReadAllForwards.sql
    • src/SqlServer/src/Eventuous.SqlServer/Scripts/5_ReadStreamBackwards.sql
    • src/SqlServer/src/Eventuous.SqlServer/Scripts/6_ReadStreamForwards.sql
    • src/SqlServer/src/Eventuous.SqlServer/Scripts/7_ReadStreamSub.sql
    • src/SqlServer/src/Eventuous.SqlServer/Scripts/8_TruncateStream.sql
  • Files skipped - 0
  • Tools
    • Whispers (Secret Scanner) - ✔︎ Successful
    • Detect-secrets (Secret Scanner) - ✔︎ Successful

Bito Usage Guide

Commands

Type the following command in the pull request comment and save the comment.

  • /review - Manually triggers a full AI review.

  • /pause - Pauses automatic reviews on this pull request.

  • /resume - Resumes automatic reviews.

  • /resolve - Marks all Bito-posted review comments as resolved.

  • /abort - Cancels all in-progress reviews.

Refer to the documentation for additional commands.

Configuration

This repository uses Default Agent You can customize the agent settings here or contact your Bito workspace admin at [email protected].

Documentation & Help

AI Code Review powered by Bito Logo

… querying the table for the record we just inserted
Replaced invalid guard `@current_version < @from_position + @count` with proper
range validation `@from_position < 0 OR @from_position > @current_version`.

Previous logic incorrectly returned no results when valid messages existed.
New check correctly validates start position independently of page size.
and added `@@ROWCOUNT` check to detect concurrent updates.
If no rows are affected, the procedure now
throws a `WrongExpectedVersion` error, aligning behavior with duplicate
append handling.

- Prevents silent version overwrite on race conditions
- Ensures consistency with optimistic concurrency semantics
- Keeps existing WrongExpectedVersion error format for client handling
@nmummau
Copy link
Contributor Author

nmummau commented Oct 10, 2025

/review

@bito-code-review
Copy link
Contributor

bito-code-review bot commented Oct 10, 2025

Code Review Agent Run #a7a288

Actionable Suggestions - 0
Review Details
  • Files reviewed - 9 · Commit Range: 9e2b609..aec84c8
    • src/Postgres/src/Eventuous.Postgresql/Scripts/6_ReadStreamForwards.sql
    • src/SqlServer/src/Eventuous.SqlServer/Scripts/1_Schema.sql
    • src/SqlServer/src/Eventuous.SqlServer/Scripts/2_AppendEvents.sql
    • src/SqlServer/src/Eventuous.SqlServer/Scripts/3_CheckStream.sql
    • src/SqlServer/src/Eventuous.SqlServer/Scripts/4_ReadAllForwards.sql
    • src/SqlServer/src/Eventuous.SqlServer/Scripts/5_ReadStreamBackwards.sql
    • src/SqlServer/src/Eventuous.SqlServer/Scripts/6_ReadStreamForwards.sql
    • src/SqlServer/src/Eventuous.SqlServer/Scripts/7_ReadStreamSub.sql
    • src/SqlServer/src/Eventuous.SqlServer/Scripts/8_TruncateStream.sql
  • Files skipped - 0
  • Tools
    • Whispers (Secret Scanner) - ✔︎ Successful
    • Detect-secrets (Secret Scanner) - ✔︎ Successful

Bito Usage Guide

Commands

Type the following command in the pull request comment and save the comment.

  • /review - Manually triggers a full AI review.

  • /pause - Pauses automatic reviews on this pull request.

  • /resume - Resumes automatic reviews.

  • /resolve - Marks all Bito-posted review comments as resolved.

  • /abort - Cancels all in-progress reviews.

Refer to the documentation for additional commands.

Configuration

This repository uses Default Agent You can customize the agent settings here or contact your Bito workspace admin at [email protected].

Documentation & Help

AI Code Review powered by Bito Logo

Copy link
Contributor

@bito-code-review bito-code-review bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review Agent Run #66ce0b

Actionable Suggestions - 2
  • src/SqlServer/src/Eventuous.SqlServer/Scripts/2_AppendEvents.sql - 1
    • Incorrect position for zero-message appends · Line 102-106
  • src/SqlServer/src/Eventuous.SqlServer/Scripts/1_Schema.sql - 1
Review Details
  • Files reviewed - 9 · Commit Range: 9e2b609..aec84c8
    • src/Postgres/src/Eventuous.Postgresql/Scripts/6_ReadStreamForwards.sql
    • src/SqlServer/src/Eventuous.SqlServer/Scripts/1_Schema.sql
    • src/SqlServer/src/Eventuous.SqlServer/Scripts/2_AppendEvents.sql
    • src/SqlServer/src/Eventuous.SqlServer/Scripts/3_CheckStream.sql
    • src/SqlServer/src/Eventuous.SqlServer/Scripts/4_ReadAllForwards.sql
    • src/SqlServer/src/Eventuous.SqlServer/Scripts/5_ReadStreamBackwards.sql
    • src/SqlServer/src/Eventuous.SqlServer/Scripts/6_ReadStreamForwards.sql
    • src/SqlServer/src/Eventuous.SqlServer/Scripts/7_ReadStreamSub.sql
    • src/SqlServer/src/Eventuous.SqlServer/Scripts/8_TruncateStream.sql
  • Files skipped - 0
  • Tools
    • Whispers (Secret Scanner) - ✔︎ Successful
    • Detect-secrets (Secret Scanner) - ✔︎ Successful

Bito Usage Guide

Commands

Type the following command in the pull request comment and save the comment.

  • /review - Manually triggers a full AI review.

  • /pause - Pauses automatic reviews on this pull request.

  • /resume - Resumes automatic reviews.

  • /resolve - Marks all Bito-posted review comments as resolved.

  • /abort - Cancels all in-progress reviews.

Refer to the documentation for additional commands.

Configuration

This repository uses Default Agent You can customize the agent settings here or contact your Bito workspace admin at [email protected].

Documentation & Help

AI Code Review powered by Bito Logo

@alexeyzimarev alexeyzimarev merged commit c489f67 into Eventuous:dev Oct 18, 2025
4 checks passed
nmummau added a commit to nmummau/eventuous that referenced this pull request Oct 22, 2025
* feat(mssql): Minor SQL changes; no functional changes
- SET NOCOUNT ON
- Wrap keywords in brackets ([Version], [Messages])
- formatting: each column on it's own line. Best practice for maintainability of procedures
- proper casing of keywords (INT, AND)
- use alias to DELETE statement
- semicolons

* feat(mssql): Minor SQL changes; no functional changes
- SET NOCOUNT ON
- Wrap keywords in brackets ([Messages])
- formatting: each column on it's own line. Best practice for maintainability of procedures
- proper casing of keywords (INT, BIGINT)
- remove 'Messages.' from query since it was redundant
- semicolons

* feat(mssql): Minor SQL changes; no functional changes
- SET NOCOUNT ON
- Wrap keywords in brackets ([Version], [Messages])
- formatting: each column on it's own line. Best practice for maintainability of procedures
- proper casing of keywords (INT)
- remove 'Messages.' from query since it was redundant
- semicolons

* feat(mssql): Minor SQL changes; no functional changes
- SET NOCOUNT ON
- Wrap keywords in brackets ([Version], [Messages])
- formatting: each column on it's own line. Best practice for maintainability of procedures
- proper casing of keywords (INT)
- remove 'Messages.' from query since it was redundant
- semicolons

* feat(mssql): Minor SQL changes; no functional changes
- SET NOCOUNT ON
- formatting: each column on it's own line. Best practice for maintainability of procedures
- use aliases
- semicolons

* feat(mssql): Minor SQL changes; no functional changes
- SET NOCOUNT ON
- formatting: each column on it's own line. Best practice for maintainability of procedures
- proper casing of keywords (AND)
- semicolons

* feat(mssql): Minor SQL changes; no functional changes
- SET NOCOUNT ON
- remove unused declared variables
- specify all parameters when calling [check_stream] procedure
- formatting: each column on it's own line. Best practice for maintainability of procedures
- proper casing of keywords (IF, ISNULL, NVARCHAR)
- use alias to UPDATE statement
- remove 'AS' keyword
- semicolons

* feat(mssql): Minor SQL updates
- consistent casing of [Version] column
- explicitly specify NULLable columns

* fix(mssql): Messages.Created NOT NULL

SQL Server schema change so that Messages.Created is NOT NULL. This is safe and accurate to do since append_events will always set this value if a NULL is provided to the procedure.

* feat(mssql): json is not null

- Messages TABLE
    - JsonMetaData is NOT NULL
    - json columns are checked to ensure they contain json data
- StreamMessage TYPE
    - json_metadata is NOT NULL

* feat(mssql): DATETIME2 is actually a DATETIME2(7) so explicitly say that in the scripts

* feat(mssql): ORDER BY StreamPosition per bito-code-review, and and remove redundencies in the CHECK definition

* feat(postgresql): ORDER BY stream_position per bito-code-review

* refactor(mssql): remove square brackets to satisfy 'inconsistent table naming' warning that bito-code-review flagged

* feat(mssql): append_events improve

At first when writing this change I assumed I would be out to fix issue Eventuous#429. Turns out that is not an issue as the client wraps this procedure call in a transaction. Thus, no transction is needed within the procedure itself. Here are some improvements, nonetheless.

- Changed @stream_name parameter to NVARCHAR(850) for consistency
- Introduced @Inserted table to capture inserted GlobalPosition values atomically
- Normalized optimistic concurrency errors (2627, 2601) to client-detectable messages
- Thrown error messages still StartsWith 'WrongExpectedVersion' but now also includes the full SQL error text for client tracing
- Removed redundant post-commit queries and error-parsing logic

* perf(mssql): enable OPTIMIZE_FOR_SEQUENTIAL_KEY on primary keys for Streams and Messages

- Added WITH (OPTIMIZE_FOR_SEQUENTIAL_KEY = ON) to clustered primary keys on __schema__.Streams (PK_Streams) and __schema__.Messages (PK_Events)
- Improves insert throughput and reduces latch contention on identity-based keys
- No changes to schema shape, constraints, or indexes beyond PK optimization

This improves concurrent insert performance under high write workloads by allowing SQL Server to better handle last-page insert contention on sequential identity keys.
See docs on OPTIMIZE_FOR_SEQUENTIAL_KEY: https://learn.microsoft.com/en-us/sql/t-sql/statements/alter-table-index-option-transact-sql?view=sql-server-ver17#optimize_for_sequential_key---on--off-

* chore(mssql): enable XACT_ABORT for all stored procedures used by Eventuous

Added SET XACT_ABORT ON to all SQL stored procedures invoked through
GetStoredProcCommand() to ensure atomic rollback behavior when the client
transaction encounters an error. This prevents partial commits and
inconsistent transactional states during event append and stream operations.

Related to issue Eventuous#429

* fix(mssql): truncate_stream wasn't properly formatting and and sending the error message

* refactor(mssql): use SCOPE_IDENTITY() to get new StreamId rather than querying the table for the record we just inserted

* fix(mssql): correct backwards read bounds check

Replaced invalid guard `@current_version < @from_position + @count` with proper
range validation `@from_position < 0 OR @from_position > @current_version`.

Previous logic incorrectly returned no results when valid messages existed.
New check correctly validates start position independently of page size.

* fix(mssql): moved stream version update inside TRY block

and added `@@ROWCOUNT` check to detect concurrent updates.
If no rows are affected, the procedure now
throws a `WrongExpectedVersion` error, aligning behavior with duplicate
append handling.

- Prevents silent version overwrite on race conditions
- Ensures consistency with optimistic concurrency semantics
- Keeps existing WrongExpectedVersion error format for client handling

---------

Co-authored-by: Neal Mummau <[email protected]>
nmummau added a commit to nmummau/eventuous that referenced this pull request Oct 23, 2025
* feat(mssql): Minor SQL changes; no functional changes
- SET NOCOUNT ON
- Wrap keywords in brackets ([Version], [Messages])
- formatting: each column on it's own line. Best practice for maintainability of procedures
- proper casing of keywords (INT, AND)
- use alias to DELETE statement
- semicolons

* feat(mssql): Minor SQL changes; no functional changes
- SET NOCOUNT ON
- Wrap keywords in brackets ([Messages])
- formatting: each column on it's own line. Best practice for maintainability of procedures
- proper casing of keywords (INT, BIGINT)
- remove 'Messages.' from query since it was redundant
- semicolons

* feat(mssql): Minor SQL changes; no functional changes
- SET NOCOUNT ON
- Wrap keywords in brackets ([Version], [Messages])
- formatting: each column on it's own line. Best practice for maintainability of procedures
- proper casing of keywords (INT)
- remove 'Messages.' from query since it was redundant
- semicolons

* feat(mssql): Minor SQL changes; no functional changes
- SET NOCOUNT ON
- Wrap keywords in brackets ([Version], [Messages])
- formatting: each column on it's own line. Best practice for maintainability of procedures
- proper casing of keywords (INT)
- remove 'Messages.' from query since it was redundant
- semicolons

* feat(mssql): Minor SQL changes; no functional changes
- SET NOCOUNT ON
- formatting: each column on it's own line. Best practice for maintainability of procedures
- use aliases
- semicolons

* feat(mssql): Minor SQL changes; no functional changes
- SET NOCOUNT ON
- formatting: each column on it's own line. Best practice for maintainability of procedures
- proper casing of keywords (AND)
- semicolons

* feat(mssql): Minor SQL changes; no functional changes
- SET NOCOUNT ON
- remove unused declared variables
- specify all parameters when calling [check_stream] procedure
- formatting: each column on it's own line. Best practice for maintainability of procedures
- proper casing of keywords (IF, ISNULL, NVARCHAR)
- use alias to UPDATE statement
- remove 'AS' keyword
- semicolons

* feat(mssql): Minor SQL updates
- consistent casing of [Version] column
- explicitly specify NULLable columns

* fix(mssql): Messages.Created NOT NULL

SQL Server schema change so that Messages.Created is NOT NULL. This is safe and accurate to do since append_events will always set this value if a NULL is provided to the procedure.

* feat(mssql): json is not null

- Messages TABLE
    - JsonMetaData is NOT NULL
    - json columns are checked to ensure they contain json data
- StreamMessage TYPE
    - json_metadata is NOT NULL

* feat(mssql): DATETIME2 is actually a DATETIME2(7) so explicitly say that in the scripts

* feat(mssql): ORDER BY StreamPosition per bito-code-review, and and remove redundencies in the CHECK definition

* feat(postgresql): ORDER BY stream_position per bito-code-review

* refactor(mssql): remove square brackets to satisfy 'inconsistent table naming' warning that bito-code-review flagged

* feat(mssql): append_events improve

At first when writing this change I assumed I would be out to fix issue Eventuous#429. Turns out that is not an issue as the client wraps this procedure call in a transaction. Thus, no transction is needed within the procedure itself. Here are some improvements, nonetheless.

- Changed @stream_name parameter to NVARCHAR(850) for consistency
- Introduced @Inserted table to capture inserted GlobalPosition values atomically
- Normalized optimistic concurrency errors (2627, 2601) to client-detectable messages
- Thrown error messages still StartsWith 'WrongExpectedVersion' but now also includes the full SQL error text for client tracing
- Removed redundant post-commit queries and error-parsing logic

* perf(mssql): enable OPTIMIZE_FOR_SEQUENTIAL_KEY on primary keys for Streams and Messages

- Added WITH (OPTIMIZE_FOR_SEQUENTIAL_KEY = ON) to clustered primary keys on __schema__.Streams (PK_Streams) and __schema__.Messages (PK_Events)
- Improves insert throughput and reduces latch contention on identity-based keys
- No changes to schema shape, constraints, or indexes beyond PK optimization

This improves concurrent insert performance under high write workloads by allowing SQL Server to better handle last-page insert contention on sequential identity keys.
See docs on OPTIMIZE_FOR_SEQUENTIAL_KEY: https://learn.microsoft.com/en-us/sql/t-sql/statements/alter-table-index-option-transact-sql?view=sql-server-ver17#optimize_for_sequential_key---on--off-

* chore(mssql): enable XACT_ABORT for all stored procedures used by Eventuous

Added SET XACT_ABORT ON to all SQL stored procedures invoked through
GetStoredProcCommand() to ensure atomic rollback behavior when the client
transaction encounters an error. This prevents partial commits and
inconsistent transactional states during event append and stream operations.

Related to issue Eventuous#429

* fix(mssql): truncate_stream wasn't properly formatting and and sending the error message

* refactor(mssql): use SCOPE_IDENTITY() to get new StreamId rather than querying the table for the record we just inserted

* fix(mssql): correct backwards read bounds check

Replaced invalid guard `@current_version < @from_position + @count` with proper
range validation `@from_position < 0 OR @from_position > @current_version`.

Previous logic incorrectly returned no results when valid messages existed.
New check correctly validates start position independently of page size.

* fix(mssql): moved stream version update inside TRY block

and added `@@ROWCOUNT` check to detect concurrent updates.
If no rows are affected, the procedure now
throws a `WrongExpectedVersion` error, aligning behavior with duplicate
append handling.

- Prevents silent version overwrite on race conditions
- Ensures consistency with optimistic concurrency semantics
- Keeps existing WrongExpectedVersion error format for client handling

---------

Co-authored-by: Neal Mummau <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Code smell unused variable

3 participants