Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
desheikh committed Feb 19, 2024
1 parent f25c06e commit 9436ab2
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 11 deletions.
20 changes: 15 additions & 5 deletions lib/eventsimple/event.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ def drives_events_for(aggregate_klass, aggregate_id:, events_namespace: nil)
self.inheritance_column = :type
self.store_full_sti_class = false

# disable automatic timestamp updates
self.record_timestamps = false

attribute :metadata, MetadataType.new
attr_writer :skip_dispatcher
attr_writer :skip_apply_check
Expand All @@ -39,7 +42,8 @@ def drives_events_for(aggregate_klass, aggregate_id:, events_namespace: nil)

before_validation :extend_validation
after_validation :perform_transition_checks
before_create :apply_and_persist
before_create :apply_aggregate_id
after_create :persist_aggregate
after_create :dispatch

include InstanceMethods
Expand Down Expand Up @@ -84,15 +88,21 @@ def extend_validation
self.aggregate = aggregate.extend(validate_form) if validate_form
end

# The aggregate canonical identifier might be set via an initial event so we need to
# apply the event to the aggregate and set the aggregate_id.
def apply_aggregate_id
apply(aggregate)

self.aggregate = aggregate
aggregate.restore_attributes
end

# Apply the transformation to the aggregate and save it.
def apply_and_persist
def persist_aggregate
apply_timestamps(aggregate)
apply(aggregate)

# Persist!
aggregate.save!

self.aggregate = aggregate
end

def dispatch
Expand Down
5 changes: 3 additions & 2 deletions lib/eventsimple/generators/templates/create_events.erb
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@ class Create<%= model_name.camelize %>Events < ActiveRecord::Migration[7.0]
t.json :data, null: false, default: {}
t.json :metadata, null: false, default: {}

t.timestamps
t.datetime :created_at, null: false, default: -> { 'CURRENT_TIMESTAMP' }
t.datetime, updated_at, null: false, default: -> { 'CURRENT_TIMESTAMP' }

t.index :idempotency_key, unique: true
end

# Enables optimistic locking on the evented table
add_column :<%= model_name.underscore.pluralize %>, :lock_version, :integer
add_column :<%= model_name.underscore.pluralize %>, :lock_version, :integer, default: 0, null: false
end
end
3 changes: 2 additions & 1 deletion spec/dummy/db/migrate/20220917150839_create_user_events.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ def change
t.json :data, null: false, default: {}
t.json :metadata, null: false, default: {}

t.timestamps
t.datetime :created_at, null: false, default: -> { 'CURRENT_TIMESTAMP' }
t.datetime :updated_at, null: false, default: -> { 'CURRENT_TIMESTAMP' }

t.index :idempotency_key, unique: true

Expand Down
6 changes: 3 additions & 3 deletions spec/dummy/db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema[7.0].define(version: 2022_09_17_150839) do
ActiveRecord::Schema[7.1].define(version: 2022_09_17_150839) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"

Expand All @@ -20,8 +20,8 @@
t.string "type", null: false
t.json "data", default: {}, null: false
t.json "metadata", default: {}, null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.datetime "created_at", default: -> { "CURRENT_TIMESTAMP" }, null: false
t.datetime "updated_at", default: -> { "CURRENT_TIMESTAMP" }, null: false
t.integer "eventide_position_id"
t.index ["aggregate_id"], name: "index_user_events_on_aggregate_id"
t.index ["eventide_position_id"], name: "index_user_events_on_eventide_position_id", unique: true
Expand Down

0 comments on commit 9436ab2

Please sign in to comment.