From 5d59dfbf1b4c8ebbb78fd30502987e863f63b61f Mon Sep 17 00:00:00 2001 From: Erik Rozendaal Date: Wed, 4 Nov 2026 12:35:28 +0100 Subject: [PATCH 1/2] Remove the support for event, command and snapshot JSON in text columns Since Sequent 8 the event store keeps the JSON of events, commands and snapshots in jsonb columns, and the event_records and command_records views are part of Sequent's schema, so the branches for JSON in a text column were no longer used. The event store now deserializes loaded events directly from their JSON, instead of assigning it to a new event_record_class instance first. --- CHANGELOG.md | 8 ++++++ lib/sequent/core/command_record.rb | 13 ++------- lib/sequent/core/event_record.rb | 27 ++----------------- lib/sequent/core/event_store.rb | 15 ++--------- lib/sequent/core/snapshot_record.rb | 5 ---- .../sequent/core/serializes_command_spec.rb | 10 +------ .../lib/sequent/core/serializes_event_spec.rb | 2 +- 7 files changed, 16 insertions(+), 64 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 66f34273f..b234bbd5b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,13 @@ # Changelog 9.x +- Breaking: the support for event, command and snapshot JSON in text + columns is removed, since the event store keeps these in jsonb + columns since Sequent 8. `serialize_json?` and + `EventRecord.serialize_to_json` are removed, `SerializesEvent` and + `SerializesCommand` store the JSON as a hash, and the event store + deserializes loaded events directly from their JSON instead of using + the `event_record_class`, which makes loading events about 40% faster. + - A record class that includes `Sequent::Core::ProjectionRecord` can only be written by projectors, unless writing is allowed using `Sequent::Core::ProjectionWrites`. The `have_no_write_callbacks` diff --git a/lib/sequent/core/command_record.rb b/lib/sequent/core/command_record.rb index ae647eb18..18bdf07e2 100644 --- a/lib/sequent/core/command_record.rb +++ b/lib/sequent/core/command_record.rb @@ -1,14 +1,12 @@ # frozen_string_literal: true require 'active_record' -require_relative 'sequent_oj' module Sequent module Core module SerializesCommand def command - args = serialize_json? ? Sequent::Core::Oj.strict_load(command_json) : command_json - Class.const_get(command_type).deserialize_from_json(args) + Class.const_get(command_type).deserialize_from_json(command_json) end def command=(command) @@ -16,7 +14,7 @@ def command=(command) self.aggregate_id = command.aggregate_id if command.respond_to? :aggregate_id self.user_id = command.user_id if command.respond_to? :user_id self.command_type = command.class.name - self.command_json = serialize_json? ? Sequent::Core::Oj.dump(command.attributes) : command.attributes + self.command_json = command.attributes # optional attributes (here for historic reasons) # this should be moved to a configurable CommandSerializer @@ -29,13 +27,6 @@ def command=(command) private - def serialize_json? - return true unless self.class.respond_to? :columns_hash - - json_column_type = self.class.columns_hash['command_json'].sql_type_metadata.type - %i[json jsonb].exclude? json_column_type - end - def serialize_attribute?(command, attribute) [self, command].all? { |obj| obj.respond_to?(attribute) } end diff --git a/lib/sequent/core/event_record.rb b/lib/sequent/core/event_record.rb index e98b66e68..d39370c6d 100644 --- a/lib/sequent/core/event_record.rb +++ b/lib/sequent/core/event_record.rb @@ -1,7 +1,6 @@ # frozen_string_literal: true require 'active_record' -require_relative 'sequent_oj' require_relative '../application_record' module Sequent @@ -46,8 +45,7 @@ def self.after_serialization(event_record, event) module SerializesEvent def event - payload = serialize_json? ? Sequent::Core::Oj.strict_load(event_json) : event_json - Class.const_get(event_type).deserialize_from_json(payload) + Class.const_get(event_type).deserialize_from_json(event_json) end def event=(event) @@ -55,31 +53,10 @@ def event=(event) self.sequence_number = event.sequence_number self.event_type = event.class.name self.created_at = event.created_at - self.event_json = serialize_json? ? self.class.serialize_to_json(event) : event.attributes + self.event_json = event.attributes Sequent.configuration.event_record_hooks_class.after_serialization(self, event) end - - module ClassMethods - def serialize_to_json(event) - Sequent::Core::Oj.dump(event) - end - - def serialize_json? - return true unless respond_to? :columns_hash - - json_column_type = columns_hash['event_json'].sql_type_metadata.type - %i[json jsonb].exclude? json_column_type - end - end - - def self.included(host_class) - host_class.extend(ClassMethods) - end - - def serialize_json? - self.class.serialize_json? - end end class EventRecord < Sequent::ApplicationRecord diff --git a/lib/sequent/core/event_store.rb b/lib/sequent/core/event_store.rb index 672050052..cacfaf7c2 100644 --- a/lib/sequent/core/event_store.rb +++ b/lib/sequent/core/event_store.rb @@ -339,19 +339,8 @@ def query_events(aggregate_ids, use_snapshots = true, load_until = nil) end def deserialize_event(event_hash) - should_serialize_json = Sequent.configuration.event_record_class.serialize_json? - record = Sequent.configuration.event_record_class.new - record.event_type = event_hash.fetch('event_type') - record.event_json = - if should_serialize_json - event_hash.fetch('event_json') - else - # When the column type is JSON or JSONB the event record - # class expects the JSON to be deserialized into a hash - # already. - Sequent::Core::Oj.strict_load(event_hash.fetch('event_json')) - end - record.event + Class.const_get(event_hash.fetch('event_type')) + .deserialize_from_json(Sequent::Core::Oj.strict_load(event_hash.fetch('event_json'))) rescue StandardError raise DeserializeEventError, event_hash end diff --git a/lib/sequent/core/snapshot_record.rb b/lib/sequent/core/snapshot_record.rb index 6d6968b7c..b02a0b5d7 100644 --- a/lib/sequent/core/snapshot_record.rb +++ b/lib/sequent/core/snapshot_record.rb @@ -34,11 +34,6 @@ def event_json def event_json=(json) self.snapshot_json = json end - - def serialize_json? - json_column_type = self.class.columns_hash['snapshot_json'].sql_type_metadata.type - %i[json jsonb].exclude? json_column_type - end end end end diff --git a/spec/lib/sequent/core/serializes_command_spec.rb b/spec/lib/sequent/core/serializes_command_spec.rb index f61b252b8..f8f6dee76 100644 --- a/spec/lib/sequent/core/serializes_command_spec.rb +++ b/spec/lib/sequent/core/serializes_command_spec.rb @@ -14,10 +14,6 @@ class RecordMock :command_json, :event_aggregate_id, :event_sequence_number - - def serialize_json? - true - end end class RecordCommand < Sequent::Core::Command @@ -39,7 +35,7 @@ class RecordCommand < Sequent::Core::Command command.valid? record = RecordMock.new record.command = command - payload = Sequent::Core::Oj.strict_load(record.command_json) + payload = record.command_json expect(payload).to have_key('aggregate_id') expect(payload).to have_key('value') expect(payload['value']).to_not have_key('errors') @@ -63,10 +59,6 @@ def initialize(aggregate_id, created_at, user_id, command_type, command_json) @command_type = command_type @command_json = command_json end - - def serialize_json? - true - end end let(:record) do diff --git a/spec/lib/sequent/core/serializes_event_spec.rb b/spec/lib/sequent/core/serializes_event_spec.rb index 576957152..9a7012b1c 100644 --- a/spec/lib/sequent/core/serializes_event_spec.rb +++ b/spec/lib/sequent/core/serializes_event_spec.rb @@ -33,7 +33,7 @@ class RecordEvent < Sequent::Core::Event value_object.valid? record = RecordMock.new record.event = event - payload = Sequent::Core::Oj.strict_load(record.event_json) + payload = record.event_json expect(payload).to have_key('aggregate_id') expect(payload).to have_key('sequence_number') expect(payload).to have_key('value') From 58a8bb44c418fa038aae2a888586b4970272a1f1 Mon Sep 17 00:00:00 2001 From: Erik Rozendaal Date: Wed, 4 Nov 2026 17:54:50 +0100 Subject: [PATCH 2/2] Store the JSON of an event or command in records without a JSON column A record that includes SerializesEvent or SerializesCommand without being an ActiveRecord model with a json column, like one using attr_accessor, kept the Time values of the attributes, and could not load the event or command again. as_json gives the values they are loaded from; ActiveRecord's json type casts them the same way. --- lib/sequent/core/command_record.rb | 2 +- lib/sequent/core/event_record.rb | 2 +- spec/lib/sequent/core/serializes_command_spec.rb | 9 +++++++++ spec/lib/sequent/core/serializes_event_spec.rb | 9 +++++++++ 4 files changed, 20 insertions(+), 2 deletions(-) diff --git a/lib/sequent/core/command_record.rb b/lib/sequent/core/command_record.rb index 18bdf07e2..0d14006cf 100644 --- a/lib/sequent/core/command_record.rb +++ b/lib/sequent/core/command_record.rb @@ -14,7 +14,7 @@ def command=(command) self.aggregate_id = command.aggregate_id if command.respond_to? :aggregate_id self.user_id = command.user_id if command.respond_to? :user_id self.command_type = command.class.name - self.command_json = command.attributes + self.command_json = command.as_json # optional attributes (here for historic reasons) # this should be moved to a configurable CommandSerializer diff --git a/lib/sequent/core/event_record.rb b/lib/sequent/core/event_record.rb index d39370c6d..00a451d34 100644 --- a/lib/sequent/core/event_record.rb +++ b/lib/sequent/core/event_record.rb @@ -53,7 +53,7 @@ def event=(event) self.sequence_number = event.sequence_number self.event_type = event.class.name self.created_at = event.created_at - self.event_json = event.attributes + self.event_json = event.as_json Sequent.configuration.event_record_hooks_class.after_serialization(self, event) end diff --git a/spec/lib/sequent/core/serializes_command_spec.rb b/spec/lib/sequent/core/serializes_command_spec.rb index f8f6dee76..8ac94eafe 100644 --- a/spec/lib/sequent/core/serializes_command_spec.rb +++ b/spec/lib/sequent/core/serializes_command_spec.rb @@ -42,6 +42,15 @@ class RecordCommand < Sequent::Core::Command expect(payload['value']).to have_key('value') end + it 'loads the command that is stored' do + command = RecordCommand.new(aggregate_id: '1', created_at: Time.utc(2026, 1, 2, 3, 4, 5.123r)) + record = RecordMock.new + record.command = command + + expect(record.command.created_at).to eq command.created_at + expect(record.command.aggregate_id).to eq '1' + end + describe 'optional fields' do class MyRecord include Sequent::Core::SerializesCommand diff --git a/spec/lib/sequent/core/serializes_event_spec.rb b/spec/lib/sequent/core/serializes_event_spec.rb index 9a7012b1c..9d6af385e 100644 --- a/spec/lib/sequent/core/serializes_event_spec.rb +++ b/spec/lib/sequent/core/serializes_event_spec.rb @@ -40,5 +40,14 @@ class RecordEvent < Sequent::Core::Event expect(payload['value']).to_not have_key('errors') expect(payload['value']).to have_key('value') end + + it 'loads the event that is stored' do + event = RecordEvent.new(aggregate_id: '1', sequence_number: 2, created_at: Time.utc(2026, 1, 2, 3, 4, 5.123r)) + record = RecordMock.new + record.event = event + + expect(record.event).to eq event + expect(record.event.created_at).to eq event.created_at + end end end