-
Notifications
You must be signed in to change notification settings - Fork 91
/
database_rewinder.rb
102 lines (86 loc) · 3.07 KB
/
database_rewinder.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
require_relative 'database_rewinder/cleaner'
module DatabaseRewinder
VERSION = Gem.loaded_specs['database_rewinder'].version.to_s
class << self
# Set your DB configuration here if you'd like to use something else than the AR configuration
attr_writer :database_configuration
def init
@cleaners, @table_names_cache, @clean_all, @only, @except, @database_configuration = [], {}, false
end
def database_configuration
@database_configuration || ActiveRecord::Base.configurations
end
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
# In order to add another database to cleanup, you can give its connection name in on of the forms below:
#
# # the simplest form
# DatabaseRewinder['the_db_name']
#
# or
#
# # with connection: key
# DatabaseRewinder[connection: 'the_db_name']
#
# or
#
# # DatabaseCleaner compatible
# DatabaseRewinder[:active_record, connection: 'the_db_name']
#
# You can cleanup multiple databases for each test using this configuration.
def [](connection)
@cleaners.detect {|c| c.connection_name == connection} || create_cleaner(connection)
end
def all=(v)
@clean_all = v
end
def cleaners
create_cleaner 'test' if @cleaners.empty?
@cleaners
end
def record_inserted_table(connection, sql)
config = connection.instance_variable_get(:'@config')
database = config[:database]
#NOTE What's the best way to get the app dir besides Rails.root? I know Dir.pwd here might not be the right solution, but it should work in most cases...
root_dir = defined?(Rails) ? 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(/\AINSERT(?:\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
def clean
if @clean_all
clean_all
else
cleaners.each(&:clean)
end
end
def clean_all
cleaners.each(&:clean_all)
end
# cache AR connection.tables
def all_table_names(connection)
db = connection.pool.spec.config[:database]
@table_names_cache[db] ||= connection.tables.reject{|t| t == ActiveRecord::Migrator.schema_migrations_table_name }
end
end
end
begin
require 'rails'
require_relative 'database_rewinder/railtie'
rescue LoadError
DatabaseRewinder.init
require_relative 'database_rewinder/active_record_monkey'
end