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



80
81
82
83
# File 'lib/database_rewinder.rb', line 80

def all_table_names(connection)
  db = connection.instance_variable_get(:'@config')[:database]
  @table_names_cache[db] ||= connection.tables.reject{|t| t == ActiveRecord::Migrator.schema_migrations_table_name }
end

.cleanObject



55
56
57
58
59
60
61
# File 'lib/database_rewinder.rb', line 55

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

.clean_allObject



63
64
65
# File 'lib/database_rewinder.rb', line 63

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

.clean_with(*args) ⇒ Object

for database_cleaner compat



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

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(Rails.root.join('config/database.yml').read).result)
end

.record_inserted_table(connection, sql) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/database_rewinder.rb', line 36

def record_inserted_table(connection, sql)
  config = connection.instance_variable_get(:'@config')
  database = config[:database]
  cleaner = cleaners.detect do |c|
    if (config[:adapter] == 'sqlite3') && (config[:database] != ':memory:')
      File.expand_path(c.db, Rails.root) == File.expand_path(database, Rails.root)
    else
      c.db == database
    end
  end or return

  match = sql.match(/\AINSERT INTO [`"]?([^\s`"]+)[`"]?/i)
  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



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

def start; end

.strategy=(args) ⇒ Object



74
75
76
77
# File 'lib/database_rewinder.rb', line 74

def strategy=(args)
  options = args.is_a?(Array) ? args.extract_options! : {}
  @only, @except = options[:only], options[:except]
end