database_cleaner 1.5.1 → 1.6.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.
- data/Gemfile.lock +19 -16
- data/History.rdoc +42 -0
- data/README.markdown +79 -9
- data/VERSION.yml +2 -2
- data/examples/Gemfile.lock +19 -16
- data/examples/lib/redis_models.rb +3 -3
- data/lib/database_cleaner/active_record/base.rb +8 -0
- data/lib/database_cleaner/active_record/deletion.rb +22 -2
- data/lib/database_cleaner/active_record/truncation.rb +3 -3
- data/lib/database_cleaner/base.rb +6 -5
- data/lib/database_cleaner/mongo/truncation_mixin.rb +6 -2
- data/lib/database_cleaner/mongo2/truncation_mixin.rb +6 -4
- data/lib/database_cleaner/mongoid/truncation.rb +1 -1
- data/lib/database_cleaner/null_strategy.rb +5 -0
- data/lib/database_cleaner/redis/base.rb +9 -3
- data/spec/database_cleaner/active_record/truncation_spec.rb +3 -3
- data/spec/database_cleaner/base_spec.rb +34 -21
- data/spec/database_cleaner/mongo_mapper/truncation_spec.rb +2 -2
- data/spec/database_cleaner/null_strategy_spec.rb +28 -0
- data/spec/database_cleaner/redis/base_spec.rb +15 -4
- data/spec/database_cleaner/redis/truncation_spec.rb +1 -1
- data/spec/support/active_record/postgresql_setup.rb +1 -1
- metadata +122 -203
- checksums.yaml +0 -7
- data/examples/config/database.yml +0 -7
- data/examples/db/activerecord_one.db +0 -0
- data/examples/db/activerecord_two.db +0 -0
- data/examples/db/datamapper_default.db +0 -0
- data/examples/db/datamapper_one.db +0 -0
- data/examples/db/datamapper_two.db +0 -0
- /data/{features → examples/features}/step_definitions/ohm_steps.rb +0 -0
- /data/{features → examples/features}/step_definitions/redis_steps.rb +0 -0
data/Gemfile.lock
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
PATH
|
|
2
2
|
remote: .
|
|
3
3
|
specs:
|
|
4
|
-
database_cleaner (1.
|
|
4
|
+
database_cleaner (1.6.0)
|
|
5
5
|
|
|
6
6
|
GEM
|
|
7
7
|
remote: https://rubygems.org/
|
|
@@ -69,7 +69,7 @@ GEM
|
|
|
69
69
|
dm-transactions (~> 1.2.0)
|
|
70
70
|
dm-types (~> 1.2.0)
|
|
71
71
|
dm-validations (~> 1.2.0)
|
|
72
|
-
diff-lcs (1.
|
|
72
|
+
diff-lcs (1.2.5)
|
|
73
73
|
dm-aggregates (1.2.0)
|
|
74
74
|
dm-core (~> 1.2.0)
|
|
75
75
|
dm-constraints (1.2.0)
|
|
@@ -210,7 +210,7 @@ GEM
|
|
|
210
210
|
activesupport (= 3.0.0)
|
|
211
211
|
rake (>= 0.8.4)
|
|
212
212
|
thor (~> 0.14.0)
|
|
213
|
-
rake (
|
|
213
|
+
rake (11.1.2)
|
|
214
214
|
rb-fsevent (0.9.4)
|
|
215
215
|
rb-inotify (0.9.5)
|
|
216
216
|
ffi (>= 0.5.0)
|
|
@@ -222,19 +222,22 @@ GEM
|
|
|
222
222
|
rest-client (1.6.8)
|
|
223
223
|
mime-types (~> 1.16)
|
|
224
224
|
rdoc (>= 2.4.2)
|
|
225
|
-
rspec (2.
|
|
226
|
-
rspec-core (~> 2.
|
|
227
|
-
rspec-expectations (~> 2.
|
|
228
|
-
rspec-mocks (~> 2.
|
|
229
|
-
rspec-core (2.
|
|
230
|
-
rspec-expectations (2.
|
|
231
|
-
diff-lcs (
|
|
232
|
-
rspec-mocks (2.
|
|
233
|
-
rspec-rails (2.
|
|
225
|
+
rspec (2.14.1)
|
|
226
|
+
rspec-core (~> 2.14.0)
|
|
227
|
+
rspec-expectations (~> 2.14.0)
|
|
228
|
+
rspec-mocks (~> 2.14.0)
|
|
229
|
+
rspec-core (2.14.8)
|
|
230
|
+
rspec-expectations (2.14.5)
|
|
231
|
+
diff-lcs (>= 1.1.3, < 2.0)
|
|
232
|
+
rspec-mocks (2.14.6)
|
|
233
|
+
rspec-rails (2.14.2)
|
|
234
234
|
actionpack (>= 3.0)
|
|
235
|
+
activemodel (>= 3.0)
|
|
235
236
|
activesupport (>= 3.0)
|
|
236
237
|
railties (>= 3.0)
|
|
237
|
-
rspec (~> 2.
|
|
238
|
+
rspec-core (~> 2.14.0)
|
|
239
|
+
rspec-expectations (~> 2.14.0)
|
|
240
|
+
rspec-mocks (~> 2.14.0)
|
|
238
241
|
sequel (3.21.0)
|
|
239
242
|
slop (3.6.0)
|
|
240
243
|
spoon (0.0.4)
|
|
@@ -242,7 +245,7 @@ GEM
|
|
|
242
245
|
sqlite3 (1.3.10)
|
|
243
246
|
stringex (1.5.1)
|
|
244
247
|
thor (0.14.6)
|
|
245
|
-
tzinfo (0.3.
|
|
248
|
+
tzinfo (0.3.48)
|
|
246
249
|
uuidtools (2.1.5)
|
|
247
250
|
zip (2.0.2)
|
|
248
251
|
|
|
@@ -274,10 +277,10 @@ DEPENDENCIES
|
|
|
274
277
|
ohm (~> 0.1.3)
|
|
275
278
|
pg
|
|
276
279
|
rake
|
|
277
|
-
rspec-rails (~> 2.
|
|
280
|
+
rspec-rails (~> 2.14.2)
|
|
278
281
|
sequel (~> 3.21.0)
|
|
279
282
|
sqlite3
|
|
280
283
|
tzinfo
|
|
281
284
|
|
|
282
285
|
BUNDLED WITH
|
|
283
|
-
1.
|
|
286
|
+
1.14.6
|
data/History.rdoc
CHANGED
|
@@ -1,5 +1,47 @@
|
|
|
1
1
|
== Development (master)
|
|
2
2
|
|
|
3
|
+
== 1.5.3 2016-04-22
|
|
4
|
+
|
|
5
|
+
=== Bug Fixes
|
|
6
|
+
* @P9GIT fixed issue #436
|
|
7
|
+
|
|
8
|
+
== 1.5.2 2016-04-17
|
|
9
|
+
|
|
10
|
+
=== New Features/Changes
|
|
11
|
+
* Use default_client with mongoid 5.0 (@stjhimy)
|
|
12
|
+
* Added comparable support for strategies (@st0012)
|
|
13
|
+
* Better README instructions that suggest `append_after` (@jrochkind)
|
|
14
|
+
* Removed deprecation warnings for Rails 5.0+ (@pschambacher)
|
|
15
|
+
* Upgrade to RSpec 2.14 (@skalee)
|
|
16
|
+
|
|
17
|
+
=== Bug Fixes
|
|
18
|
+
* @dmitrypol fixed issue #409
|
|
19
|
+
* @schmierkov fixed the Travis builds with PR #416
|
|
20
|
+
* @shosti fixed issue #345
|
|
21
|
+
* @kylev fixed issue #379
|
|
22
|
+
* @skalee fixed the Travis builds for Neo4j with PR #433
|
|
23
|
+
|
|
24
|
+
== 1.5.1 2015-09-05
|
|
25
|
+
|
|
26
|
+
== Bug Fix
|
|
27
|
+
* Added mongo2 missing files to the gemspec. (@geerzo)
|
|
28
|
+
|
|
29
|
+
== 1.5.0 2015-09-02
|
|
30
|
+
|
|
31
|
+
=== New Features/Changes
|
|
32
|
+
* Use ensure within the cleaning method. (@cema-sp)
|
|
33
|
+
* Restored mysql2 + jruby support. (@ahorek)
|
|
34
|
+
* Added required ruby version to gemspec. (@garethrees)
|
|
35
|
+
* Added support for Mongoid 5.0 and Mongo Ruby Driver 2.0. (@jprincipe)
|
|
36
|
+
* Added support for additional Neo4j::Session.open options. (@brienw)
|
|
37
|
+
* And a bunch of code style and README improvements
|
|
38
|
+
|
|
39
|
+
=== Bug Fixes
|
|
40
|
+
* Fixed truncation for MongoDB 3. (@andreale)
|
|
41
|
+
* Fixed YAML error when building gem. (@joshnesbitt)
|
|
42
|
+
* Fixed deletion strategy for JDBC MySQL. (@DanElbert)
|
|
43
|
+
* Fixed open transactions for multiple connections. (@claptimes5)
|
|
44
|
+
|
|
3
45
|
== 1.4.1 2015-03-09
|
|
4
46
|
* Support for deletion with Sequel. (@cyberdelia)
|
|
5
47
|
* Use ActiveRecord::Base configuration when different from config file. (@wendy0402)
|
data/README.markdown
CHANGED
|
@@ -1,13 +1,25 @@
|
|
|
1
1
|
# Database Cleaner
|
|
2
2
|
|
|
3
|
+
[](https://travis-ci.org/DatabaseCleaner/database_cleaner)
|
|
4
|
+
[](https://codeclimate.com/github/DatabaseCleaner/database_cleaner)
|
|
5
|
+
|
|
3
6
|
Database Cleaner is a set of strategies for cleaning your database in Ruby.
|
|
4
7
|
|
|
5
8
|
The original use case was to ensure a clean state during tests.
|
|
6
9
|
Each strategy is a small amount of code but is code that is usually needed in any ruby app that is testing with a database.
|
|
7
10
|
|
|
8
|
-
|
|
11
|
+
## Gem Setup
|
|
12
|
+
|
|
13
|
+
```ruby
|
|
14
|
+
# Gemfile
|
|
15
|
+
group :test do
|
|
16
|
+
gem 'database_cleaner'
|
|
17
|
+
end
|
|
18
|
+
```
|
|
9
19
|
|
|
10
|
-
|
|
20
|
+
## Supported Databases, Libraries and Strategies
|
|
21
|
+
|
|
22
|
+
ActiveRecord, DataMapper, Sequel, MongoMapper, Mongoid, CouchPotato, Ohm and Redis are supported.
|
|
11
23
|
|
|
12
24
|
Here is an overview of the strategies supported for each library:
|
|
13
25
|
|
|
@@ -76,7 +88,7 @@ Here is an overview of the strategies supported for each library:
|
|
|
76
88
|
</tbody>
|
|
77
89
|
</table>
|
|
78
90
|
|
|
79
|
-
|
|
91
|
+
\* Truncation and Deletion strategies for Neo4j will just delete all nodes and relationships from the database.
|
|
80
92
|
|
|
81
93
|
<table>
|
|
82
94
|
<tbody>
|
|
@@ -154,7 +166,7 @@ passed to [`keys`](http://redis.io/commands/keys)).
|
|
|
154
166
|
|
|
155
167
|
(I should point out the truncation strategy will never truncate your schema_migrations table.)
|
|
156
168
|
|
|
157
|
-
Some strategies need to be started before tests are run (for example the `:transaction` strategy needs to know to open up a transaction). This can be accomplished by calling `DatabaseCleaner.start` at the beginning of the run, or by running the tests inside a block to `
|
|
169
|
+
Some strategies need to be started before tests are run (for example the `:transaction` strategy needs to know to open up a transaction). This can be accomplished by calling `DatabaseCleaner.start` at the beginning of the run, or by running the tests inside a block to `DatabaseCleaner.cleaning`. So you would have:
|
|
158
170
|
|
|
159
171
|
```ruby
|
|
160
172
|
require 'database_cleaner'
|
|
@@ -221,23 +233,81 @@ end
|
|
|
221
233
|
|
|
222
234
|
### RSpec with Capybara Example
|
|
223
235
|
|
|
224
|
-
|
|
236
|
+
You'll typically discover a feature spec is incorrectly using transaction
|
|
237
|
+
instead of truncation strategy when the data created in the spec is not
|
|
238
|
+
visible in the app-under-test.
|
|
239
|
+
|
|
240
|
+
A frequently occurring example of this is when, after creating a user in a
|
|
241
|
+
spec, the spec mysteriously fails to login with the user. This happens because
|
|
242
|
+
the user is created inside of an uncommitted transaction on one database
|
|
243
|
+
connection, while the login attempt is made using a separate database
|
|
244
|
+
connection. This separate database connection cannot access the
|
|
245
|
+
uncommitted user data created over the first database connection due to
|
|
246
|
+
transaction isolation.
|
|
247
|
+
|
|
248
|
+
For feature specs using a Capybara driver for an external
|
|
249
|
+
JavaScript-capable browser (in practice this is all drivers except
|
|
250
|
+
`:rack_test`), the Rack app under test and the specs do not share a
|
|
251
|
+
database connection.
|
|
252
|
+
|
|
253
|
+
When a spec and app-under-test do not share a database connection,
|
|
254
|
+
you'll likely need to use the truncation strategy instead of the
|
|
255
|
+
transaction strategy.
|
|
256
|
+
|
|
257
|
+
See the suggested config below to temporarily enable truncation strategy
|
|
258
|
+
for affected feature specs only. This config continues to use transaction
|
|
259
|
+
strategy for all other specs.
|
|
260
|
+
|
|
261
|
+
It's also recommended to use `append_after` to ensure `DatabaseCleaner.clean`
|
|
262
|
+
runs *after* the after-test cleanup `capybara/rspec` installs.
|
|
225
263
|
|
|
226
264
|
```ruby
|
|
265
|
+
require 'capybara/rspec'
|
|
266
|
+
|
|
267
|
+
#...
|
|
268
|
+
|
|
227
269
|
RSpec.configure do |config|
|
|
228
270
|
|
|
229
271
|
config.use_transactional_fixtures = false
|
|
230
272
|
|
|
231
273
|
config.before(:suite) do
|
|
274
|
+
if config.use_transactional_fixtures?
|
|
275
|
+
raise(<<-MSG)
|
|
276
|
+
Delete line `config.use_transactional_fixtures = true` from rails_helper.rb
|
|
277
|
+
(or set it to false) to prevent uncommitted transactions being used in
|
|
278
|
+
JavaScript-dependent specs.
|
|
279
|
+
|
|
280
|
+
During testing, the app-under-test that the browser driver connects to
|
|
281
|
+
uses a different database connection to the database connection used by
|
|
282
|
+
the spec. The app's database connection would not be able to access
|
|
283
|
+
uncommitted transaction data setup over the spec's database connection.
|
|
284
|
+
MSG
|
|
285
|
+
end
|
|
232
286
|
DatabaseCleaner.clean_with(:truncation)
|
|
233
287
|
end
|
|
234
288
|
|
|
235
|
-
config.before(:each) do
|
|
236
|
-
DatabaseCleaner.strategy =
|
|
289
|
+
config.before(:each) do
|
|
290
|
+
DatabaseCleaner.strategy = :transaction
|
|
291
|
+
end
|
|
292
|
+
|
|
293
|
+
config.before(:each, type: :feature) do
|
|
294
|
+
# :rack_test driver's Rack app under test shares database connection
|
|
295
|
+
# with the specs, so continue to use transaction strategy for speed.
|
|
296
|
+
driver_shares_db_connection_with_specs = Capybara.current_driver == :rack_test
|
|
297
|
+
|
|
298
|
+
if !driver_shares_db_connection_with_specs
|
|
299
|
+
# Driver is probably for an external browser with an app
|
|
300
|
+
# under test that does *not* share a database connection with the
|
|
301
|
+
# specs, so use truncation strategy.
|
|
302
|
+
DatabaseCleaner.strategy = :truncation
|
|
303
|
+
end
|
|
304
|
+
end
|
|
305
|
+
|
|
306
|
+
config.before(:each) do
|
|
237
307
|
DatabaseCleaner.start
|
|
238
308
|
end
|
|
239
309
|
|
|
240
|
-
config.
|
|
310
|
+
config.append_after(:each) do
|
|
241
311
|
DatabaseCleaner.clean
|
|
242
312
|
end
|
|
243
313
|
|
|
@@ -412,7 +482,7 @@ For ActiveRecord, you add the following parameter in your database.yml file:
|
|
|
412
482
|
test:
|
|
413
483
|
adapter: postgresql
|
|
414
484
|
# ...
|
|
415
|
-
min_messages: WARNING
|
|
485
|
+
min_messages: WARNING
|
|
416
486
|
</pre>
|
|
417
487
|
|
|
418
488
|
### Nothing happens in JRuby with Sequel using transactions
|
data/VERSION.yml
CHANGED
data/examples/Gemfile.lock
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
PATH
|
|
2
2
|
remote: .
|
|
3
3
|
specs:
|
|
4
|
-
database_cleaner (1.
|
|
4
|
+
database_cleaner (1.6.0)
|
|
5
5
|
|
|
6
6
|
GEM
|
|
7
7
|
remote: https://rubygems.org/
|
|
@@ -69,7 +69,7 @@ GEM
|
|
|
69
69
|
dm-transactions (~> 1.2.0)
|
|
70
70
|
dm-types (~> 1.2.0)
|
|
71
71
|
dm-validations (~> 1.2.0)
|
|
72
|
-
diff-lcs (1.
|
|
72
|
+
diff-lcs (1.2.5)
|
|
73
73
|
dm-aggregates (1.2.0)
|
|
74
74
|
dm-core (~> 1.2.0)
|
|
75
75
|
dm-constraints (1.2.0)
|
|
@@ -210,7 +210,7 @@ GEM
|
|
|
210
210
|
activesupport (= 3.0.0)
|
|
211
211
|
rake (>= 0.8.4)
|
|
212
212
|
thor (~> 0.14.0)
|
|
213
|
-
rake (
|
|
213
|
+
rake (11.1.2)
|
|
214
214
|
rb-fsevent (0.9.4)
|
|
215
215
|
rb-inotify (0.9.5)
|
|
216
216
|
ffi (>= 0.5.0)
|
|
@@ -222,19 +222,22 @@ GEM
|
|
|
222
222
|
rest-client (1.6.8)
|
|
223
223
|
mime-types (~> 1.16)
|
|
224
224
|
rdoc (>= 2.4.2)
|
|
225
|
-
rspec (2.
|
|
226
|
-
rspec-core (~> 2.
|
|
227
|
-
rspec-expectations (~> 2.
|
|
228
|
-
rspec-mocks (~> 2.
|
|
229
|
-
rspec-core (2.
|
|
230
|
-
rspec-expectations (2.
|
|
231
|
-
diff-lcs (
|
|
232
|
-
rspec-mocks (2.
|
|
233
|
-
rspec-rails (2.
|
|
225
|
+
rspec (2.14.1)
|
|
226
|
+
rspec-core (~> 2.14.0)
|
|
227
|
+
rspec-expectations (~> 2.14.0)
|
|
228
|
+
rspec-mocks (~> 2.14.0)
|
|
229
|
+
rspec-core (2.14.8)
|
|
230
|
+
rspec-expectations (2.14.5)
|
|
231
|
+
diff-lcs (>= 1.1.3, < 2.0)
|
|
232
|
+
rspec-mocks (2.14.6)
|
|
233
|
+
rspec-rails (2.14.2)
|
|
234
234
|
actionpack (>= 3.0)
|
|
235
|
+
activemodel (>= 3.0)
|
|
235
236
|
activesupport (>= 3.0)
|
|
236
237
|
railties (>= 3.0)
|
|
237
|
-
rspec (~> 2.
|
|
238
|
+
rspec-core (~> 2.14.0)
|
|
239
|
+
rspec-expectations (~> 2.14.0)
|
|
240
|
+
rspec-mocks (~> 2.14.0)
|
|
238
241
|
sequel (3.21.0)
|
|
239
242
|
slop (3.6.0)
|
|
240
243
|
spoon (0.0.4)
|
|
@@ -242,7 +245,7 @@ GEM
|
|
|
242
245
|
sqlite3 (1.3.10)
|
|
243
246
|
stringex (1.5.1)
|
|
244
247
|
thor (0.14.6)
|
|
245
|
-
tzinfo (0.3.
|
|
248
|
+
tzinfo (0.3.48)
|
|
246
249
|
uuidtools (2.1.5)
|
|
247
250
|
zip (2.0.2)
|
|
248
251
|
|
|
@@ -274,10 +277,10 @@ DEPENDENCIES
|
|
|
274
277
|
ohm (~> 0.1.3)
|
|
275
278
|
pg
|
|
276
279
|
rake
|
|
277
|
-
rspec-rails (~> 2.
|
|
280
|
+
rspec-rails (~> 2.14.2)
|
|
278
281
|
sequel (~> 3.21.0)
|
|
279
282
|
sqlite3
|
|
280
283
|
tzinfo
|
|
281
284
|
|
|
282
285
|
BUNDLED WITH
|
|
283
|
-
1.
|
|
286
|
+
1.14.6
|
|
@@ -3,7 +3,7 @@ require 'redis'
|
|
|
3
3
|
class RedisWidget
|
|
4
4
|
|
|
5
5
|
def self.redis
|
|
6
|
-
threaded ||= Redis.
|
|
6
|
+
threaded ||= Redis.new
|
|
7
7
|
end
|
|
8
8
|
|
|
9
9
|
def self.redis=(connection)
|
|
@@ -45,7 +45,7 @@ end
|
|
|
45
45
|
class RedisWidgetUsingDatabaseOne < RedisWidget
|
|
46
46
|
|
|
47
47
|
def self.redis
|
|
48
|
-
threaded[self.class.to_s] ||= Redis.
|
|
48
|
+
threaded[self.class.to_s] ||= Redis.new :url => ENV['REDIS_URL_ONE']
|
|
49
49
|
end
|
|
50
50
|
|
|
51
51
|
def self.create!
|
|
@@ -56,7 +56,7 @@ end
|
|
|
56
56
|
class RedisWidgetUsingDatabaseTwo < RedisWidget
|
|
57
57
|
|
|
58
58
|
def self.redis
|
|
59
|
-
threaded[self.class.to_s] ||= Redis.
|
|
59
|
+
threaded[self.class.to_s] ||= Redis.new :url => ENV['REDIS_URL_TWO']
|
|
60
60
|
end
|
|
61
61
|
|
|
62
62
|
def self.create!
|
|
@@ -57,6 +57,14 @@ module DatabaseCleaner
|
|
|
57
57
|
end
|
|
58
58
|
end
|
|
59
59
|
|
|
60
|
+
def self.migration_table_name
|
|
61
|
+
if ::ActiveRecord::VERSION::MAJOR < 5
|
|
62
|
+
::ActiveRecord::Migrator.schema_migrations_table_name
|
|
63
|
+
else
|
|
64
|
+
::ActiveRecord::SchemaMigration.table_name
|
|
65
|
+
end
|
|
66
|
+
end
|
|
67
|
+
|
|
60
68
|
private
|
|
61
69
|
|
|
62
70
|
def lookup_from_connection_pool
|
|
@@ -54,11 +54,31 @@ module DatabaseCleaner::ActiveRecord
|
|
|
54
54
|
|
|
55
55
|
def tables_with_new_rows(connection)
|
|
56
56
|
@db_name ||= connection.instance_variable_get('@config')[:database]
|
|
57
|
-
|
|
58
|
-
|
|
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
|
+
@table_stats_query = connection.select_values(<<-SQL).join(' UNION ')
|
|
70
|
+
SELECT CONCAT('SELECT \"', table_name, '\" AS table_name, COUNT(*) AS exact_row_count FROM ', table_name)
|
|
71
|
+
FROM
|
|
72
|
+
INFORMATION_SCHEMA.TABLES
|
|
73
|
+
WHERE
|
|
74
|
+
table_schema = '#{db_name}'
|
|
75
|
+
AND table_name <> '#{migration_table_name}';
|
|
76
|
+
SQL
|
|
77
|
+
end
|
|
59
78
|
end
|
|
60
79
|
|
|
61
80
|
def information_schema_exists? connection
|
|
81
|
+
return false unless connection.is_a? ActiveRecord::ConnectionAdapters::Mysql2Adapter
|
|
62
82
|
@information_schema_exists ||=
|
|
63
83
|
begin
|
|
64
84
|
connection.execute("SELECT 1 FROM information_schema.tables")
|
|
@@ -27,7 +27,7 @@ module DatabaseCleaner
|
|
|
27
27
|
|
|
28
28
|
def database_cleaner_table_cache
|
|
29
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 ||= tables
|
|
30
|
+
@database_cleaner_tables ||= ::ActiveRecord::VERSION::MAJOR >= 5 ? data_sources : tables
|
|
31
31
|
end
|
|
32
32
|
|
|
33
33
|
def truncate_table(table_name)
|
|
@@ -192,7 +192,7 @@ module DatabaseCleaner
|
|
|
192
192
|
FROM pg_tables
|
|
193
193
|
WHERE
|
|
194
194
|
tablename !~ '_prt_' AND
|
|
195
|
-
tablename <> '#{::ActiveRecord::
|
|
195
|
+
tablename <> '#{::DatabaseCleaner::ActiveRecord::Base.migration_table_name}' AND
|
|
196
196
|
schemaname = ANY (current_schemas(false))
|
|
197
197
|
_SQL
|
|
198
198
|
rows.collect { |result| result.first }
|
|
@@ -257,7 +257,7 @@ module DatabaseCleaner::ActiveRecord
|
|
|
257
257
|
|
|
258
258
|
# overwritten
|
|
259
259
|
def migration_storage_names
|
|
260
|
-
[::ActiveRecord::
|
|
260
|
+
[::DatabaseCleaner::ActiveRecord::Base.migration_table_name]
|
|
261
261
|
end
|
|
262
262
|
|
|
263
263
|
def cache_tables?
|
|
@@ -1,6 +1,12 @@
|
|
|
1
1
|
require 'database_cleaner/null_strategy'
|
|
2
2
|
module DatabaseCleaner
|
|
3
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
|
+
|
|
4
10
|
def initialize(desired_orm = nil,opts = {})
|
|
5
11
|
if [:autodetect, nil, "autodetect"].include?(desired_orm)
|
|
6
12
|
autodetect
|
|
@@ -96,11 +102,6 @@ module DatabaseCleaner
|
|
|
96
102
|
!!@autodetected
|
|
97
103
|
end
|
|
98
104
|
|
|
99
|
-
#TODO make strategies directly comparable
|
|
100
|
-
def ==(other)
|
|
101
|
-
self.orm == other.orm && self.db == other.db
|
|
102
|
-
end
|
|
103
|
-
|
|
104
105
|
def autodetect_orm
|
|
105
106
|
if defined? ::ActiveRecord
|
|
106
107
|
:active_record
|
|
@@ -4,9 +4,9 @@ module DatabaseCleaner
|
|
|
4
4
|
|
|
5
5
|
def clean
|
|
6
6
|
if @only
|
|
7
|
-
collections.each { |c| c.
|
|
7
|
+
collections.each { |c| c.send(truncate_method_name) if @only.include?(c.name) }
|
|
8
8
|
else
|
|
9
|
-
collections.each { |c| c.
|
|
9
|
+
collections.each { |c| c.send(truncate_method_name) unless @tables_to_exclude.include?(c.name) }
|
|
10
10
|
end
|
|
11
11
|
true
|
|
12
12
|
end
|
|
@@ -17,6 +17,10 @@ module DatabaseCleaner
|
|
|
17
17
|
database.collections.select { |c| c.name !~ /^system\./ }
|
|
18
18
|
end
|
|
19
19
|
|
|
20
|
+
def truncate_method_name
|
|
21
|
+
# This constant only exists in the 2.x series.
|
|
22
|
+
defined?(::Mongo::VERSION) ? :delete_many : :remove
|
|
23
|
+
end
|
|
20
24
|
end
|
|
21
25
|
end
|
|
22
26
|
end
|
|
@@ -26,10 +26,12 @@ module DatabaseCleaner
|
|
|
26
26
|
database.use(db)
|
|
27
27
|
end
|
|
28
28
|
|
|
29
|
-
database
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
29
|
+
database.collections.collect { |c| c.namespace.split('.',2)[1] }
|
|
30
|
+
|
|
31
|
+
# database['system.namespaces'].find(:name => { '$not' => /\.system\.|\$/ }).to_a.map do |collection|
|
|
32
|
+
# _, name = collection['name'].split('.', 2)
|
|
33
|
+
# name
|
|
34
|
+
# end
|
|
33
35
|
end
|
|
34
36
|
|
|
35
37
|
end
|
|
@@ -22,10 +22,16 @@ module DatabaseCleaner
|
|
|
22
22
|
private
|
|
23
23
|
|
|
24
24
|
def connection
|
|
25
|
-
@connection ||=
|
|
25
|
+
@connection ||= begin
|
|
26
|
+
if url == :default
|
|
27
|
+
::Redis.new
|
|
28
|
+
elsif db.is_a?(::Redis) # pass directly the connection
|
|
29
|
+
db
|
|
30
|
+
else
|
|
31
|
+
::Redis.new(:url => url)
|
|
32
|
+
end
|
|
33
|
+
end
|
|
26
34
|
end
|
|
27
|
-
|
|
28
35
|
end
|
|
29
36
|
end
|
|
30
37
|
end
|
|
31
|
-
|
|
@@ -40,7 +40,7 @@ module DatabaseCleaner
|
|
|
40
40
|
Truncation.new.clean
|
|
41
41
|
end
|
|
42
42
|
|
|
43
|
-
it "should use ActiveRecord's
|
|
43
|
+
it "should use ActiveRecord's SchemaMigration.table_name" do
|
|
44
44
|
connection.stub(:database_cleaner_table_cache).and_return(%w[pre_schema_migrations_suf widgets dogs])
|
|
45
45
|
::ActiveRecord::Base.stub(:table_name_prefix).and_return('pre_')
|
|
46
46
|
::ActiveRecord::Base.stub(:table_name_suffix).and_return('_suf')
|
|
@@ -118,7 +118,7 @@ module DatabaseCleaner
|
|
|
118
118
|
connection.should_receive(:database_cleaner_table_cache).and_return([])
|
|
119
119
|
connection.should_not_receive(:tables)
|
|
120
120
|
|
|
121
|
-
connection.stub
|
|
121
|
+
connection.stub(:truncate_tables)
|
|
122
122
|
Truncation.new({ :cache_tables => true }).clean
|
|
123
123
|
end
|
|
124
124
|
end
|
|
@@ -128,7 +128,7 @@ module DatabaseCleaner
|
|
|
128
128
|
connection.should_not_receive(:database_cleaner_table_cache)
|
|
129
129
|
connection.should_receive(:tables).and_return([])
|
|
130
130
|
|
|
131
|
-
connection.stub
|
|
131
|
+
connection.stub(:truncate_tables)
|
|
132
132
|
Truncation.new({ :cache_tables => false }).clean
|
|
133
133
|
end
|
|
134
134
|
end
|