class DatabaseRollbacker::ActiveRecord::Rollbacker

Public Class Methods

new() click to toggle source
# File lib/database_rollbacker/active_record/rollbacker.rb, line 6
def initialize
  @savepoints = []
end

Public Instance Methods

clean() click to toggle source
# File lib/database_rollbacker/active_record/rollbacker.rb, line 36
def clean
  while ActiveRecord::Base.connection.open_transactions > 0 do
    ActiveRecord::Base.connection.rollback_transaction
  end
  @savepoints = []
end
rollback(savepoint_name) click to toggle source
# File lib/database_rollbacker/active_record/rollbacker.rb, line 19
def rollback(savepoint_name)
  savepoint = fetch_savepoint(savepoint_name)
  raise ArgumentError.new("savepoint not found") unless savepoint.present?
  while ActiveRecord::Base.connection.open_transactions > 0 do
    break if ActiveRecord::Base.connection.open_transactions == savepoint.savepoint_id
    ActiveRecord::Base.connection.rollback_transaction
  end
  while @savepoints.present? do
    if @savepoints.last.name == savepoint.name
      @savepoints.pop
      break
    else
      @savepoints.pop
    end
  end
end
save(savepoint_name) click to toggle source
# File lib/database_rollbacker/active_record/rollbacker.rb, line 10
def save(savepoint_name)
  raise ArgumentError.new('duplicate savepoint name') if savepoint_exist?(savepoint_name)
  transaction_number = ActiveRecord::Base.connection.open_transactions
  ActiveRecord::Base.connection.begin_transaction(joinable: false)
  @savepoints.push DatabaseRollbacker::Savepoint.new(
    savepoint_name,
    transaction_number)
end

Private Instance Methods

fetch_savepoint(savepoint_name) click to toggle source
# File lib/database_rollbacker/active_record/rollbacker.rb, line 49
def fetch_savepoint(savepoint_name)
  @savepoints.find { |s| s.name == savepoint_name }
end
savepoint_exist?(savepoint_name) click to toggle source
# File lib/database_rollbacker/active_record/rollbacker.rb, line 45
def savepoint_exist?(savepoint_name)
  @savepoints.any? { |s| s.name == savepoint_name }
end