Module: DatabaseRewinder

Defined in:
lib/database_rewinder.rb,
lib/database_rewinder/cleaner.rb,
lib/database_rewinder/railtie.rb,
lib/database_rewinder/active_record_monkey.rb

Defined Under Namespace

Modules: InsertRecorder Classes: Cleaner, Railtie

Constant Summary collapse

VERSION =
Gem.loaded_specs['database_rewinder'].version.to_s

Class Method Summary collapse

Class Method Details

.[](_orm, connection: nil) ⇒ Object



19
20
21
22
23
24
25
# File 'lib/database_rewinder.rb', line 19

def [](_orm, connection: nil, **)
  if (cl = @cleaners.detect {|c| c.connection_name == connection})
    return cl
  end

  create_cleaner connection
end

.all=(v) ⇒ Object



27
28
29
# File 'lib/database_rewinder.rb', line 27

def all=(v)
  @clean_all = v
end

.all_table_names(connection) ⇒ Object

cache AR connection.tables



72
73
74
75
# File 'lib/database_rewinder.rb', line 72

def all_table_names(connection)
  db = connection.instance_variable_get(:'@config')[:database]
  @table_names_cache[db] ||= connection.tables
end

.cleanObject



48
49
50
51
52
53
54
# File 'lib/database_rewinder.rb', line 48

def clean
  if @clean_all
    clean_all
  else
    cleaners.each {|c| c.clean}
  end
end

.clean_allObject



56
57
58
# File 'lib/database_rewinder.rb', line 56

def clean_all
  cleaners.each {|c| c.clean_all}
end

.clean_with(*args) ⇒ Object

for database_cleaner compat



61
62
63
# File 'lib/database_rewinder.rb', line 61

def clean_with(*args)
  cleaners.each {|c| c.clean_with *args}
end

.cleanersObject



31
32
33
34
# File 'lib/database_rewinder.rb', line 31

def cleaners
  create_cleaner 'test' if @cleaners.empty?
  @cleaners
end

.create_cleaner(connection_name) ⇒ Object



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

def create_cleaner(connection_name)
  config = @db_config[connection_name] or raise %Q[Database configuration named "#{connection_name}" is not configured.]

  Cleaner.new(db: config['database'], connection_name: connection_name, only: @only, except: @except).tap {|c| @cleaners << c}
end

.initObject



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

def init
  @cleaners, @table_names_cache, @clean_all, @only, @except = [], {}, false
  @db_config = YAML::load(ERB.new(IO.read(Rails.root.join 'config/database.yml')).result)
end

.record_inserted_table(connection, sql) ⇒ Object



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

def record_inserted_table(connection, sql)
  database = connection.instance_variable_get(:'@config')[:database]
  cleaner = cleaners.detect {|c| c.db == database} or return

  match = sql.match(/\AINSERT INTO [`"]?([^\s`"]+)[`"]?/)
  table = match[1] if match
  if table
    cleaner.inserted_tables << table unless cleaner.inserted_tables.include? table
    cleaner.pool ||= connection.pool
  end
end

.startObject

for database_cleaner compat



66
# File 'lib/database_rewinder.rb', line 66

def start; end

.strategy=(_strategy, only: nil, except: nil) ⇒ Object



67
68
69
# File 'lib/database_rewinder.rb', line 67

def strategy=(_strategy, only: nil, except: nil, **)
  @only, @except = only, except
end