Permalink
Browse files

Added savepoint strategy to DatabaseCleaner

  • Loading branch information...
1 parent 6fcf1aa commit 816f9b48469981d1a19915f3524f6f251fc634a2 Eric Schwartz committed Aug 17, 2011
Showing with 45 additions and 0 deletions.
  1. +1 −0 features/cleaning.feature
  2. +44 −0 lib/database_cleaner/active_record/savepoint.rb
@@ -12,6 +12,7 @@ Feature: database cleaning
Examples:
| ORM | Strategy |
+ | ActiveRecord | savepoint |
| ActiveRecord | transaction |
| ActiveRecord | truncation |
| ActiveRecord | deletion |
@@ -0,0 +1,44 @@
+require 'database_cleaner/active_record/base'
+require 'ruby-debug'
+
+$logger ||= Logger.new("/tmp/activerecord.log")
+ActiveRecord::Base.logger = $logger
+
+module DatabaseCleaner::ActiveRecord
+ class Savepoint
+ include ::DatabaseCleaner::ActiveRecord::Base
+
+ def start
+ if connection_klass.connection.open_transactions == 0
+ connection_klass.connection.begin_db_transaction
+ end
+ connection_klass.connection.create_savepoint
+
+#debugger
+ if connection_klass.connection.respond_to?(:increment_open_transactions)
+ connection_klass.connection.increment_open_transactions
+ else
+ connection_klass.__send__(:increment_open_transactions)
+ end
+ end
+
+
+ def clean
+#debugger
+ if connection_klass.connection.respond_to?(:decrement_open_transactions)
+ connection_klass.connection.decrement_open_transactions
+ else
+ connection_klass.__send__(:decrement_open_transactions)
+ end
+
+ if connection_klass.connection.open_transactions >= 0
+ connection_klass.connection.rollback_to_savepoint
+ connection_klass.connection.release_savepoint
+ end
+
+ if connection_klass.connection.open_transactions == 0
+ connection_klass.connection.rollback_db_transaction
+ end
+ end
+ end
+end

0 comments on commit 816f9b4

Please sign in to comment.