Skip to content
Merged
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
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
# Changelog

## 0.2.4

Additions

* `GitHub::SQL.transaction` was added to allow `GitHub::SQL` queries to run transactionally https://github.com/github/github-ds/pull/24

## 0.2.3

Additions

* github-ds does not use `blank?` anymore hence not depending on `active_support/core_ext/object/blank` https://github.com/github/github-ds/commit/a22c397eaaa00bb441fb4a0ecdf3e371daa9001a
* github-ds does not use `blank?` anymore, thus not depending on `active_support/core_ext/object/blank` https://github.com/github/github-ds/commit/a22c397eaaa00bb441fb4a0ecdf3e371daa9001a

Fixes

Expand Down
2 changes: 1 addition & 1 deletion lib/github/ds/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module GitHub
module DS
VERSION = "0.2.3"
VERSION = "0.2.4"
end
end
7 changes: 7 additions & 0 deletions lib/github/sql.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,13 @@ def inspect
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
Expand Down
24 changes: 24 additions & 0 deletions test/github/sql_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,30 @@ def test_add_unless_empty_does_not_add_to_an_empty_query
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
Expand Down