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.



13
14
15
16
# File 'lib/database_rewinder/cleaner.rb', line 13

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.



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

def config
  @config
end

#connection_nameObject

Returns the value of attribute connection_name.



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

def connection_name
  @connection_name
end

#exceptObject

Returns the value of attribute except.



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

def except
  @except
end

#inserted_tablesObject

Returns the value of attribute inserted_tables.



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

def inserted_tables
  @inserted_tables
end

#onlyObject

Returns the value of attribute only.



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

def only
  @only
end

#poolObject

Returns the value of attribute pool.



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

def pool
  @pool
end

Instance Method Details

#clean(multiple: true) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/database_rewinder/cleaner.rb', line 26

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.lease_connection), DatabaseRewinder.all_table_names(ar_conn) & inserted_tables, multiple: multiple
  end
  reset
end

#clean_all(multiple: true) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/database_rewinder/cleaner.rb', line 39

def clean_all(multiple: true)
  if pool
    ar_conn = pool.lease_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



18
19
20
# File 'lib/database_rewinder/cleaner.rb', line 18

def db
  config['database']
end

#hostObject



22
23
24
# File 'lib/database_rewinder/cleaner.rb', line 22

def host
  config['host']
end