Class: DatabaseRewinder::Cleaner

Inherits:
Object
  • Object
show all
Includes:
Compatibility
Defined in:
lib/database_rewinder/cleaner.rb,
lib/database_rewinder/compatibility.rb

Defined Under Namespace

Modules: Compatibility

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Compatibility

#clean_with, #strategy=

Constructor Details

#initialize(config: nil, connection_name: nil, only: nil, except: nil) ⇒ Cleaner

Returns a new instance of Cleaner.



11
12
13
14
# File 'lib/database_rewinder/cleaner.rb', line 11

def initialize(config: nil, connection_name: nil, only: nil, except: nil)
  @config, @connection_name, @only, @except = config, connection_name, Array(only), Array(except)
  reset
end

Instance Attribute Details

#configObject

Returns the value of attribute config.



9
10
11
# File 'lib/database_rewinder/cleaner.rb', line 9

def config
  @config
end

#connection_nameObject

Returns the value of attribute connection_name.



9
10
11
# File 'lib/database_rewinder/cleaner.rb', line 9

def connection_name
  @connection_name
end

#exceptObject

Returns the value of attribute except.



9
10
11
# File 'lib/database_rewinder/cleaner.rb', line 9

def except
  @except
end

#inserted_tablesObject

Returns the value of attribute inserted_tables.



9
10
11
# File 'lib/database_rewinder/cleaner.rb', line 9

def inserted_tables
  @inserted_tables
end

#onlyObject

Returns the value of attribute only.



9
10
11
# File 'lib/database_rewinder/cleaner.rb', line 9

def only
  @only
end

#poolObject

Returns the value of attribute pool.



9
10
11
# File 'lib/database_rewinder/cleaner.rb', line 9

def pool
  @pool
end

Instance Method Details

#clean(multiple: true) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/database_rewinder/cleaner.rb', line 20

def clean(multiple: true)
  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, multiple: multiple
  end
  reset
end

#clean_all(multiple: true) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/database_rewinder/cleaner.rb', line 33

def clean_all(multiple: true)
  if pool
    ar_conn = pool.connection
    delete_all ar_conn, DatabaseRewinder.all_table_names(ar_conn), multiple: multiple
  else
    require 'database_rewinder/dummy_model'
    DummyModel.with_temporary_connection(config) do |temporary_connection|
      delete_all temporary_connection, DatabaseRewinder.all_table_names(temporary_connection), multiple: multiple
    end
  end

  reset
end

#dbObject



16
17
18
# File 'lib/database_rewinder/cleaner.rb', line 16

def db
  config['database']
end