Module: DatabaseRewinder
- Extended by:
- Compatibility
- Defined in:
- lib/database_rewinder.rb,
lib/database_rewinder/cleaner.rb,
lib/database_rewinder/railtie.rb,
lib/database_rewinder/dummy_model.rb,
lib/database_rewinder/compatibility.rb,
lib/database_rewinder/active_record_monkey.rb,
lib/database_rewinder/multiple_statements_executor.rb
Defined Under Namespace
Modules: Compatibility, InsertRecorder, MultipleStatementsExecutor
Classes: Cleaner, DummyModel, Railtie
Constant Summary
collapse
- VERSION =
Gem.loaded_specs['database_rewinder'].version.to_s
Class Attribute Summary collapse
Class Method Summary
collapse
[], clean_with, cleaning, start, strategy=
Class Attribute Details
.database_configuration ⇒ Object
15
16
17
|
# File 'lib/database_rewinder.rb', line 15
def database_configuration
@database_configuration || ActiveRecord::Base.configurations
end
|
Class Method Details
.[](connection) ⇒ Object
25
26
27
|
# File 'lib/database_rewinder.rb', line 25
def [](connection)
@cleaners.detect {|c| c.connection_name == connection} || create_cleaner(connection)
end
|
.all=(v) ⇒ Object
29
30
31
|
# File 'lib/database_rewinder.rb', line 29
def all=(v)
@clean_all = v
end
|
.all_table_names(connection) ⇒ Object
cache AR connection.tables
74
75
76
77
78
79
80
81
82
|
# File 'lib/database_rewinder.rb', line 74
def all_table_names(connection)
cache_key = connection.pool.spec.config
tables = ActiveSupport::Deprecation.silence { connection.tables }
@table_names_cache[cache_key] ||= tables.reject do |t|
(t == ActiveRecord::SchemaMigration.table_name) ||
(ActiveRecord::Base.respond_to?(:internal_metadata_table_name) && (t == ActiveRecord::Base.internal_metadata_table_name))
end
end
|
.clean(multiple: true) ⇒ Object
61
62
63
64
65
66
67
|
# File 'lib/database_rewinder.rb', line 61
def clean(multiple: true)
if @clean_all
clean_all multiple: multiple
else
cleaners.each {|c| c.clean multiple: multiple}
end
end
|
.clean_all(multiple: true) ⇒ Object
69
70
71
|
# File 'lib/database_rewinder.rb', line 69
def clean_all(multiple: true)
cleaners.each {|c| c.clean_all multiple: multiple}
end
|
.cleaners ⇒ Object
33
34
35
36
|
# File 'lib/database_rewinder.rb', line 33
def cleaners
create_cleaner 'test' if @cleaners.empty?
@cleaners
end
|
.create_cleaner(connection_name) ⇒ Object
19
20
21
22
23
|
# File 'lib/database_rewinder.rb', line 19
def create_cleaner(connection_name)
config = database_configuration[connection_name] or raise %Q[Database configuration named "#{connection_name}" is not configured.]
Cleaner.new(config: config, connection_name: connection_name, only: @only, except: @except).tap {|c| @cleaners << c}
end
|
.init ⇒ Object
11
12
13
|
# File 'lib/database_rewinder.rb', line 11
def init
@cleaners, @table_names_cache, @clean_all, @only, @except, @database_configuration = [], {}, false
end
|
.record_inserted_table(connection, sql) ⇒ Object
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
|
# File 'lib/database_rewinder.rb', line 38
def record_inserted_table(connection, sql)
config = connection.instance_variable_get(:'@config')
database = config[:database]
root_dir = defined?(Rails) && Rails.respond_to?(:root) ? Rails.root : Dir.pwd
cleaner = cleaners.detect do |c|
if (config[:adapter] == 'sqlite3') && (config[:database] != ':memory:')
File.expand_path(c.db, root_dir) == File.expand_path(database, root_dir)
else
c.db == database
end
end or return
match = sql.match(/\A\s*INSERT(?:\s+IGNORE)?(?:\s+INTO)?\s+(?:\.*[`"]?([^.\s`"]+)[`"]?)*/i)
return unless match
table = match[1]
if table
cleaner.inserted_tables << table unless cleaner.inserted_tables.include? table
cleaner.pool ||= connection.pool
end
end
|