Sha256: 8feb04deb9d0613a12aff9f827c4cd0c35e30be0fafea01d6f0f37de59f80c56
Contents?: true
Size: 1.61 KB
Versions: 1
Compression:
Stored size: 1.61 KB
Contents
module DatabaseRewinder class Cleaner attr_accessor :db, :connection_name, :only, :except, :inserted_tables, :pool def initialize(db: nil, connection_name: nil, only: nil, except: nil) @db, @connection_name, @only, @except = db, connection_name, Array(only), Array(except) reset end def clean return if !pool || inserted_tables.empty? # When the application uses multiple database connections, a connection # pool used in test could be already removed (i.e., pool.connected? = false). # In this case, we have to reconnect to the database to clean inserted # tables. with_automatic_reconnect(pool) do delete_all (ar_conn = pool.connection), DatabaseRewinder.all_table_names(ar_conn) & inserted_tables end reset end def clean_all ar_conn = pool ? pool.connection : ActiveRecord::Base.connection delete_all ar_conn, DatabaseRewinder.all_table_names(ar_conn) reset end private def delete_all(ar_conn, tables) tables = tables & @only if @only.any? tables -= @except if @except.any? return if tables.empty? ar_conn.disable_referential_integrity do tables.each do |table_name| ar_conn.execute "DELETE FROM #{ar_conn.quote_table_name(table_name)};" end end end def reset @inserted_tables = [] end def with_automatic_reconnect(pool, &block) reconnect = pool.automatic_reconnect pool.automatic_reconnect = true block.call ensure pool.automatic_reconnect = reconnect end end end require_relative 'compatibility'
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
database_rewinder-0.3.0 | lib/database_rewinder/cleaner.rb |