diff --git a/CHANGELOG.md b/CHANGELOG.md index 1c29c36fc..9fa504ec0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## Unreleased +## 1.2.1 - 2023-11-18 +### Changed +- Allow timestamp data in events to override default timestamps update. +- This allows us to create temporal events for example in the case of snapshots. + ## 1.2.0 - 2023-09-26 ### Changed - Fix issue where the reactor worker might not work correctly for models not inheriting from ApplicationRecord. diff --git a/Gemfile.lock b/Gemfile.lock index f26e810c5..896a5bdba 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,7 +1,7 @@ PATH remote: . specs: - eventsimple (1.2.0) + eventsimple (1.2.1) dry-struct (~> 1.6) dry-types (~> 1.7) pg (~> 1.4) @@ -89,7 +89,7 @@ GEM coderay (1.1.3) concurrent-ruby (1.2.2) crass (1.0.6) - date (3.3.3) + date (3.3.4) diff-lcs (1.5.0) dry-core (1.0.1) concurrent-ruby (~> 1.0) @@ -158,12 +158,12 @@ GEM mini_mime (1.1.5) minitest (5.20.0) nenv (0.3.0) - net-imap (0.3.7) + net-imap (0.4.5) date net-protocol net-pop (0.1.2) net-protocol - net-protocol (0.2.1) + net-protocol (0.2.2) timeout net-smtp (0.4.0) net-protocol @@ -300,7 +300,7 @@ GEM lint_roller (~> 1.0) rubocop-rails (~> 2.20.2) thor (1.2.2) - timeout (0.4.0) + timeout (0.4.1) treetop (1.6.12) polyglot (~> 0.3) tzinfo (2.0.6) diff --git a/README.md b/README.md index a93293d23..adff70af3 100644 --- a/README.md +++ b/README.md @@ -176,8 +176,6 @@ module UserComponent def apply(user) user.canonical_id = data.canonical_id user.email = data.email - - user end end end diff --git a/lib/eventsimple/entity.rb b/lib/eventsimple/entity.rb index b4b506f54..03fb9ea98 100644 --- a/lib/eventsimple/entity.rb +++ b/lib/eventsimple/entity.rb @@ -35,8 +35,8 @@ def reproject(at: nil) assign_attributes(self.class.column_defaults.except(*ignore_props)) event_history.each do |event| - event.apply(self) event.apply_timestamps(self) + event.apply(self) end self diff --git a/lib/eventsimple/event.rb b/lib/eventsimple/event.rb index 40416f529..4c3361869 100644 --- a/lib/eventsimple/event.rb +++ b/lib/eventsimple/event.rb @@ -86,8 +86,8 @@ def extend_validation # Apply the transformation to the aggregate and save it. def apply_and_persist - apply(aggregate) apply_timestamps(aggregate) + apply(aggregate) # Persist! aggregate.save! diff --git a/lib/eventsimple/support/spec_helpers.rb b/lib/eventsimple/support/spec_helpers.rb index 261117a8f..5b7f97eea 100644 --- a/lib/eventsimple/support/spec_helpers.rb +++ b/lib/eventsimple/support/spec_helpers.rb @@ -45,3 +45,5 @@ end end end + +RSpec::Matchers.define_negated_matcher(:not_change, :change) diff --git a/lib/eventsimple/version.rb b/lib/eventsimple/version.rb index 83dadb83d..5c112833d 100644 --- a/lib/eventsimple/version.rb +++ b/lib/eventsimple/version.rb @@ -1,5 +1,5 @@ # frozen_string_literal: true module Eventsimple - VERSION = '1.2.0' + VERSION = '1.2.1' end diff --git a/spec/dummy/app/components/user_component/events/created.rb b/spec/dummy/app/components/user_component/events/created.rb index bb9758648..54655b259 100644 --- a/spec/dummy/app/components/user_component/events/created.rb +++ b/spec/dummy/app/components/user_component/events/created.rb @@ -19,7 +19,6 @@ def apply(user) user.canonical_id = data.canonical_id user.username = data.username user.email = data.email - user end end end diff --git a/spec/dummy/app/components/user_component/events/deleted.rb b/spec/dummy/app/components/user_component/events/deleted.rb index 5c76a684d..967d34a36 100644 --- a/spec/dummy/app/components/user_component/events/deleted.rb +++ b/spec/dummy/app/components/user_component/events/deleted.rb @@ -12,8 +12,6 @@ def apply(user) user.created_at ||= created_at user.updated_at = created_at - - user end end end diff --git a/spec/dummy/app/components/user_component/events/override_timestamp.rb b/spec/dummy/app/components/user_component/events/override_timestamp.rb new file mode 100644 index 000000000..601a3535b --- /dev/null +++ b/spec/dummy/app/components/user_component/events/override_timestamp.rb @@ -0,0 +1,21 @@ +# frozen_string_literal: true + +module UserComponent + module Events + class OverrideTimestamp < UserEvent + attribute :data, Eventsimple::DataType.new(self) + + class Message < Eventsimple::Message + attribute :canonical_id, DryTypes::Strict::String + attribute :created_at, DryTypes::JSON::Time + attribute :updated_at, DryTypes::JSON::Time + end + + def apply(user) + user.canonical_id = data.canonical_id + user.created_at = data.created_at + user.updated_at = data.updated_at + end + end + end +end diff --git a/spec/dummy/app/components/user_component/events/rescued_invalid_transition.rb b/spec/dummy/app/components/user_component/events/rescued_invalid_transition.rb index 58434c4ca..3d444ae11 100644 --- a/spec/dummy/app/components/user_component/events/rescued_invalid_transition.rb +++ b/spec/dummy/app/components/user_component/events/rescued_invalid_transition.rb @@ -19,7 +19,6 @@ def apply(user) user.canonical_id = data.canonical_id user.username = 'test' user.email = 'test@example.com' - user end end end diff --git a/spec/dummy/app/components/user_component/events/rescued_invalid_transition_with_reraise.rb b/spec/dummy/app/components/user_component/events/rescued_invalid_transition_with_reraise.rb index 3341bc2f2..b69f8be03 100644 --- a/spec/dummy/app/components/user_component/events/rescued_invalid_transition_with_reraise.rb +++ b/spec/dummy/app/components/user_component/events/rescued_invalid_transition_with_reraise.rb @@ -21,7 +21,6 @@ def apply(user) user.canonical_id = data.canonical_id user.username = 'test' user.email = 'test@example.com' - user end end end diff --git a/spec/dummy/app/components/user_component/events/updated.rb b/spec/dummy/app/components/user_component/events/updated.rb index c1153fb18..081081a07 100644 --- a/spec/dummy/app/components/user_component/events/updated.rb +++ b/spec/dummy/app/components/user_component/events/updated.rb @@ -15,7 +15,6 @@ def can_apply?(user) def apply(user) user.email = data.email - user end end end diff --git a/spec/dummy/db/migrate/20220917150826_create_users.rb b/spec/dummy/db/migrate/20220917150826_create_users.rb index be1fbce1f..cb6edbf2c 100644 --- a/spec/dummy/db/migrate/20220917150826_create_users.rb +++ b/spec/dummy/db/migrate/20220917150826_create_users.rb @@ -3,7 +3,7 @@ def change create_table :users do |t| t.string :canonical_id, null: false - t.string :username, null: false + t.string :username t.string :email t.integer :lock_version diff --git a/spec/dummy/db/schema.rb b/spec/dummy/db/schema.rb index aa0ed4328..448630da2 100644 --- a/spec/dummy/db/schema.rb +++ b/spec/dummy/db/schema.rb @@ -30,7 +30,7 @@ create_table "users", force: :cascade do |t| t.string "canonical_id", null: false - t.string "username", null: false + t.string "username" t.string "email" t.integer "lock_version" t.datetime "deleted_at" diff --git a/spec/dummy/spec/components/user_component/events/override_timestamp_spec.rb b/spec/dummy/spec/components/user_component/events/override_timestamp_spec.rb new file mode 100644 index 000000000..65160c5c1 --- /dev/null +++ b/spec/dummy/spec/components/user_component/events/override_timestamp_spec.rb @@ -0,0 +1,29 @@ +# frozen_string_literal: true + +RSpec.describe UserComponent::Events::OverrideTimestamp do + describe '#create' do + subject(:create_event) { event.save } + + let(:canonical_id) { SecureRandom.uuid } + + let(:user) { User.new } + let(:event) do + described_class.new( + user: user, + data: { + canonical_id: canonical_id, + created_at: Time.new(2023, 1, 1), + updated_at: Time.new(2023, 1, 1), + }, + ) + end + + it 'updates the user properties' do + create_event + + expect(user.canonical_id).to eq(event.data.canonical_id) + expect(user.created_at).to eq(event.data.created_at) + expect(user.updated_at).to eq(event.data.updated_at) + end + end +end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 47d898964..065d10d13 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -20,7 +20,6 @@ ENV['RAILS_ROOT'] ||= "#{File.dirname(__FILE__)}../../../spec/dummy" require 'rspec/rails' - RSpec::Matchers.define_negated_matcher(:not_change, :change) ActiveRecord::Migration.maintain_test_schema!