database_cleaner 1.7.0 → 2.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/database_cleaner.rb +1 -12
- metadata +24 -418
- data/CONTRIBUTE.markdown +0 -28
- data/Gemfile.lock +0 -286
- data/History.rdoc +0 -453
- data/LICENSE +0 -20
- data/README.markdown +0 -541
- data/Rakefile +0 -40
- data/TODO +0 -3
- data/VERSION.yml +0 -4
- data/cucumber.yml +0 -1
- data/lib/database_cleaner/active_record/base.rb +0 -92
- data/lib/database_cleaner/active_record/deletion.rb +0 -108
- data/lib/database_cleaner/active_record/transaction.rb +0 -59
- data/lib/database_cleaner/active_record/truncation.rb +0 -275
- data/lib/database_cleaner/base.rb +0 -166
- data/lib/database_cleaner/configuration.rb +0 -131
- data/lib/database_cleaner/couch_potato/base.rb +0 -7
- data/lib/database_cleaner/couch_potato/truncation.rb +0 -28
- data/lib/database_cleaner/cucumber.rb +0 -3
- data/lib/database_cleaner/data_mapper/base.rb +0 -21
- data/lib/database_cleaner/data_mapper/transaction.rb +0 -28
- data/lib/database_cleaner/data_mapper/truncation.rb +0 -172
- data/lib/database_cleaner/generic/base.rb +0 -29
- data/lib/database_cleaner/generic/transaction.rb +0 -11
- data/lib/database_cleaner/generic/truncation.rb +0 -40
- data/lib/database_cleaner/mongo/base.rb +0 -16
- data/lib/database_cleaner/mongo/truncation.rb +0 -62
- data/lib/database_cleaner/mongo/truncation_mixin.rb +0 -26
- data/lib/database_cleaner/mongo2/base.rb +0 -16
- data/lib/database_cleaner/mongo2/truncation_mixin.rb +0 -39
- data/lib/database_cleaner/mongo_mapper/base.rb +0 -20
- data/lib/database_cleaner/mongo_mapper/truncation.rb +0 -19
- data/lib/database_cleaner/mongoid/base.rb +0 -20
- data/lib/database_cleaner/mongoid/truncation.rb +0 -49
- data/lib/database_cleaner/moped/base.rb +0 -39
- data/lib/database_cleaner/moped/truncation.rb +0 -9
- data/lib/database_cleaner/moped/truncation_base.rb +0 -40
- data/lib/database_cleaner/neo4j/base.rb +0 -62
- data/lib/database_cleaner/neo4j/deletion.rb +0 -16
- data/lib/database_cleaner/neo4j/transaction.rb +0 -35
- data/lib/database_cleaner/neo4j/truncation.rb +0 -9
- data/lib/database_cleaner/null_strategy.rb +0 -20
- data/lib/database_cleaner/ohm/truncation.rb +0 -15
- data/lib/database_cleaner/redis/base.rb +0 -37
- data/lib/database_cleaner/redis/truncation.rb +0 -26
- data/lib/database_cleaner/safeguard.rb +0 -72
- data/lib/database_cleaner/sequel/base.rb +0 -22
- data/lib/database_cleaner/sequel/deletion.rb +0 -47
- data/lib/database_cleaner/sequel/transaction.rb +0 -40
- data/lib/database_cleaner/sequel/truncation.rb +0 -78
@@ -1,108 +0,0 @@
|
|
1
|
-
require 'active_record/base'
|
2
|
-
require 'active_record/connection_adapters/abstract_adapter'
|
3
|
-
require "database_cleaner/generic/truncation"
|
4
|
-
require 'database_cleaner/active_record/base'
|
5
|
-
require 'database_cleaner/active_record/truncation'
|
6
|
-
|
7
|
-
module DatabaseCleaner
|
8
|
-
module ConnectionAdapters
|
9
|
-
module AbstractDeleteAdapter
|
10
|
-
def delete_table(table_name)
|
11
|
-
raise NotImplementedError
|
12
|
-
end
|
13
|
-
end
|
14
|
-
|
15
|
-
module GenericDeleteAdapter
|
16
|
-
def delete_table(table_name)
|
17
|
-
execute("DELETE FROM #{quote_table_name(table_name)};")
|
18
|
-
end
|
19
|
-
end
|
20
|
-
|
21
|
-
module OracleDeleteAdapter
|
22
|
-
def delete_table(table_name)
|
23
|
-
execute("DELETE FROM #{quote_table_name(table_name)}")
|
24
|
-
end
|
25
|
-
end
|
26
|
-
end
|
27
|
-
end
|
28
|
-
|
29
|
-
module ActiveRecord
|
30
|
-
module ConnectionAdapters
|
31
|
-
AbstractAdapter.class_eval { include DatabaseCleaner::ConnectionAdapters::AbstractDeleteAdapter }
|
32
|
-
|
33
|
-
JdbcAdapter.class_eval { include ::DatabaseCleaner::ConnectionAdapters::GenericDeleteAdapter } if defined?(JdbcAdapter)
|
34
|
-
AbstractMysqlAdapter.class_eval { include ::DatabaseCleaner::ConnectionAdapters::GenericDeleteAdapter } if defined?(AbstractMysqlAdapter)
|
35
|
-
Mysql2Adapter.class_eval { include ::DatabaseCleaner::ConnectionAdapters::GenericDeleteAdapter } if defined?(Mysql2Adapter)
|
36
|
-
SQLiteAdapter.class_eval { include ::DatabaseCleaner::ConnectionAdapters::GenericDeleteAdapter } if defined?(SQLiteAdapter)
|
37
|
-
SQLite3Adapter.class_eval { include ::DatabaseCleaner::ConnectionAdapters::GenericDeleteAdapter } if defined?(SQLite3Adapter)
|
38
|
-
PostgreSQLAdapter.class_eval { include ::DatabaseCleaner::ConnectionAdapters::GenericDeleteAdapter } if defined?(PostgreSQLAdapter)
|
39
|
-
IBM_DBAdapter.class_eval { include ::DatabaseCleaner::ConnectionAdapters::GenericDeleteAdapter } if defined?(IBM_DBAdapter)
|
40
|
-
SQLServerAdapter.class_eval { include ::DatabaseCleaner::ConnectionAdapters::GenericDeleteAdapter } if defined?(SQLServerAdapter)
|
41
|
-
OracleEnhancedAdapter.class_eval { include ::DatabaseCleaner::ConnectionAdapters::OracleDeleteAdapter } if defined?(OracleEnhancedAdapter)
|
42
|
-
end
|
43
|
-
end
|
44
|
-
|
45
|
-
module DatabaseCleaner::ActiveRecord
|
46
|
-
module SelectiveTruncation
|
47
|
-
def tables_to_truncate(connection)
|
48
|
-
if information_schema_exists?(connection)
|
49
|
-
(@only || tables_with_new_rows(connection)) - @tables_to_exclude
|
50
|
-
else
|
51
|
-
super
|
52
|
-
end
|
53
|
-
end
|
54
|
-
|
55
|
-
def tables_with_new_rows(connection)
|
56
|
-
@db_name ||= connection.instance_variable_get('@config')[:database]
|
57
|
-
stats = table_stats_query(connection, @db_name)
|
58
|
-
if stats != ''
|
59
|
-
connection.exec_query(stats).inject([]) {|all, stat| all << stat['table_name'] if stat['exact_row_count'] > 0; all }
|
60
|
-
else
|
61
|
-
[]
|
62
|
-
end
|
63
|
-
end
|
64
|
-
|
65
|
-
def table_stats_query(connection, db_name)
|
66
|
-
if @cache_tables && !@table_stats_query.nil?
|
67
|
-
return @table_stats_query
|
68
|
-
else
|
69
|
-
tables = connection.select_values(<<-SQL)
|
70
|
-
SELECT table_name
|
71
|
-
FROM information_schema.tables
|
72
|
-
WHERE table_schema = '#{db_name}'
|
73
|
-
AND #{::DatabaseCleaner::ActiveRecord::Base.exclusion_condition('table_name')};
|
74
|
-
SQL
|
75
|
-
queries = tables.map do |table|
|
76
|
-
"SELECT #{connection.quote(table)} AS table_name, COUNT(*) AS exact_row_count FROM #{connection.quote_table_name(table)}"
|
77
|
-
end
|
78
|
-
@table_stats_query = queries.join(' UNION ')
|
79
|
-
end
|
80
|
-
end
|
81
|
-
|
82
|
-
def information_schema_exists? connection
|
83
|
-
return false unless connection.is_a? ActiveRecord::ConnectionAdapters::Mysql2Adapter
|
84
|
-
@information_schema_exists ||=
|
85
|
-
begin
|
86
|
-
connection.execute("SELECT 1 FROM information_schema.tables")
|
87
|
-
true
|
88
|
-
rescue
|
89
|
-
false
|
90
|
-
end
|
91
|
-
end
|
92
|
-
end
|
93
|
-
|
94
|
-
class Deletion < Truncation
|
95
|
-
if defined?(ActiveRecord::ConnectionAdapters::Mysql2Adapter)
|
96
|
-
include SelectiveTruncation
|
97
|
-
end
|
98
|
-
|
99
|
-
def clean
|
100
|
-
connection = connection_class.connection
|
101
|
-
connection.disable_referential_integrity do
|
102
|
-
tables_to_truncate(connection).each do |table_name|
|
103
|
-
connection.delete_table table_name
|
104
|
-
end
|
105
|
-
end
|
106
|
-
end
|
107
|
-
end
|
108
|
-
end
|
@@ -1,59 +0,0 @@
|
|
1
|
-
require 'database_cleaner/active_record/base'
|
2
|
-
require 'database_cleaner/generic/transaction'
|
3
|
-
|
4
|
-
module DatabaseCleaner::ActiveRecord
|
5
|
-
class Transaction
|
6
|
-
include ::DatabaseCleaner::ActiveRecord::Base
|
7
|
-
include ::DatabaseCleaner::Generic::Transaction
|
8
|
-
|
9
|
-
def start
|
10
|
-
# Hack to make sure that the connection is properly setup for
|
11
|
-
# the clean code.
|
12
|
-
connection_class.connection.transaction{ }
|
13
|
-
|
14
|
-
if connection_maintains_transaction_count?
|
15
|
-
if connection_class.connection.respond_to?(:increment_open_transactions)
|
16
|
-
connection_class.connection.increment_open_transactions
|
17
|
-
else
|
18
|
-
connection_class.__send__(:increment_open_transactions)
|
19
|
-
end
|
20
|
-
end
|
21
|
-
if connection_class.connection.respond_to?(:begin_transaction)
|
22
|
-
connection_class.connection.begin_transaction :joinable => false
|
23
|
-
else
|
24
|
-
connection_class.connection.begin_db_transaction
|
25
|
-
end
|
26
|
-
end
|
27
|
-
|
28
|
-
|
29
|
-
def clean
|
30
|
-
connection_class.connection_pool.connections.each do |connection|
|
31
|
-
next unless connection.open_transactions > 0
|
32
|
-
|
33
|
-
if connection.respond_to?(:rollback_transaction)
|
34
|
-
connection.rollback_transaction
|
35
|
-
else
|
36
|
-
connection.rollback_db_transaction
|
37
|
-
end
|
38
|
-
|
39
|
-
# The below is for handling after_commit hooks.. see https://github.com/bmabey/database_cleaner/issues/99
|
40
|
-
if connection.respond_to?(:rollback_transaction_records, true)
|
41
|
-
connection.send(:rollback_transaction_records, true)
|
42
|
-
end
|
43
|
-
|
44
|
-
if connection_maintains_transaction_count?
|
45
|
-
if connection.respond_to?(:decrement_open_transactions)
|
46
|
-
connection.decrement_open_transactions
|
47
|
-
else
|
48
|
-
connection_class.__send__(:decrement_open_transactions)
|
49
|
-
end
|
50
|
-
end
|
51
|
-
end
|
52
|
-
end
|
53
|
-
|
54
|
-
def connection_maintains_transaction_count?
|
55
|
-
ActiveRecord::VERSION::MAJOR < 4
|
56
|
-
end
|
57
|
-
|
58
|
-
end
|
59
|
-
end
|
@@ -1,275 +0,0 @@
|
|
1
|
-
require 'active_record/base'
|
2
|
-
|
3
|
-
require 'active_record/connection_adapters/abstract_adapter'
|
4
|
-
|
5
|
-
#Load available connection adapters
|
6
|
-
%w(
|
7
|
-
abstract_mysql_adapter postgresql_adapter sqlite3_adapter mysql_adapter mysql2_adapter
|
8
|
-
).each do |known_adapter|
|
9
|
-
begin
|
10
|
-
require "active_record/connection_adapters/#{known_adapter}"
|
11
|
-
rescue LoadError
|
12
|
-
end
|
13
|
-
end
|
14
|
-
|
15
|
-
require "database_cleaner/generic/truncation"
|
16
|
-
require 'database_cleaner/active_record/base'
|
17
|
-
|
18
|
-
module DatabaseCleaner
|
19
|
-
module ConnectionAdapters
|
20
|
-
|
21
|
-
module AbstractAdapter
|
22
|
-
# used to be called views but that can clash with gems like schema_plus
|
23
|
-
# this gem is not meant to be exposing such an extra interface any way
|
24
|
-
def database_cleaner_view_cache
|
25
|
-
@views ||= select_values("select table_name from information_schema.views where table_schema = '#{current_database}'") rescue []
|
26
|
-
end
|
27
|
-
|
28
|
-
def database_cleaner_table_cache
|
29
|
-
# the adapters don't do caching (#130) but we make the assumption that the list stays the same in tests
|
30
|
-
@database_cleaner_tables ||= ::ActiveRecord::VERSION::MAJOR >= 5 ? data_sources : tables
|
31
|
-
end
|
32
|
-
|
33
|
-
def truncate_table(table_name)
|
34
|
-
raise NotImplementedError
|
35
|
-
end
|
36
|
-
|
37
|
-
def truncate_tables(tables)
|
38
|
-
tables.each do |table_name|
|
39
|
-
self.truncate_table(table_name)
|
40
|
-
end
|
41
|
-
end
|
42
|
-
end
|
43
|
-
|
44
|
-
module AbstractMysqlAdapter
|
45
|
-
def truncate_table(table_name)
|
46
|
-
execute("TRUNCATE TABLE #{quote_table_name(table_name)};")
|
47
|
-
end
|
48
|
-
|
49
|
-
def truncate_tables(tables)
|
50
|
-
tables.each { |t| truncate_table(t) }
|
51
|
-
end
|
52
|
-
|
53
|
-
def pre_count_truncate_tables(tables, options = {:reset_ids => true})
|
54
|
-
filter = options[:reset_ids] ? method(:has_been_used?) : method(:has_rows?)
|
55
|
-
truncate_tables(tables.select(&filter))
|
56
|
-
end
|
57
|
-
|
58
|
-
private
|
59
|
-
|
60
|
-
def row_count(table)
|
61
|
-
# Patch for MysqlAdapter with ActiveRecord 3.2.7 later
|
62
|
-
# select_value("SELECT 1") #=> "1"
|
63
|
-
select_value("SELECT EXISTS (SELECT 1 FROM #{quote_table_name(table)} LIMIT 1)").to_i
|
64
|
-
end
|
65
|
-
|
66
|
-
# Returns a boolean indicating if the given table has an auto-inc number higher than 0.
|
67
|
-
# Note, this is different than an empty table since an table may populated, the index increased,
|
68
|
-
# but then the table is cleaned. In other words, this function tells us if the given table
|
69
|
-
# was ever inserted into.
|
70
|
-
def has_been_used?(table)
|
71
|
-
if has_rows?(table)
|
72
|
-
true
|
73
|
-
else
|
74
|
-
# Patch for MysqlAdapter with ActiveRecord 3.2.7 later
|
75
|
-
# select_value("SELECT 1") #=> "1"
|
76
|
-
select_value(<<-SQL).to_i > 1 # returns nil if not present
|
77
|
-
SELECT Auto_increment
|
78
|
-
FROM information_schema.tables
|
79
|
-
WHERE table_name='#{table}';
|
80
|
-
SQL
|
81
|
-
end
|
82
|
-
end
|
83
|
-
|
84
|
-
def has_rows?(table)
|
85
|
-
row_count(table) > 0
|
86
|
-
end
|
87
|
-
end
|
88
|
-
|
89
|
-
module IBM_DBAdapter
|
90
|
-
def truncate_table(table_name)
|
91
|
-
execute("TRUNCATE #{quote_table_name(table_name)} IMMEDIATE")
|
92
|
-
end
|
93
|
-
end
|
94
|
-
|
95
|
-
module SQLiteAdapter
|
96
|
-
def delete_table(table_name)
|
97
|
-
execute("DELETE FROM #{quote_table_name(table_name)};")
|
98
|
-
if uses_sequence
|
99
|
-
execute("DELETE FROM sqlite_sequence where name = '#{table_name}';")
|
100
|
-
end
|
101
|
-
end
|
102
|
-
alias truncate_table delete_table
|
103
|
-
|
104
|
-
def truncate_tables(tables)
|
105
|
-
tables.each { |t| truncate_table(t) }
|
106
|
-
end
|
107
|
-
|
108
|
-
private
|
109
|
-
|
110
|
-
# Returns a boolean indicating if the SQLite database is using the sqlite_sequence table.
|
111
|
-
def uses_sequence
|
112
|
-
select_value("SELECT name FROM sqlite_master WHERE type='table' AND name='sqlite_sequence';")
|
113
|
-
end
|
114
|
-
end
|
115
|
-
|
116
|
-
module TruncateOrDelete
|
117
|
-
def truncate_table(table_name)
|
118
|
-
begin
|
119
|
-
execute("TRUNCATE TABLE #{quote_table_name(table_name)};")
|
120
|
-
rescue ::ActiveRecord::StatementInvalid
|
121
|
-
execute("DELETE FROM #{quote_table_name(table_name)};")
|
122
|
-
end
|
123
|
-
end
|
124
|
-
end
|
125
|
-
|
126
|
-
module OracleAdapter
|
127
|
-
def truncate_table(table_name)
|
128
|
-
execute("TRUNCATE TABLE #{quote_table_name(table_name)}")
|
129
|
-
end
|
130
|
-
end
|
131
|
-
|
132
|
-
module PostgreSQLAdapter
|
133
|
-
def db_version
|
134
|
-
@db_version ||= postgresql_version
|
135
|
-
end
|
136
|
-
|
137
|
-
def cascade
|
138
|
-
@cascade ||= db_version >= 80200 ? 'CASCADE' : ''
|
139
|
-
end
|
140
|
-
|
141
|
-
def restart_identity
|
142
|
-
@restart_identity ||= db_version >= 80400 ? 'RESTART IDENTITY' : ''
|
143
|
-
end
|
144
|
-
|
145
|
-
def truncate_table(table_name)
|
146
|
-
truncate_tables([table_name])
|
147
|
-
end
|
148
|
-
|
149
|
-
def truncate_tables(table_names)
|
150
|
-
return if table_names.nil? || table_names.empty?
|
151
|
-
execute("TRUNCATE TABLE #{table_names.map{|name| quote_table_name(name)}.join(', ')} #{restart_identity} #{cascade};")
|
152
|
-
end
|
153
|
-
|
154
|
-
def pre_count_truncate_tables(tables, options = {:reset_ids => true})
|
155
|
-
filter = options[:reset_ids] ? method(:has_been_used?) : method(:has_rows?)
|
156
|
-
truncate_tables(tables.select(&filter))
|
157
|
-
end
|
158
|
-
|
159
|
-
def database_cleaner_table_cache
|
160
|
-
# AR returns a list of tables without schema but then returns a
|
161
|
-
# migrations table with the schema. There are other problems, too,
|
162
|
-
# with using the base list. If a table exists in multiple schemas
|
163
|
-
# within the search path, truncation without the schema name could
|
164
|
-
# result in confusing, if not unexpected results.
|
165
|
-
@database_cleaner_tables ||= tables_with_schema
|
166
|
-
end
|
167
|
-
|
168
|
-
private
|
169
|
-
|
170
|
-
# Returns a boolean indicating if the given table has an auto-inc number higher than 0.
|
171
|
-
# Note, this is different than an empty table since an table may populated, the index increased,
|
172
|
-
# but then the table is cleaned. In other words, this function tells us if the given table
|
173
|
-
# was ever inserted into.
|
174
|
-
def has_been_used?(table)
|
175
|
-
return has_rows?(table) unless has_sequence?(table)
|
176
|
-
|
177
|
-
cur_val = select_value("SELECT currval('#{table}_id_seq');").to_i rescue 0
|
178
|
-
cur_val > 0
|
179
|
-
end
|
180
|
-
|
181
|
-
def has_sequence?(table)
|
182
|
-
select_value("SELECT true FROM pg_class WHERE relname = '#{table}_id_seq';")
|
183
|
-
end
|
184
|
-
|
185
|
-
def has_rows?(table)
|
186
|
-
select_value("SELECT true FROM #{table} LIMIT 1;")
|
187
|
-
end
|
188
|
-
|
189
|
-
def tables_with_schema
|
190
|
-
rows = select_rows <<-_SQL
|
191
|
-
SELECT schemaname || '.' || tablename
|
192
|
-
FROM pg_tables
|
193
|
-
WHERE
|
194
|
-
tablename !~ '_prt_' AND
|
195
|
-
#{::DatabaseCleaner::ActiveRecord::Base.exclusion_condition('tablename')} AND
|
196
|
-
schemaname = ANY (current_schemas(false))
|
197
|
-
_SQL
|
198
|
-
rows.collect { |result| result.first }
|
199
|
-
end
|
200
|
-
end
|
201
|
-
end
|
202
|
-
end
|
203
|
-
|
204
|
-
module ActiveRecord
|
205
|
-
module ConnectionAdapters
|
206
|
-
#Apply adapter decoraters where applicable (adapter should be loaded)
|
207
|
-
AbstractAdapter.class_eval { include DatabaseCleaner::ConnectionAdapters::AbstractAdapter }
|
208
|
-
|
209
|
-
if defined?(JdbcAdapter)
|
210
|
-
if defined?(OracleJdbcConnection)
|
211
|
-
JdbcAdapter.class_eval { include ::DatabaseCleaner::ConnectionAdapters::OracleAdapter }
|
212
|
-
else
|
213
|
-
JdbcAdapter.class_eval { include ::DatabaseCleaner::ConnectionAdapters::TruncateOrDelete }
|
214
|
-
end
|
215
|
-
end
|
216
|
-
AbstractMysqlAdapter.class_eval { include ::DatabaseCleaner::ConnectionAdapters::AbstractMysqlAdapter } if defined?(AbstractMysqlAdapter)
|
217
|
-
Mysql2Adapter.class_eval { include ::DatabaseCleaner::ConnectionAdapters::AbstractMysqlAdapter } if defined?(Mysql2Adapter)
|
218
|
-
MysqlAdapter.class_eval { include ::DatabaseCleaner::ConnectionAdapters::AbstractMysqlAdapter } if defined?(MysqlAdapter)
|
219
|
-
SQLiteAdapter.class_eval { include ::DatabaseCleaner::ConnectionAdapters::SQLiteAdapter } if defined?(SQLiteAdapter)
|
220
|
-
SQLite3Adapter.class_eval { include ::DatabaseCleaner::ConnectionAdapters::SQLiteAdapter } if defined?(SQLite3Adapter)
|
221
|
-
PostgreSQLAdapter.class_eval { include ::DatabaseCleaner::ConnectionAdapters::PostgreSQLAdapter } if defined?(PostgreSQLAdapter)
|
222
|
-
IBM_DBAdapter.class_eval { include ::DatabaseCleaner::ConnectionAdapters::IBM_DBAdapter } if defined?(IBM_DBAdapter)
|
223
|
-
SQLServerAdapter.class_eval { include ::DatabaseCleaner::ConnectionAdapters::TruncateOrDelete } if defined?(SQLServerAdapter)
|
224
|
-
OracleEnhancedAdapter.class_eval { include ::DatabaseCleaner::ConnectionAdapters::OracleAdapter } if defined?(OracleEnhancedAdapter)
|
225
|
-
end
|
226
|
-
end
|
227
|
-
|
228
|
-
module DatabaseCleaner::ActiveRecord
|
229
|
-
class Truncation
|
230
|
-
include ::DatabaseCleaner::ActiveRecord::Base
|
231
|
-
include ::DatabaseCleaner::Generic::Truncation
|
232
|
-
|
233
|
-
def clean
|
234
|
-
connection = connection_class.connection
|
235
|
-
connection.disable_referential_integrity do
|
236
|
-
if pre_count? && connection.respond_to?(:pre_count_truncate_tables)
|
237
|
-
connection.pre_count_truncate_tables(tables_to_truncate(connection), {:reset_ids => reset_ids?})
|
238
|
-
else
|
239
|
-
connection.truncate_tables(tables_to_truncate(connection))
|
240
|
-
end
|
241
|
-
end
|
242
|
-
end
|
243
|
-
|
244
|
-
private
|
245
|
-
|
246
|
-
def tables_to_truncate(connection)
|
247
|
-
tables_in_db = cache_tables? ? connection.database_cleaner_table_cache : connection.tables
|
248
|
-
to_reject = (@tables_to_exclude + connection.database_cleaner_view_cache)
|
249
|
-
(@only || tables_in_db).reject do |table|
|
250
|
-
if ( m = table.match(/([^.]+)$/) )
|
251
|
-
to_reject.include?(m[1])
|
252
|
-
else
|
253
|
-
false
|
254
|
-
end
|
255
|
-
end
|
256
|
-
end
|
257
|
-
|
258
|
-
# overwritten
|
259
|
-
def migration_storage_names
|
260
|
-
[::DatabaseCleaner::ActiveRecord::Base.migration_table_name]
|
261
|
-
end
|
262
|
-
|
263
|
-
def cache_tables?
|
264
|
-
!!@cache_tables
|
265
|
-
end
|
266
|
-
|
267
|
-
def pre_count?
|
268
|
-
@pre_count == true
|
269
|
-
end
|
270
|
-
|
271
|
-
def reset_ids?
|
272
|
-
@reset_ids != false
|
273
|
-
end
|
274
|
-
end
|
275
|
-
end
|
@@ -1,166 +0,0 @@
|
|
1
|
-
require 'database_cleaner/null_strategy'
|
2
|
-
require 'database_cleaner/safeguard'
|
3
|
-
module DatabaseCleaner
|
4
|
-
class Base
|
5
|
-
include Comparable
|
6
|
-
|
7
|
-
def <=>(other)
|
8
|
-
(self.orm <=> other.orm) == 0 ? self.db <=> other.db : self.orm <=> other.orm
|
9
|
-
end
|
10
|
-
|
11
|
-
def initialize(desired_orm = nil,opts = {})
|
12
|
-
if [:autodetect, nil, "autodetect"].include?(desired_orm)
|
13
|
-
autodetect
|
14
|
-
else
|
15
|
-
self.orm = desired_orm
|
16
|
-
end
|
17
|
-
self.db = opts[:connection] || opts[:model] if opts.has_key?(:connection) || opts.has_key?(:model)
|
18
|
-
set_default_orm_strategy
|
19
|
-
Safeguard.new.run
|
20
|
-
end
|
21
|
-
|
22
|
-
def db=(desired_db)
|
23
|
-
self.strategy_db = desired_db
|
24
|
-
@db = desired_db
|
25
|
-
end
|
26
|
-
|
27
|
-
def strategy_db=(desired_db)
|
28
|
-
if strategy.respond_to? :db=
|
29
|
-
strategy.db = desired_db
|
30
|
-
elsif desired_db!= :default
|
31
|
-
raise ArgumentError, "You must provide a strategy object that supports non default databases when you specify a database"
|
32
|
-
end
|
33
|
-
end
|
34
|
-
|
35
|
-
def db
|
36
|
-
@db ||= :default
|
37
|
-
end
|
38
|
-
|
39
|
-
def create_strategy(*args)
|
40
|
-
strategy, *strategy_args = args
|
41
|
-
orm_strategy(strategy).new(*strategy_args)
|
42
|
-
end
|
43
|
-
|
44
|
-
def clean_with(*args)
|
45
|
-
strategy = create_strategy(*args)
|
46
|
-
set_strategy_db strategy, self.db
|
47
|
-
|
48
|
-
strategy.clean
|
49
|
-
strategy
|
50
|
-
end
|
51
|
-
|
52
|
-
alias clean_with! clean_with
|
53
|
-
|
54
|
-
def set_strategy_db(strategy, desired_db)
|
55
|
-
if strategy.respond_to? :db=
|
56
|
-
strategy.db = desired_db
|
57
|
-
elsif desired_db != :default
|
58
|
-
raise ArgumentError, "You must provide a strategy object that supports non default databases when you specify a database"
|
59
|
-
end
|
60
|
-
end
|
61
|
-
|
62
|
-
def strategy=(args)
|
63
|
-
strategy, *strategy_args = args
|
64
|
-
if strategy.is_a?(Symbol)
|
65
|
-
@strategy = create_strategy(*args)
|
66
|
-
elsif strategy_args.empty?
|
67
|
-
@strategy = strategy
|
68
|
-
else
|
69
|
-
raise ArgumentError, "You must provide a strategy object, or a symbol for a known strategy along with initialization params."
|
70
|
-
end
|
71
|
-
|
72
|
-
set_strategy_db @strategy, self.db
|
73
|
-
|
74
|
-
@strategy
|
75
|
-
end
|
76
|
-
|
77
|
-
def strategy
|
78
|
-
@strategy ||= NullStrategy
|
79
|
-
end
|
80
|
-
|
81
|
-
def orm=(desired_orm)
|
82
|
-
@orm = desired_orm.to_sym
|
83
|
-
end
|
84
|
-
|
85
|
-
def orm
|
86
|
-
@orm || autodetect
|
87
|
-
end
|
88
|
-
|
89
|
-
def start
|
90
|
-
strategy.start
|
91
|
-
end
|
92
|
-
|
93
|
-
def clean
|
94
|
-
strategy.clean
|
95
|
-
end
|
96
|
-
|
97
|
-
alias clean! clean
|
98
|
-
|
99
|
-
def cleaning(&block)
|
100
|
-
strategy.cleaning(&block)
|
101
|
-
end
|
102
|
-
|
103
|
-
def auto_detected?
|
104
|
-
!!@autodetected
|
105
|
-
end
|
106
|
-
|
107
|
-
def autodetect_orm
|
108
|
-
if defined? ::ActiveRecord
|
109
|
-
:active_record
|
110
|
-
elsif defined? ::DataMapper
|
111
|
-
:data_mapper
|
112
|
-
elsif defined? ::MongoMapper
|
113
|
-
:mongo_mapper
|
114
|
-
elsif defined? ::Mongoid
|
115
|
-
:mongoid
|
116
|
-
elsif defined? ::CouchPotato
|
117
|
-
:couch_potato
|
118
|
-
elsif defined? ::Sequel
|
119
|
-
:sequel
|
120
|
-
elsif defined? ::Moped
|
121
|
-
:moped
|
122
|
-
elsif defined? ::Ohm
|
123
|
-
:ohm
|
124
|
-
elsif defined? ::Redis
|
125
|
-
:redis
|
126
|
-
elsif defined? ::Neo4j
|
127
|
-
:neo4j
|
128
|
-
end
|
129
|
-
end
|
130
|
-
|
131
|
-
private
|
132
|
-
|
133
|
-
def orm_module
|
134
|
-
::DatabaseCleaner.orm_module(orm)
|
135
|
-
end
|
136
|
-
|
137
|
-
def orm_strategy(strategy)
|
138
|
-
require "database_cleaner/#{orm.to_s}/#{strategy.to_s}"
|
139
|
-
orm_module.const_get(strategy.to_s.capitalize)
|
140
|
-
rescue LoadError
|
141
|
-
if orm_module.respond_to? :available_strategies
|
142
|
-
raise UnknownStrategySpecified, "The '#{strategy}' strategy does not exist for the #{orm} ORM! Available strategies: #{orm_module.available_strategies.join(', ')}"
|
143
|
-
else
|
144
|
-
raise UnknownStrategySpecified, "The '#{strategy}' strategy does not exist for the #{orm} ORM!"
|
145
|
-
end
|
146
|
-
end
|
147
|
-
|
148
|
-
def autodetect
|
149
|
-
@autodetected = true
|
150
|
-
|
151
|
-
@orm ||= autodetect_orm ||
|
152
|
-
raise(NoORMDetected, "No known ORM was detected! Is ActiveRecord, DataMapper, Sequel, MongoMapper, Mongoid, Moped, or CouchPotato, Redis or Ohm loaded?")
|
153
|
-
end
|
154
|
-
|
155
|
-
def set_default_orm_strategy
|
156
|
-
case orm
|
157
|
-
when :active_record, :data_mapper, :sequel
|
158
|
-
self.strategy = :transaction
|
159
|
-
when :mongo_mapper, :mongoid, :couch_potato, :moped, :ohm, :redis
|
160
|
-
self.strategy = :truncation
|
161
|
-
when :neo4j
|
162
|
-
self.strategy = :transaction
|
163
|
-
end
|
164
|
-
end
|
165
|
-
end
|
166
|
-
end
|