Используя шаблон Бруно, затем добавив индикатор статуса транзакции:
def transaction(&block)
raise ArgumentError, "No block was given" unless block_given?
begin
raw_query("BEGIN")
yield
raw_query("COMMIT")
return true # Successful Transaction
rescue
raw_query("ROLLBACK")
return false # Failed Transaction
end
end
Взаимодействие с #transaction:
def run_queries(queries)
raise ArgumentError, "Invalid Queries Argument: #{queries}" unless queries.respond_to?(:each)
success = transaction do
queries.each do |q|
raw_query(q)
end
end
raise RuntimeError, "Transaction Failed for Queries: #{queries}" unless success
end