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
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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`
Expand Down
13 changes: 2 additions & 11 deletions lib/sequent/core/command_record.rb
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
# 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)
self.created_at = command.created_at
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.as_json

# optional attributes (here for historic reasons)
# this should be moved to a configurable CommandSerializer
Expand All @@ -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
Expand Down
27 changes: 2 additions & 25 deletions lib/sequent/core/event_record.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# frozen_string_literal: true

require 'active_record'
require_relative 'sequent_oj'
require_relative '../application_record'

module Sequent
Expand Down Expand Up @@ -46,40 +45,18 @@ 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)
self.aggregate_id = event.aggregate_id
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.as_json

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
Expand Down
15 changes: 2 additions & 13 deletions lib/sequent/core/event_store.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 0 additions & 5 deletions lib/sequent/core/snapshot_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
19 changes: 10 additions & 9 deletions spec/lib/sequent/core/serializes_command_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -39,13 +35,22 @@ 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')
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
Expand All @@ -63,10 +68,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
Expand Down
11 changes: 10 additions & 1 deletion spec/lib/sequent/core/serializes_event_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,21 @@ 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')
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