Skip to content

Commit 37a05a0

Browse files
committed
Simplify append result detection
- Remove the redundant status field from append results because the presence of conflicting_events already distinguishes failures from successful appends. - Keep the repository result mapping aligned with the smaller database result shape.
1 parent f42a576 commit 37a05a0

4 files changed

Lines changed: 17 additions & 25 deletions

File tree

db/schema/0.1.0.sql

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ CREATE TYPE en57.event AS (
2727
);
2828

2929
CREATE TYPE en57.append_result AS (
30-
status text,
3130
"position" bigint,
3231
conflicting_events jsonb
3332
);
@@ -117,8 +116,7 @@ BEGIN
117116
e.id = matched_event_id
118117
INTO
119118
conflicting_events;
120-
RETURN ROW ('append_condition_violated',
121-
matched_position,
119+
RETURN ROW (matched_position,
122120
conflicting_events)::en57.append_result;
123121
END IF;
124122
END LOOP;
@@ -147,8 +145,7 @@ INSERT INTO en57.events (id, type, data, meta)
147145
FROM
148146
unnest(new_events) AS e
149147
CROSS JOIN LATERAL unnest(COALESCE(e.tags, ARRAY[]::text[])) AS t (value);
150-
RETURN ROW ('success',
151-
appended_position,
148+
RETURN ROW (appended_position,
152149
NULL)::en57.append_result;
153150
END;
154151
$$;

lib/en57/repository.rb

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ def append(events, fail_if:)
3535
] = fail_if_events_match unless fail_if_events_match.empty?
3636

3737
statement =
38-
"SELECT status, position, conflicting_events FROM en57.append_events($1::en57.event[], $2::jsonb)"
38+
"SELECT position, conflicting_events FROM en57.append_events($1::en57.event[], $2::jsonb)"
3939
params = [
4040
@array_encoder.encode(event_records),
4141
JSON.generate(append_condition),
@@ -54,19 +54,18 @@ def append(events, fail_if:)
5454
end
5555
end
5656

57-
case row.first.fetch("status")
58-
when "success"
59-
Success.new(
60-
position: row.first.fetch("position").then { Integer(it) },
61-
)
62-
when "append_condition_violated"
57+
if row.first.fetch("conflicting_events")
6358
Failure.new(
6459
position: row.first.fetch("position").then { Integer(it) },
6560
conflicting_events:
6661
JSON
6762
.parse(row.first.fetch("conflicting_events"))
6863
.map { deserialize_event(it) },
6964
)
65+
else
66+
Success.new(
67+
position: row.first.fetch("position").then { Integer(it) },
68+
)
7069
end
7170
rescue @adapter.serialization_error
7271
if attempts_remaining.positive?

test/pg_regress/expected/001_schema.out

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ CREATE TYPE en57.event AS (
2727
);
2828

2929
CREATE TYPE en57.append_result AS (
30-
status text,
3130
"position" bigint,
3231
conflicting_events jsonb
3332
);
@@ -117,8 +116,7 @@ BEGIN
117116
e.id = matched_event_id
118117
INTO
119118
conflicting_events;
120-
RETURN ROW ('append_condition_violated',
121-
matched_position,
119+
RETURN ROW (matched_position,
122120
conflicting_events)::en57.append_result;
123121
END IF;
124122
END LOOP;
@@ -147,8 +145,7 @@ INSERT INTO en57.events (id, type, data, meta)
147145
FROM
148146
unnest(new_events) AS e
149147
CROSS JOIN LATERAL unnest(COALESCE(e.tags, ARRAY[]::text[])) AS t (value);
150-
RETURN ROW ('success',
151-
appended_position,
148+
RETURN ROW (appended_position,
152149
NULL)::en57.append_result;
153150
END;
154151
$$;

test/test_repository.rb

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ def test_append_without_fail_if_uses_plain_transaction
3636
:exec_params,
3737
success_result,
3838
[
39-
"SELECT status, position, conflicting_events FROM en57.append_events($1::en57.event[], $2::jsonb)",
39+
"SELECT position, conflicting_events FROM en57.append_events($1::en57.event[], $2::jsonb)",
4040
[expected_events, "{}"],
4141
],
4242
)
@@ -80,7 +80,7 @@ def test_append_persists_empty_event_data_as_null
8080
:exec_params,
8181
success_result,
8282
[
83-
"SELECT status, position, conflicting_events FROM en57.append_events($1::en57.event[], $2::jsonb)",
83+
"SELECT position, conflicting_events FROM en57.append_events($1::en57.event[], $2::jsonb)",
8484
[expected_events, "{}"],
8585
],
8686
)
@@ -108,7 +108,7 @@ def test_append_passes_fail_if_and_after_conditions
108108
:exec_params,
109109
success_result,
110110
[
111-
"SELECT status, position, conflicting_events FROM en57.append_events($1::en57.event[], $2::jsonb)",
111+
"SELECT position, conflicting_events FROM en57.append_events($1::en57.event[], $2::jsonb)",
112112
[
113113
expected_events,
114114
'{"fail_if_events_match":[{"types":["OrderPlaced"],"after":42}]}',
@@ -156,7 +156,7 @@ def test_append_rolls_back_transaction_on_pg_failure
156156
connection.expect(:exec, nil, ["BEGIN"])
157157
connection.expect(:exec_params, nil) do |sql, params|
158158
assert_equal(
159-
"SELECT status, position, conflicting_events FROM en57.append_events($1::en57.event[], $2::jsonb)",
159+
"SELECT position, conflicting_events FROM en57.append_events($1::en57.event[], $2::jsonb)",
160160
sql,
161161
)
162162
assert_equal(
@@ -536,7 +536,7 @@ def test_read_events_filtered_by_type
536536
end
537537
end
538538

539-
def test_append_returns_failure_when_sql_status_is_append_condition_violated
539+
def test_append_returns_failure_when_sql_returns_conflicting_events
540540
with_connection do |connection|
541541
connection.expect(:exec, nil, ["BEGIN ISOLATION LEVEL SERIALIZABLE"])
542542
connection.expect(:exec_params, failure_result, append_args)
@@ -654,12 +654,11 @@ def array_encoder = @array_encoder ||= PG::TextEncoder::Array.new
654654

655655
def record_encoder = @record_encoder ||= PG::TextEncoder::Record.new
656656

657-
def success_result = [{ "status" => "success", "position" => "1" }]
657+
def success_result = [{ "position" => "1", "conflicting_events" => nil }]
658658

659659
def failure_result
660660
[
661661
{
662-
"status" => "append_condition_violated",
663662
"position" => "3",
664663
"conflicting_events" =>
665664
JSON.generate(
@@ -693,7 +692,7 @@ def append_event_records
693692

694693
def append_args
695694
[
696-
"SELECT status, position, conflicting_events FROM en57.append_events($1::en57.event[], $2::jsonb)",
695+
"SELECT position, conflicting_events FROM en57.append_events($1::en57.event[], $2::jsonb)",
697696
[
698697
array_encoder.encode(append_event_records),
699698
'{"fail_if_events_match":[{"types":["OrderPlaced"]}]}',

0 commit comments

Comments
 (0)