database_cleaner 1.6.2 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +5 -5
- data/lib/database_cleaner.rb +1 -8
- metadata +18 -498
- data/CONTRIBUTE.markdown +0 -28
- data/Gemfile.lock +0 -286
- data/History.rdoc +0 -430
- data/LICENSE +0 -20
- data/README.markdown +0 -518
- data/Rakefile +0 -40
- data/TODO +0 -3
- data/VERSION.yml +0 -4
- data/cucumber.yml +0 -1
- data/examples/Gemfile +0 -1
- data/examples/Gemfile.lock +0 -1
- data/examples/config/database.yml.example +0 -8
- data/examples/config/redis.yml +0 -8
- data/examples/db/sqlite_databases_go_here +0 -0
- data/examples/features/example.feature +0 -11
- data/examples/features/example_multiple_db.feature +0 -23
- data/examples/features/example_multiple_orm.feature +0 -22
- data/examples/features/step_definitions/activerecord_steps.rb +0 -31
- data/examples/features/step_definitions/couchpotato_steps.rb +0 -31
- data/examples/features/step_definitions/datamapper_steps.rb +0 -37
- data/examples/features/step_definitions/mongoid_steps.rb +0 -23
- data/examples/features/step_definitions/mongomapper_steps.rb +0 -31
- data/examples/features/step_definitions/neo4j_steps.rb +0 -23
- data/examples/features/step_definitions/ohm_steps.rb +0 -31
- data/examples/features/step_definitions/redis_steps.rb +0 -31
- data/examples/features/step_definitions/translation_steps.rb +0 -55
- data/examples/features/support/env.rb +0 -61
- data/examples/lib/activerecord_models.rb +0 -41
- data/examples/lib/couchpotato_models.rb +0 -61
- data/examples/lib/datamapper_models.rb +0 -50
- data/examples/lib/mongoid_models.rb +0 -44
- data/examples/lib/mongomapper_models.rb +0 -51
- data/examples/lib/neo4j_models.rb +0 -17
- data/examples/lib/ohm_models.rb +0 -43
- data/examples/lib/redis_models.rb +0 -65
- data/examples/lib/sequel_models.rb +0 -9
- data/features/cleaning.feature +0 -30
- data/features/cleaning_default_strategy.feature +0 -23
- data/features/cleaning_multiple_dbs.feature +0 -22
- data/features/cleaning_multiple_orms.feature +0 -67
- data/features/step_definitions/database_cleaner_steps.rb +0 -33
- data/features/support/env.rb +0 -4
- data/features/support/feature_runner.rb +0 -39
- data/lib/database_cleaner/active_record/base.rb +0 -92
- data/lib/database_cleaner/active_record/deletion.rb +0 -106
- 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 -164
- 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/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
- data/spec/database_cleaner/active_record/base_spec.rb +0 -188
- data/spec/database_cleaner/active_record/transaction_spec.rb +0 -176
- data/spec/database_cleaner/active_record/truncation/mysql2_spec.rb +0 -38
- data/spec/database_cleaner/active_record/truncation/mysql_spec.rb +0 -38
- data/spec/database_cleaner/active_record/truncation/postgresql_spec.rb +0 -75
- data/spec/database_cleaner/active_record/truncation/shared_fast_truncation.rb +0 -40
- data/spec/database_cleaner/active_record/truncation/sqlite3_spec.rb +0 -40
- data/spec/database_cleaner/active_record/truncation_spec.rb +0 -180
- data/spec/database_cleaner/base_spec.rb +0 -617
- data/spec/database_cleaner/configuration_spec.rb +0 -345
- data/spec/database_cleaner/couch_potato/truncation_spec.rb +0 -41
- data/spec/database_cleaner/data_mapper/base_spec.rb +0 -30
- data/spec/database_cleaner/data_mapper/transaction_spec.rb +0 -23
- data/spec/database_cleaner/data_mapper/truncation/sqlite3_spec.rb +0 -41
- data/spec/database_cleaner/data_mapper/truncation_spec.rb +0 -11
- data/spec/database_cleaner/generic/base_spec.rb +0 -61
- data/spec/database_cleaner/generic/truncation_spec.rb +0 -118
- data/spec/database_cleaner/mongo/mongo_examples.rb +0 -26
- data/spec/database_cleaner/mongo/truncation_spec.rb +0 -72
- data/spec/database_cleaner/mongo_mapper/base_spec.rb +0 -33
- data/spec/database_cleaner/mongo_mapper/mongo_examples.rb +0 -8
- data/spec/database_cleaner/mongo_mapper/truncation_spec.rb +0 -74
- data/spec/database_cleaner/moped/moped_examples.rb +0 -32
- data/spec/database_cleaner/moped/truncation_spec.rb +0 -80
- data/spec/database_cleaner/neo4j/base_spec.rb +0 -43
- data/spec/database_cleaner/neo4j/transaction_spec.rb +0 -25
- data/spec/database_cleaner/null_strategy_spec.rb +0 -28
- data/spec/database_cleaner/ohm/truncation_spec.rb +0 -70
- data/spec/database_cleaner/redis/base_spec.rb +0 -43
- data/spec/database_cleaner/redis/truncation_spec.rb +0 -63
- data/spec/database_cleaner/sequel/base_spec.rb +0 -32
- data/spec/database_cleaner/sequel/deletion_spec.rb +0 -58
- data/spec/database_cleaner/sequel/transaction_spec.rb +0 -21
- data/spec/database_cleaner/sequel/truncation/sqlite3_spec.rb +0 -0
- data/spec/database_cleaner/sequel/truncation_spec.rb +0 -182
- data/spec/database_cleaner/shared_strategy.rb +0 -15
- data/spec/rcov.opts +0 -1
- data/spec/spec_helper.rb +0 -21
- data/spec/support/active_record/database_setup.rb +0 -6
- data/spec/support/active_record/migrations/20150101010000_create_users.rb +0 -14
- data/spec/support/active_record/migrations/20150101020000_create_agents.rb +0 -14
- data/spec/support/active_record/mysql2_setup.rb +0 -39
- data/spec/support/active_record/mysql_setup.rb +0 -38
- data/spec/support/active_record/postgresql_setup.rb +0 -48
- data/spec/support/active_record/schema_setup.rb +0 -17
- data/spec/support/active_record/sqlite3_setup.rb +0 -40
- data/spec/support/data_mapper/schema_setup.rb +0 -15
- data/spec/support/data_mapper/sqlite3_setup.rb +0 -39
|
@@ -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,164 +0,0 @@
|
|
|
1
|
-
require 'database_cleaner/null_strategy'
|
|
2
|
-
module DatabaseCleaner
|
|
3
|
-
class Base
|
|
4
|
-
include Comparable
|
|
5
|
-
|
|
6
|
-
def <=>(other)
|
|
7
|
-
(self.orm <=> other.orm) == 0 ? self.db <=> other.db : self.orm <=> other.orm
|
|
8
|
-
end
|
|
9
|
-
|
|
10
|
-
def initialize(desired_orm = nil,opts = {})
|
|
11
|
-
if [:autodetect, nil, "autodetect"].include?(desired_orm)
|
|
12
|
-
autodetect
|
|
13
|
-
else
|
|
14
|
-
self.orm = desired_orm
|
|
15
|
-
end
|
|
16
|
-
self.db = opts[:connection] || opts[:model] if opts.has_key?(:connection) || opts.has_key?(:model)
|
|
17
|
-
set_default_orm_strategy
|
|
18
|
-
end
|
|
19
|
-
|
|
20
|
-
def db=(desired_db)
|
|
21
|
-
self.strategy_db = desired_db
|
|
22
|
-
@db = desired_db
|
|
23
|
-
end
|
|
24
|
-
|
|
25
|
-
def strategy_db=(desired_db)
|
|
26
|
-
if strategy.respond_to? :db=
|
|
27
|
-
strategy.db = desired_db
|
|
28
|
-
elsif desired_db!= :default
|
|
29
|
-
raise ArgumentError, "You must provide a strategy object that supports non default databases when you specify a database"
|
|
30
|
-
end
|
|
31
|
-
end
|
|
32
|
-
|
|
33
|
-
def db
|
|
34
|
-
@db ||= :default
|
|
35
|
-
end
|
|
36
|
-
|
|
37
|
-
def create_strategy(*args)
|
|
38
|
-
strategy, *strategy_args = args
|
|
39
|
-
orm_strategy(strategy).new(*strategy_args)
|
|
40
|
-
end
|
|
41
|
-
|
|
42
|
-
def clean_with(*args)
|
|
43
|
-
strategy = create_strategy(*args)
|
|
44
|
-
set_strategy_db strategy, self.db
|
|
45
|
-
|
|
46
|
-
strategy.clean
|
|
47
|
-
strategy
|
|
48
|
-
end
|
|
49
|
-
|
|
50
|
-
alias clean_with! clean_with
|
|
51
|
-
|
|
52
|
-
def set_strategy_db(strategy, desired_db)
|
|
53
|
-
if strategy.respond_to? :db=
|
|
54
|
-
strategy.db = desired_db
|
|
55
|
-
elsif desired_db != :default
|
|
56
|
-
raise ArgumentError, "You must provide a strategy object that supports non default databases when you specify a database"
|
|
57
|
-
end
|
|
58
|
-
end
|
|
59
|
-
|
|
60
|
-
def strategy=(args)
|
|
61
|
-
strategy, *strategy_args = args
|
|
62
|
-
if strategy.is_a?(Symbol)
|
|
63
|
-
@strategy = create_strategy(*args)
|
|
64
|
-
elsif strategy_args.empty?
|
|
65
|
-
@strategy = strategy
|
|
66
|
-
else
|
|
67
|
-
raise ArgumentError, "You must provide a strategy object, or a symbol for a known strategy along with initialization params."
|
|
68
|
-
end
|
|
69
|
-
|
|
70
|
-
set_strategy_db @strategy, self.db
|
|
71
|
-
|
|
72
|
-
@strategy
|
|
73
|
-
end
|
|
74
|
-
|
|
75
|
-
def strategy
|
|
76
|
-
@strategy ||= NullStrategy
|
|
77
|
-
end
|
|
78
|
-
|
|
79
|
-
def orm=(desired_orm)
|
|
80
|
-
@orm = desired_orm.to_sym
|
|
81
|
-
end
|
|
82
|
-
|
|
83
|
-
def orm
|
|
84
|
-
@orm || autodetect
|
|
85
|
-
end
|
|
86
|
-
|
|
87
|
-
def start
|
|
88
|
-
strategy.start
|
|
89
|
-
end
|
|
90
|
-
|
|
91
|
-
def clean
|
|
92
|
-
strategy.clean
|
|
93
|
-
end
|
|
94
|
-
|
|
95
|
-
alias clean! clean
|
|
96
|
-
|
|
97
|
-
def cleaning(&block)
|
|
98
|
-
strategy.cleaning(&block)
|
|
99
|
-
end
|
|
100
|
-
|
|
101
|
-
def auto_detected?
|
|
102
|
-
!!@autodetected
|
|
103
|
-
end
|
|
104
|
-
|
|
105
|
-
def autodetect_orm
|
|
106
|
-
if defined? ::ActiveRecord
|
|
107
|
-
:active_record
|
|
108
|
-
elsif defined? ::DataMapper
|
|
109
|
-
:data_mapper
|
|
110
|
-
elsif defined? ::MongoMapper
|
|
111
|
-
:mongo_mapper
|
|
112
|
-
elsif defined? ::Mongoid
|
|
113
|
-
:mongoid
|
|
114
|
-
elsif defined? ::CouchPotato
|
|
115
|
-
:couch_potato
|
|
116
|
-
elsif defined? ::Sequel
|
|
117
|
-
:sequel
|
|
118
|
-
elsif defined? ::Moped
|
|
119
|
-
:moped
|
|
120
|
-
elsif defined? ::Ohm
|
|
121
|
-
:ohm
|
|
122
|
-
elsif defined? ::Redis
|
|
123
|
-
:redis
|
|
124
|
-
elsif defined? ::Neo4j
|
|
125
|
-
:neo4j
|
|
126
|
-
end
|
|
127
|
-
end
|
|
128
|
-
|
|
129
|
-
private
|
|
130
|
-
|
|
131
|
-
def orm_module
|
|
132
|
-
::DatabaseCleaner.orm_module(orm)
|
|
133
|
-
end
|
|
134
|
-
|
|
135
|
-
def orm_strategy(strategy)
|
|
136
|
-
require "database_cleaner/#{orm.to_s}/#{strategy.to_s}"
|
|
137
|
-
orm_module.const_get(strategy.to_s.capitalize)
|
|
138
|
-
rescue LoadError
|
|
139
|
-
if orm_module.respond_to? :available_strategies
|
|
140
|
-
raise UnknownStrategySpecified, "The '#{strategy}' strategy does not exist for the #{orm} ORM! Available strategies: #{orm_module.available_strategies.join(', ')}"
|
|
141
|
-
else
|
|
142
|
-
raise UnknownStrategySpecified, "The '#{strategy}' strategy does not exist for the #{orm} ORM!"
|
|
143
|
-
end
|
|
144
|
-
end
|
|
145
|
-
|
|
146
|
-
def autodetect
|
|
147
|
-
@autodetected = true
|
|
148
|
-
|
|
149
|
-
@orm ||= autodetect_orm ||
|
|
150
|
-
raise(NoORMDetected, "No known ORM was detected! Is ActiveRecord, DataMapper, Sequel, MongoMapper, Mongoid, Moped, or CouchPotato, Redis or Ohm loaded?")
|
|
151
|
-
end
|
|
152
|
-
|
|
153
|
-
def set_default_orm_strategy
|
|
154
|
-
case orm
|
|
155
|
-
when :active_record, :data_mapper, :sequel
|
|
156
|
-
self.strategy = :transaction
|
|
157
|
-
when :mongo_mapper, :mongoid, :couch_potato, :moped, :ohm, :redis
|
|
158
|
-
self.strategy = :truncation
|
|
159
|
-
when :neo4j
|
|
160
|
-
self.strategy = :transaction
|
|
161
|
-
end
|
|
162
|
-
end
|
|
163
|
-
end
|
|
164
|
-
end
|
|
@@ -1,131 +0,0 @@
|
|
|
1
|
-
require 'database_cleaner/base'
|
|
2
|
-
|
|
3
|
-
module DatabaseCleaner
|
|
4
|
-
|
|
5
|
-
class NoORMDetected < StandardError; end
|
|
6
|
-
class UnknownStrategySpecified < ArgumentError; end
|
|
7
|
-
|
|
8
|
-
class << self
|
|
9
|
-
def init_cleaners
|
|
10
|
-
@cleaners ||= {}
|
|
11
|
-
# ghetto ordered hash.. maintains 1.8 compat and old API
|
|
12
|
-
@connections ||= []
|
|
13
|
-
end
|
|
14
|
-
|
|
15
|
-
def [](orm,opts = {})
|
|
16
|
-
raise NoORMDetected unless orm
|
|
17
|
-
init_cleaners
|
|
18
|
-
# TODO: deprecate
|
|
19
|
-
# this method conflates creation with lookup. Both a command and a query. Yuck.
|
|
20
|
-
if @cleaners.has_key? [orm, opts]
|
|
21
|
-
@cleaners[[orm, opts]]
|
|
22
|
-
else
|
|
23
|
-
add_cleaner(orm, opts)
|
|
24
|
-
end
|
|
25
|
-
end
|
|
26
|
-
|
|
27
|
-
def add_cleaner(orm,opts = {})
|
|
28
|
-
init_cleaners
|
|
29
|
-
cleaner = DatabaseCleaner::Base.new(orm,opts)
|
|
30
|
-
@cleaners[[orm, opts]] = cleaner
|
|
31
|
-
@connections << cleaner
|
|
32
|
-
cleaner
|
|
33
|
-
end
|
|
34
|
-
|
|
35
|
-
def app_root=(desired_root)
|
|
36
|
-
@app_root = desired_root
|
|
37
|
-
end
|
|
38
|
-
|
|
39
|
-
def app_root
|
|
40
|
-
@app_root ||= Dir.pwd
|
|
41
|
-
end
|
|
42
|
-
|
|
43
|
-
def connections
|
|
44
|
-
# double yuck.. can't wait to deprecate this whole class...
|
|
45
|
-
unless defined?(@cleaners) && @cleaners
|
|
46
|
-
autodetected = ::DatabaseCleaner::Base.new
|
|
47
|
-
add_cleaner(autodetected.orm)
|
|
48
|
-
end
|
|
49
|
-
@connections
|
|
50
|
-
end
|
|
51
|
-
|
|
52
|
-
def logger=(log_source)
|
|
53
|
-
@logger = log_source
|
|
54
|
-
end
|
|
55
|
-
|
|
56
|
-
def logger
|
|
57
|
-
return @logger if @logger
|
|
58
|
-
|
|
59
|
-
@logger = Logger.new(STDOUT)
|
|
60
|
-
@logger.level = Logger::ERROR
|
|
61
|
-
@logger
|
|
62
|
-
end
|
|
63
|
-
|
|
64
|
-
def strategy=(stratagem)
|
|
65
|
-
connections.each { |connect| connect.strategy = stratagem }
|
|
66
|
-
remove_duplicates
|
|
67
|
-
end
|
|
68
|
-
|
|
69
|
-
def orm=(orm)
|
|
70
|
-
connections.each { |connect| connect.orm = orm }
|
|
71
|
-
remove_duplicates
|
|
72
|
-
end
|
|
73
|
-
|
|
74
|
-
def start
|
|
75
|
-
connections.each { |connection| connection.start }
|
|
76
|
-
end
|
|
77
|
-
|
|
78
|
-
def clean
|
|
79
|
-
connections.each { |connection| connection.clean }
|
|
80
|
-
end
|
|
81
|
-
|
|
82
|
-
alias clean! clean
|
|
83
|
-
|
|
84
|
-
def cleaning(&inner_block)
|
|
85
|
-
connections.inject(inner_block) do |curr_block, connection|
|
|
86
|
-
proc { connection.cleaning(&curr_block) }
|
|
87
|
-
end.call
|
|
88
|
-
end
|
|
89
|
-
|
|
90
|
-
def clean_with(*args)
|
|
91
|
-
connections.each { |connection| connection.clean_with(*args) }
|
|
92
|
-
end
|
|
93
|
-
|
|
94
|
-
alias clean_with! clean_with
|
|
95
|
-
|
|
96
|
-
def remove_duplicates
|
|
97
|
-
temp = []
|
|
98
|
-
connections.each do |connect|
|
|
99
|
-
temp.push connect unless temp.include? connect
|
|
100
|
-
end
|
|
101
|
-
@connections = temp
|
|
102
|
-
end
|
|
103
|
-
|
|
104
|
-
def orm_module(symbol)
|
|
105
|
-
case symbol
|
|
106
|
-
when :active_record
|
|
107
|
-
DatabaseCleaner::ActiveRecord
|
|
108
|
-
when :data_mapper
|
|
109
|
-
DatabaseCleaner::DataMapper
|
|
110
|
-
when :mongo
|
|
111
|
-
DatabaseCleaner::Mongo
|
|
112
|
-
when :mongoid
|
|
113
|
-
DatabaseCleaner::Mongoid
|
|
114
|
-
when :mongo_mapper
|
|
115
|
-
DatabaseCleaner::MongoMapper
|
|
116
|
-
when :moped
|
|
117
|
-
DatabaseCleaner::Moped
|
|
118
|
-
when :couch_potato
|
|
119
|
-
DatabaseCleaner::CouchPotato
|
|
120
|
-
when :sequel
|
|
121
|
-
DatabaseCleaner::Sequel
|
|
122
|
-
when :ohm
|
|
123
|
-
DatabaseCleaner::Ohm
|
|
124
|
-
when :redis
|
|
125
|
-
DatabaseCleaner::Redis
|
|
126
|
-
when :neo4j
|
|
127
|
-
DatabaseCleaner::Neo4j
|
|
128
|
-
end
|
|
129
|
-
end
|
|
130
|
-
end
|
|
131
|
-
end
|
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
require 'database_cleaner/generic/truncation'
|
|
2
|
-
|
|
3
|
-
module DatabaseCleaner
|
|
4
|
-
module CouchPotato
|
|
5
|
-
class Truncation
|
|
6
|
-
include ::DatabaseCleaner::Generic::Truncation
|
|
7
|
-
|
|
8
|
-
def initialize(options = {})
|
|
9
|
-
if options.has_key?(:only) || options.has_key?(:except)
|
|
10
|
-
raise ArgumentError, "The :only and :except options are not available for use with CouchPotato/CouchDB."
|
|
11
|
-
elsif !options.empty?
|
|
12
|
-
raise ArgumentError, "Unsupported option. You specified #{options.keys.join(',')}."
|
|
13
|
-
end
|
|
14
|
-
super
|
|
15
|
-
end
|
|
16
|
-
|
|
17
|
-
def clean
|
|
18
|
-
database.recreate!
|
|
19
|
-
end
|
|
20
|
-
|
|
21
|
-
private
|
|
22
|
-
|
|
23
|
-
def database
|
|
24
|
-
::CouchPotato.couchrest_database
|
|
25
|
-
end
|
|
26
|
-
end
|
|
27
|
-
end
|
|
28
|
-
end
|
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
require 'database_cleaner/generic/base'
|
|
2
|
-
module DatabaseCleaner
|
|
3
|
-
module DataMapper
|
|
4
|
-
def self.available_strategies
|
|
5
|
-
%w[truncation transaction]
|
|
6
|
-
end
|
|
7
|
-
|
|
8
|
-
module Base
|
|
9
|
-
include ::DatabaseCleaner::Generic::Base
|
|
10
|
-
|
|
11
|
-
def db=(desired_db)
|
|
12
|
-
@db = desired_db
|
|
13
|
-
end
|
|
14
|
-
|
|
15
|
-
def db
|
|
16
|
-
@db ||= :default
|
|
17
|
-
end
|
|
18
|
-
|
|
19
|
-
end
|
|
20
|
-
end
|
|
21
|
-
end
|
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
require 'database_cleaner/data_mapper/base'
|
|
2
|
-
require 'database_cleaner/generic/transaction'
|
|
3
|
-
|
|
4
|
-
module DatabaseCleaner::DataMapper
|
|
5
|
-
class Transaction
|
|
6
|
-
include ::DatabaseCleaner::DataMapper::Base
|
|
7
|
-
include ::DatabaseCleaner::Generic::Transaction
|
|
8
|
-
|
|
9
|
-
def start(repository = self.db)
|
|
10
|
-
::DataMapper.repository(repository) do |r|
|
|
11
|
-
transaction = DataMapper::Transaction.new(r)
|
|
12
|
-
transaction.begin
|
|
13
|
-
r.adapter.push_transaction(transaction)
|
|
14
|
-
end
|
|
15
|
-
end
|
|
16
|
-
|
|
17
|
-
def clean(repository = self.db)
|
|
18
|
-
::DataMapper.repository(repository) do |r|
|
|
19
|
-
adapter = r.adapter
|
|
20
|
-
while adapter.current_transaction
|
|
21
|
-
adapter.current_transaction.rollback
|
|
22
|
-
adapter.pop_transaction
|
|
23
|
-
end
|
|
24
|
-
end
|
|
25
|
-
end
|
|
26
|
-
|
|
27
|
-
end
|
|
28
|
-
end
|