To add support for running queries inside an
`ActiveRecord::Base.transaction` scope
This commit is contained in:
miguelff 2017-09-20 17:45:23 +02:00
Родитель 6781dd5272
Коммит 7161e2b791
2 изменённых файлов: 31 добавлений и 0 удалений

Просмотреть файл

@ -67,6 +67,13 @@ module GitHub
end
end
# Public: Run inside a transaction
def self.transaction
ActiveRecord::Base.connection.transaction do
yield
end
end
# Public: Instantiate a literal SQL value.
#
# WARNING: The given value is LITERALLY inserted into your SQL without being

Просмотреть файл

@ -147,6 +147,30 @@ class GitHub::SQLTest < Minitest::Test
refute_includes sql.query, "foo"
end
def test_transaction
GitHub::SQL.run("CREATE TEMPORARY TABLE affected_rows_test (x INT)")
begin
GitHub::SQL.transaction do
GitHub::SQL.run("INSERT INTO affected_rows_test VALUES (1), (2)")
GitHub::SQL.run("INSERT INTO affected_rows_test VALUES (3), (4)")
raise "BOOM"
end
rescue => e
assert_equal 0, GitHub::SQL.new("Select count(*) from affected_rows_test").value
else
fail
end
GitHub::SQL.transaction do
GitHub::SQL.run("INSERT INTO affected_rows_test VALUES (1), (2)")
GitHub::SQL.run("INSERT INTO affected_rows_test VALUES (3), (4)")
end
assert_equal 4, GitHub::SQL.new("Select count(*) from affected_rows_test").value
ensure
GitHub::SQL.run("DROP TABLE affected_rows_test")
end
def test_literal
assert_kind_of GitHub::SQL::Literal, GitHub::SQL::LITERAL("foo")
end