diff --git a/db/schema/0.1.0.sql b/db/schema/0.1.0.sql index d83b4f3..f1faae5 100644 --- a/db/schema/0.1.0.sql +++ b/db/schema/0.1.0.sql @@ -27,7 +27,6 @@ CREATE TYPE en57.event AS ( ); CREATE TYPE en57.append_result AS ( - status text, "position" bigint, conflicting_events jsonb ); @@ -117,8 +116,7 @@ BEGIN e.id = matched_event_id INTO conflicting_events; - RETURN ROW ('append_condition_violated', - matched_position, + RETURN ROW (matched_position, conflicting_events)::en57.append_result; END IF; END LOOP; @@ -147,8 +145,7 @@ INSERT INTO en57.events (id, type, data, meta) FROM unnest(new_events) AS e CROSS JOIN LATERAL unnest(COALESCE(e.tags, ARRAY[]::text[])) AS t (value); - RETURN ROW ('success', - appended_position, + RETURN ROW (appended_position, NULL)::en57.append_result; END; $$; diff --git a/lib/en57/repository.rb b/lib/en57/repository.rb index 027bceb..2bc88a5 100644 --- a/lib/en57/repository.rb +++ b/lib/en57/repository.rb @@ -35,7 +35,7 @@ module En57 ] = fail_if_events_match unless fail_if_events_match.empty? statement = - "SELECT status, position, conflicting_events FROM en57.append_events($1::en57.event[], $2::jsonb)" + "SELECT position, conflicting_events FROM en57.append_events($1::en57.event[], $2::jsonb)" params = [ @array_encoder.encode(event_records), JSON.generate(append_condition), @@ -54,12 +54,7 @@ module En57 end end - case row.first.fetch("status") - when "success" - Success.new( - position: row.first.fetch("position").then { Integer(it) }, - ) - when "append_condition_violated" + if row.first.fetch("conflicting_events") Failure.new( position: row.first.fetch("position").then { Integer(it) }, conflicting_events: @@ -67,6 +62,10 @@ module En57 .parse(row.first.fetch("conflicting_events")) .map { deserialize_event(it) }, ) + else + Success.new( + position: row.first.fetch("position").then { Integer(it) }, + ) end rescue @adapter.serialization_error if attempts_remaining.positive? diff --git a/test/pg_regress/expected/001_schema.out b/test/pg_regress/expected/001_schema.out index d83b4f3..f1faae5 100644 --- a/test/pg_regress/expected/001_schema.out +++ b/test/pg_regress/expected/001_schema.out @@ -27,7 +27,6 @@ CREATE TYPE en57.event AS ( ); CREATE TYPE en57.append_result AS ( - status text, "position" bigint, conflicting_events jsonb ); @@ -117,8 +116,7 @@ BEGIN e.id = matched_event_id INTO conflicting_events; - RETURN ROW ('append_condition_violated', - matched_position, + RETURN ROW (matched_position, conflicting_events)::en57.append_result; END IF; END LOOP; @@ -147,8 +145,7 @@ INSERT INTO en57.events (id, type, data, meta) FROM unnest(new_events) AS e CROSS JOIN LATERAL unnest(COALESCE(e.tags, ARRAY[]::text[])) AS t (value); - RETURN ROW ('success', - appended_position, + RETURN ROW (appended_position, NULL)::en57.append_result; END; $$; diff --git a/test/test_repository.rb b/test/test_repository.rb index 37ea976..2e57035 100644 --- a/test/test_repository.rb +++ b/test/test_repository.rb @@ -36,7 +36,7 @@ module En57 :exec_params, success_result, [ - "SELECT status, position, conflicting_events FROM en57.append_events($1::en57.event[], $2::jsonb)", + "SELECT position, conflicting_events FROM en57.append_events($1::en57.event[], $2::jsonb)", [expected_events, "{}"], ], ) @@ -80,7 +80,7 @@ module En57 :exec_params, success_result, [ - "SELECT status, position, conflicting_events FROM en57.append_events($1::en57.event[], $2::jsonb)", + "SELECT position, conflicting_events FROM en57.append_events($1::en57.event[], $2::jsonb)", [expected_events, "{}"], ], ) @@ -108,7 +108,7 @@ module En57 :exec_params, success_result, [ - "SELECT status, position, conflicting_events FROM en57.append_events($1::en57.event[], $2::jsonb)", + "SELECT position, conflicting_events FROM en57.append_events($1::en57.event[], $2::jsonb)", [ expected_events, '{"fail_if_events_match":[{"types":["OrderPlaced"],"after":42}]}', @@ -156,7 +156,7 @@ module En57 connection.expect(:exec, nil, ["BEGIN"]) connection.expect(:exec_params, nil) do |sql, params| assert_equal( - "SELECT status, position, conflicting_events FROM en57.append_events($1::en57.event[], $2::jsonb)", + "SELECT position, conflicting_events FROM en57.append_events($1::en57.event[], $2::jsonb)", sql, ) assert_equal( @@ -536,7 +536,7 @@ module En57 end end - def test_append_returns_failure_when_sql_status_is_append_condition_violated + def test_append_returns_failure_when_sql_returns_conflicting_events with_connection do |connection| connection.expect(:exec, nil, ["BEGIN ISOLATION LEVEL SERIALIZABLE"]) connection.expect(:exec_params, failure_result, append_args) @@ -654,12 +654,11 @@ module En57 def record_encoder = @record_encoder ||= PG::TextEncoder::Record.new - def success_result = [{ "status" => "success", "position" => "1" }] + def success_result = [{ "position" => "1", "conflicting_events" => nil }] def failure_result [ { - "status" => "append_condition_violated", "position" => "3", "conflicting_events" => JSON.generate( @@ -693,7 +692,7 @@ module En57 def append_args [ - "SELECT status, position, conflicting_events FROM en57.append_events($1::en57.event[], $2::jsonb)", + "SELECT position, conflicting_events FROM en57.append_events($1::en57.event[], $2::jsonb)", [ array_encoder.encode(append_event_records), '{"fail_if_events_match":[{"types":["OrderPlaced"]}]}',