Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using System.Threading.Tasks;
using JasperFx.Events;
using Marten.Testing.Harness;
using Npgsql;
using Shouldly;
using Weasel.Core;
using Xunit;

namespace EventSourcingTests.Bugs;

public class Bug_4246_integer_out_of_range_in_quick_append_function : OneOffConfigurationsContext
{
public Bug_4246_integer_out_of_range_in_quick_append_function()
{
StoreOptions(opts =>
{
opts.Events.AppendMode = EventAppendMode.Quick;
opts.Connection(ConnectionSource.ConnectionString);
});
}

[Fact]
public async Task quick_append_should_handle_sequence_ids_above_int_max_value()
{
var schemaName = theStore.Options.DatabaseSchemaName;
const long expectedFirstSequence = 1L;
const long expectedSecondSequence = 2_200_000_000L;

await using var session = theStore.LightweightSession();
var streamId = session.Events.StartStream<Quest>(new QuestStarted { Name = "Test" });
await session.SaveChangesAsync();

await using var conn = new NpgsqlConnection(ConnectionSource.ConnectionString);
await conn.OpenAsync();
await conn.CreateCommand($"ALTER SEQUENCE {schemaName}.mt_events_sequence RESTART WITH {expectedSecondSequence}")
.ExecuteNonQueryAsync();
await conn.CloseAsync();

await using var session2 = theStore.LightweightSession();
session2.Events.Append(streamId.Id, new MembersJoined { Members = new[] { "Frodo" } });
await session2.SaveChangesAsync();

await using var session3 = theStore.LightweightSession();
var events = await session3.Events.FetchStreamAsync(streamId.Id);
events.Count.ShouldBe(2);
events[0].Sequence.ShouldBe(expectedFirstSequence);
events[1].Sequence.ShouldBe(expectedSecondSequence);
}
}
8 changes: 4 additions & 4 deletions src/Marten/Events/Schema/QuickAppendEventFunction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,16 +98,16 @@ public override void WriteCreateStatement(Migrator migrator, TextWriter writer)
}

writer.WriteLine($@"
CREATE OR REPLACE FUNCTION {Identifier}(stream {streamIdType}, stream_type varchar, tenantid varchar, event_ids uuid[], event_types varchar[], dotnet_types varchar[], bodies jsonb[]{metadataParameters}{tagParameters}) RETURNS int[] AS $$
CREATE OR REPLACE FUNCTION {Identifier}(stream {streamIdType}, stream_type varchar, tenantid varchar, event_ids uuid[], event_types varchar[], dotnet_types varchar[], bodies jsonb[]{metadataParameters}{tagParameters}) RETURNS bigint[] AS $$
DECLARE
event_version int;
event_version bigint;
event_type varchar;
event_id uuid;
body jsonb;
index int;
seq int;
seq bigint;
actual_tenant varchar;
return_value int[];
return_value bigint[];
BEGIN
select version into event_version from {databaseSchema}.mt_streams where {streamsWhere};
if event_version IS NULL then
Expand Down
2 changes: 1 addition & 1 deletion src/Marten/Schema/SQL/mt_get_next_hi.sql
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
CREATE
OR REPLACE FUNCTION {databaseSchema}.mt_get_next_hi(entity varchar) RETURNS integer AS
OR REPLACE FUNCTION {databaseSchema}.mt_get_next_hi(entity varchar) RETURNS bigint AS
$$
DECLARE
current_value bigint;
Expand Down
Loading