diff --git a/CHANGELOG.md b/CHANGELOG.md index 2cf97fc..361414a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/lib/github/ds/version.rb b/lib/github/ds/version.rb index 3e42417..044d08f 100644 --- a/lib/github/ds/version.rb +++ b/lib/github/ds/version.rb @@ -1,5 +1,5 @@ module GitHub module DS - VERSION = "0.2.3" + VERSION = "0.2.4" end end diff --git a/lib/github/sql.rb b/lib/github/sql.rb index d631ea7..d5a54ef 100644 --- a/lib/github/sql.rb +++ b/lib/github/sql.rb @@ -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 diff --git a/test/github/sql_test.rb b/test/github/sql_test.rb index 0405cb6..3daaf58 100644 --- a/test/github/sql_test.rb +++ b/test/github/sql_test.rb @@ -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