@@ -7,9 +7,30 @@ class EventRepository
7
7
8
8
def initialize ( model_factory : WithDefaultModels . new , serializer :)
9
9
@serializer = serializer
10
-
11
10
@event_klass , @stream_klass = model_factory . call
12
- @event_klass . include ( SkipJsonSerialization )
11
+ if serializer == NULL && json_data_type?
12
+ warn <<~MSG
13
+ The data or metadata column is of a JSON/B type and expects a JSON string.
14
+
15
+ Yet the repository serializer is configured as #{ serializer } and it would not
16
+ produce the expected JSON string.
17
+
18
+ In ActiveRecord there's an implicit serialization to JSON for JSON/B column types
19
+ that made it work so far. This behaviour is unfortunately also a source of undesired
20
+ double serialization — first in the EventRepository, second in the ActiveRecord.
21
+
22
+ In the past we've advised workarounds that introduced configuration incosistency
23
+ with other data types and serialization formats, i.e. explicitly passing NULL serializer
24
+ just for the JSON/B data types.
25
+
26
+ As of now this special ActiveRecord behaviour is disabled. You should be using JSON
27
+ serializer back again:
28
+
29
+ RubyEventStore::ActiveRecord::EventRepository.new(serializer: JSON)
30
+ MSG
31
+ else
32
+ @event_klass . include ( SkipJsonSerialization )
33
+ end
13
34
@repo_reader = EventRepositoryReader . new ( @event_klass , @stream_klass , serializer )
14
35
@index_violation_detector = IndexViolationDetector . new ( @event_klass . table_name , @stream_klass . table_name )
15
36
end
@@ -164,6 +185,12 @@ def append_to_stream_(records, stream, expected_version)
164
185
end
165
186
add_to_stream ( event_ids , stream , expected_version ) { @event_klass . insert_all! ( hashes ) }
166
187
end
188
+
189
+ private
190
+
191
+ def json_data_type?
192
+ %i[ data metadata ] . any? { |attr | @event_klass . column_for_attribute ( attr ) . type . start_with? ( "json" ) }
193
+ end
167
194
end
168
195
end
169
196
end
0 commit comments