Permalink
Browse files

change the savepoint interface to allow passing the name.

  • Loading branch information...
senny committed Sep 30, 2013
1 parent 47a19eb commit 032998ad7460c59916b8268467251d78c6cd18b7
@@ -1,3 +1,8 @@
+* `create_savepoint`, `rollback_to_savepoint` and `release_savepoint` accept
+ a savepoint name.
+
+ *Yves Senn*
+
* Make `next_migration_number` accessible for third party generators.
*Yves Senn*
@@ -395,13 +395,13 @@ def open_transactions
@transaction.number
end
- def create_savepoint
+ def create_savepoint(name = nil)
end
- def rollback_to_savepoint
+ def rollback_to_savepoint(name = nil)
end
- def release_savepoint
+ def release_savepoint(name = nil)
end
def case_sensitive_modifier(node)
@@ -340,16 +340,16 @@ def rollback_db_transaction #:nodoc:
# Transactions aren't supported
end
- def create_savepoint
- execute("SAVEPOINT #{current_savepoint_name}")
+ def create_savepoint(name = current_savepoint_name)
+ execute("SAVEPOINT #{name}")
end
- def rollback_to_savepoint
- execute("ROLLBACK TO SAVEPOINT #{current_savepoint_name}")
+ def rollback_to_savepoint(name = current_savepoint_name)
+ execute("ROLLBACK TO SAVEPOINT #{name}")
end
- def release_savepoint
- execute("RELEASE SAVEPOINT #{current_savepoint_name}")
+ def release_savepoint(name = current_savepoint_name)
+ execute("RELEASE SAVEPOINT #{name}")
end
# In the simple case, MySQL allows us to place JOINs directly into the UPDATE
@@ -219,16 +219,16 @@ def rollback_db_transaction
execute "ROLLBACK"
end
- def create_savepoint
- execute("SAVEPOINT #{current_savepoint_name}")
+ def create_savepoint(name = current_savepoint_name)
+ execute("SAVEPOINT #{name}")
end
- def rollback_to_savepoint
- execute("ROLLBACK TO SAVEPOINT #{current_savepoint_name}")
+ def rollback_to_savepoint(name = current_savepoint_name)
+ execute("ROLLBACK TO SAVEPOINT #{name}")
end
- def release_savepoint
- execute("RELEASE SAVEPOINT #{current_savepoint_name}")
+ def release_savepoint(name = current_savepoint_name)
+ execute("RELEASE SAVEPOINT #{name}")
end
end
end
@@ -351,16 +351,16 @@ def select_rows(sql, name = nil)
exec_query(sql, name).rows
end
- def create_savepoint
- execute("SAVEPOINT #{current_savepoint_name}")
+ def create_savepoint(name = current_savepoint_name)
+ execute("SAVEPOINT #{name}")
end
- def rollback_to_savepoint
- execute("ROLLBACK TO SAVEPOINT #{current_savepoint_name}")
+ def rollback_to_savepoint(name = current_savepoint_name)
+ execute("ROLLBACK TO SAVEPOINT #{name}")
end
- def release_savepoint
- execute("RELEASE SAVEPOINT #{current_savepoint_name}")
+ def release_savepoint(name = current_savepoint_name)
+ execute("RELEASE SAVEPOINT #{name}")
end
def begin_db_transaction #:nodoc:
@@ -375,6 +375,36 @@ def test_many_savepoints
assert_equal "Three", @three
end if Topic.connection.supports_savepoints?
+ def test_using_named_savepoints
+ Topic.transaction do
+ @first.approved = true
+ @first.save!
+ Topic.connection.create_savepoint("first")
+
+ @first.approved = false
+ @first.save!
+ Topic.connection.rollback_to_savepoint("first")
+ assert @first.reload.approved?
+
+ @first.approved = false
+ @first.save!
+ Topic.connection.release_savepoint("first")
+ assert_not @first.reload.approved?
+ end
+ end if Topic.connection.supports_savepoints?
+
+ def test_releasing_named_savepoints
+ Topic.transaction do
+ Topic.connection.create_savepoint("another")
+ Topic.connection.release_savepoint("another")
+
+ # The savepoint is now gone and we can't remove it again.
+ assert_raises(ActiveRecord::StatementInvalid) do
+ Topic.connection.release_savepoint("another")
+ end
+ end
+ end
+
def test_rollback_when_commit_raises
Topic.connection.expects(:begin_db_transaction)
Topic.connection.expects(:commit_db_transaction).raises('OH NOES')

0 comments on commit 032998a

Please sign in to comment.