Skip to content
Closed
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
4 changes: 4 additions & 0 deletions lib/active_graph/transaction.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ def apply_callbacks
after_commit_registry.each(&:call)
end

def clear_callbacks
after_commit_registry.clear
end

private

def after_commit_registry
Expand Down
12 changes: 7 additions & 5 deletions lib/active_graph/transactions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,13 @@ def lock_node(node)
private

def send_transaction(method, **config, &block)
return yield tx if tx
return run_transaction_work(explicit_session, method, **config, &block) if explicit_session&.open?
driver.session do |session|
run_transaction_work(session, method, **config, &block)
end
return run_transaction_work(explicit_session, method, **config, &block) if !tx && explicit_session&.open?
return driver.session { |session| run_transaction_work(session, method, **config, &block) } unless tx

yield tx
rescue ActiveGraph::Rollback
tx.clear_callbacks
false
end

def run_transaction_work(session, method, **config, &block)
Expand Down
18 changes: 18 additions & 0 deletions spec/active_graph/transactions_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -122,4 +122,22 @@
end
end
end

describe 'rollback in nested transaction' do
it 'allows reading model errors after failed update inside an outer transaction' do
stub_node_class('ValidatedStudent') do
property :name
validates :name, presence: true
end

student = ValidatedStudent.create!(name: 'Alice')

errors = ActiveGraph::Base.transaction do
student.update(name: nil)
student.errors.full_messages
Comment on lines +135 to +137

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This may look unnatural, but graphiti performs validations inside the transaction.

end

expect(errors).to include("Name can't be blank")
end
end
end
Loading