зеркало из https://github.com/github/github-ds.git
Adds `SQL.transaction`
To add support for running queries inside an `ActiveRecord::Base.transaction` scope
This commit is contained in:
Родитель
6781dd5272
Коммит
7161e2b791
|
@ -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
|
||||
|
|
Загрузка…
Ссылка в новой задаче