database_cleaner 1.99.0 → 2.0.0.beta
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 -47
- metadata +14 -484
- data/CONTRIBUTE.markdown +0 -27
- data/Gemfile.lock +0 -324
- data/History.rdoc +0 -514
- data/LICENSE +0 -20
- data/README.markdown +0 -389
- data/Rakefile +0 -44
- data/TODO +0 -3
- data/adapters/database_cleaner-active_record/lib/database_cleaner-active_record.rb +0 -1
- data/adapters/database_cleaner-active_record/lib/database_cleaner/active_record.rb +0 -6
- data/adapters/database_cleaner-active_record/lib/database_cleaner/active_record/base.rb +0 -101
- data/adapters/database_cleaner-active_record/lib/database_cleaner/active_record/deletion.rb +0 -108
- data/adapters/database_cleaner-active_record/lib/database_cleaner/active_record/transaction.rb +0 -59
- data/adapters/database_cleaner-active_record/lib/database_cleaner/active_record/truncation.rb +0 -288
- data/adapters/database_cleaner-active_record/lib/database_cleaner/active_record/version.rb +0 -5
- data/adapters/database_cleaner-couch_potato/lib/database_cleaner-couch_potato.rb +0 -1
- data/adapters/database_cleaner-couch_potato/lib/database_cleaner/couch_potato.rb +0 -13
- data/adapters/database_cleaner-couch_potato/lib/database_cleaner/couch_potato/base.rb +0 -7
- data/adapters/database_cleaner-couch_potato/lib/database_cleaner/couch_potato/truncation.rb +0 -28
- data/adapters/database_cleaner-couch_potato/lib/database_cleaner/couch_potato/version.rb +0 -5
- data/adapters/database_cleaner-data_mapper/lib/database_cleaner-data_mapper.rb +0 -1
- data/adapters/database_cleaner-data_mapper/lib/database_cleaner/data_mapper.rb +0 -6
- data/adapters/database_cleaner-data_mapper/lib/database_cleaner/data_mapper/base.rb +0 -25
- data/adapters/database_cleaner-data_mapper/lib/database_cleaner/data_mapper/transaction.rb +0 -28
- data/adapters/database_cleaner-data_mapper/lib/database_cleaner/data_mapper/truncation.rb +0 -173
- data/adapters/database_cleaner-data_mapper/lib/database_cleaner/data_mapper/version.rb +0 -5
- data/adapters/database_cleaner-mongo/lib/database_cleaner-mongo.rb +0 -1
- data/adapters/database_cleaner-mongo/lib/database_cleaner/mongo.rb +0 -11
- data/adapters/database_cleaner-mongo/lib/database_cleaner/mongo/base.rb +0 -16
- data/adapters/database_cleaner-mongo/lib/database_cleaner/mongo/deletion.rb +0 -8
- data/adapters/database_cleaner-mongo/lib/database_cleaner/mongo/truncation.rb +0 -74
- data/adapters/database_cleaner-mongo/lib/database_cleaner/mongo/truncation_mixin.rb +0 -26
- data/adapters/database_cleaner-mongo/lib/database_cleaner/mongo/version.rb +0 -5
- data/adapters/database_cleaner-mongo_mapper/lib/database_cleaner-mongo_mapper.rb +0 -1
- data/adapters/database_cleaner-mongo_mapper/lib/database_cleaner/mongo_mapper.rb +0 -5
- data/adapters/database_cleaner-mongo_mapper/lib/database_cleaner/mongo_mapper/base.rb +0 -24
- data/adapters/database_cleaner-mongo_mapper/lib/database_cleaner/mongo_mapper/truncation.rb +0 -35
- data/adapters/database_cleaner-mongo_mapper/lib/database_cleaner/mongo_mapper/version.rb +0 -5
- data/adapters/database_cleaner-mongoid/lib/database_cleaner-mongoid.rb +0 -1
- data/adapters/database_cleaner-mongoid/lib/database_cleaner/mongoid.rb +0 -11
- data/adapters/database_cleaner-mongoid/lib/database_cleaner/mongoid/base.rb +0 -20
- data/adapters/database_cleaner-mongoid/lib/database_cleaner/mongoid/deletion.rb +0 -10
- data/adapters/database_cleaner-mongoid/lib/database_cleaner/mongoid/mongo1_truncation_mixin.rb +0 -26
- data/adapters/database_cleaner-mongoid/lib/database_cleaner/mongoid/mongo2_truncation_mixin.rb +0 -38
- data/adapters/database_cleaner-mongoid/lib/database_cleaner/mongoid/mongoid_truncation_mixin.rb +0 -65
- data/adapters/database_cleaner-mongoid/lib/database_cleaner/mongoid/truncation.rb +0 -49
- data/adapters/database_cleaner-mongoid/lib/database_cleaner/mongoid/version.rb +0 -5
- data/adapters/database_cleaner-moped/lib/database_cleaner-moped.rb +0 -1
- data/adapters/database_cleaner-moped/lib/database_cleaner/moped.rb +0 -12
- data/adapters/database_cleaner-moped/lib/database_cleaner/moped/base.rb +0 -39
- data/adapters/database_cleaner-moped/lib/database_cleaner/moped/truncation.rb +0 -9
- data/adapters/database_cleaner-moped/lib/database_cleaner/moped/truncation_base.rb +0 -44
- data/adapters/database_cleaner-moped/lib/database_cleaner/moped/version.rb +0 -5
- data/adapters/database_cleaner-neo4j/lib/database_cleaner-neo4j.rb +0 -1
- data/adapters/database_cleaner-neo4j/lib/database_cleaner/neo4j.rb +0 -7
- data/adapters/database_cleaner-neo4j/lib/database_cleaner/neo4j/base.rb +0 -66
- data/adapters/database_cleaner-neo4j/lib/database_cleaner/neo4j/deletion.rb +0 -16
- data/adapters/database_cleaner-neo4j/lib/database_cleaner/neo4j/transaction.rb +0 -35
- data/adapters/database_cleaner-neo4j/lib/database_cleaner/neo4j/truncation.rb +0 -9
- data/adapters/database_cleaner-neo4j/lib/database_cleaner/neo4j/version.rb +0 -5
- data/adapters/database_cleaner-ohm/lib/database_cleaner-ohm.rb +0 -1
- data/adapters/database_cleaner-ohm/lib/database_cleaner/ohm.rb +0 -12
- data/adapters/database_cleaner-ohm/lib/database_cleaner/ohm/truncation.rb +0 -24
- data/adapters/database_cleaner-ohm/lib/database_cleaner/ohm/version.rb +0 -5
- data/adapters/database_cleaner-redis/lib/database_cleaner-redis.rb +0 -1
- data/adapters/database_cleaner-redis/lib/database_cleaner/redis.rb +0 -5
- data/adapters/database_cleaner-redis/lib/database_cleaner/redis/base.rb +0 -45
- data/adapters/database_cleaner-redis/lib/database_cleaner/redis/deletion.rb +0 -9
- data/adapters/database_cleaner-redis/lib/database_cleaner/redis/truncation.rb +0 -26
- data/adapters/database_cleaner-redis/lib/database_cleaner/redis/version.rb +0 -5
- data/adapters/database_cleaner-sequel/lib/database_cleaner-sequel.rb +0 -1
- data/adapters/database_cleaner-sequel/lib/database_cleaner/sequel.rb +0 -6
- data/adapters/database_cleaner-sequel/lib/database_cleaner/sequel/base.rb +0 -26
- data/adapters/database_cleaner-sequel/lib/database_cleaner/sequel/deletion.rb +0 -47
- data/adapters/database_cleaner-sequel/lib/database_cleaner/sequel/transaction.rb +0 -40
- data/adapters/database_cleaner-sequel/lib/database_cleaner/sequel/truncation.rb +0 -79
- data/adapters/database_cleaner-sequel/lib/database_cleaner/sequel/version.rb +0 -5
- data/cucumber.yml +0 -1
- data/lib/database_cleaner/base.rb +0 -169
- data/lib/database_cleaner/configuration.rb +0 -136
- data/lib/database_cleaner/cucumber.rb +0 -3
- data/lib/database_cleaner/deprecation.rb +0 -26
- 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/null_strategy.rb +0 -20
- data/lib/database_cleaner/orm_autodetector.rb +0 -41
- data/lib/database_cleaner/safeguard.rb +0 -107
- data/lib/database_cleaner/spec.rb +0 -2
- data/lib/database_cleaner/spec/database_helper.rb +0 -82
- data/lib/database_cleaner/spec/shared_examples.rb +0 -15
- data/lib/database_cleaner/version.rb +0 -3
data/LICENSE
DELETED
@@ -1,20 +0,0 @@
|
|
1
|
-
Copyright (c) 2009 Ben Mabey
|
2
|
-
|
3
|
-
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
-
a copy of this software and associated documentation files (the
|
5
|
-
"Software"), to deal in the Software without restriction, including
|
6
|
-
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
-
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
-
permit persons to whom the Software is furnished to do so, subject to
|
9
|
-
the following conditions:
|
10
|
-
|
11
|
-
The above copyright notice and this permission notice shall be
|
12
|
-
included in all copies or substantial portions of the Software.
|
13
|
-
|
14
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
-
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
-
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
-
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
-
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
-
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
-
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.markdown
DELETED
@@ -1,389 +0,0 @@
|
|
1
|
-
<!--
|
2
|
-
**If you're viewing this at https://github.com/DatabaseCleaner/database_cleaner,
|
3
|
-
you're reading the documentation for the `master` branch.
|
4
|
-
[View documentation for the latest release
|
5
|
-
(1.7.0).](https://github.com/DatabaseCleaner/database_cleaner/blob/v1.7.0/README.markdown)**
|
6
|
-
-->
|
7
|
-
# Database Cleaner
|
8
|
-
|
9
|
-
[![Build Status](https://travis-ci.org/DatabaseCleaner/database_cleaner.svg?branch=master)](https://travis-ci.org/DatabaseCleaner/database_cleaner)
|
10
|
-
[![Code Climate](https://codeclimate.com/github/DatabaseCleaner/database_cleaner/badges/gpa.svg)](https://codeclimate.com/github/DatabaseCleaner/database_cleaner)
|
11
|
-
![Gem Version](https://badge.fury.io/rb/database_cleaner.svg)
|
12
|
-
[![SemVer](https://api.dependabot.com/badges/compatibility_score?dependency-name=database_cleaner&package-manager=bundler&version-scheme=semver)](https://dependabot.com/compatibility-score.html?dependency-name=database_cleaner&package-manager=bundler&version-scheme=semver)
|
13
|
-
|
14
|
-
Database Cleaner is a set of gems containing strategies for cleaning your database in Ruby.
|
15
|
-
|
16
|
-
The original use case was to ensure a clean state during tests.
|
17
|
-
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.
|
18
|
-
|
19
|
-
## Gem Setup
|
20
|
-
|
21
|
-
Instead of using the `database_cleaner` gem directly, each ORM has its own gem. Most projects will only need the `database_cleaner-active_record` gem:
|
22
|
-
|
23
|
-
```ruby
|
24
|
-
# Gemfile
|
25
|
-
group :test do
|
26
|
-
gem 'database_cleaner-active_record'
|
27
|
-
end
|
28
|
-
```
|
29
|
-
|
30
|
-
If you are using multiple ORMs, just load multiple gems:
|
31
|
-
|
32
|
-
|
33
|
-
```ruby
|
34
|
-
# Gemfile
|
35
|
-
group :test do
|
36
|
-
gem 'database_cleaner-active_record'
|
37
|
-
gem 'database_cleaner-redis'
|
38
|
-
end
|
39
|
-
```
|
40
|
-
|
41
|
-
## List of adapters
|
42
|
-
|
43
|
-
Here is an overview of the databases and ORMs supported by each adapter:
|
44
|
-
|
45
|
-
MySQL, PostgreSQL, SQLite, etc
|
46
|
-
* [database_cleaner-active_record](adapters/database_cleaner-active_record)
|
47
|
-
* [database_cleaner-sequel](adapters/database_cleaner-sequel)
|
48
|
-
* [database_cleaner-data_mapper](adapters/database_cleaner-data_mapper)
|
49
|
-
|
50
|
-
CouchDB
|
51
|
-
* [database_cleaner-couch_potato](adapters/database_cleaner-couch_potato)
|
52
|
-
|
53
|
-
MongoDB
|
54
|
-
* [database_cleaner-mongoid](adapters/database_cleaner-mongoid)
|
55
|
-
* [database_cleaner-mongo_mapper](adapters/database_cleaner-mongo_mapper)
|
56
|
-
* [database_cleaner-moped](adapters/database_cleaner-moped)
|
57
|
-
* [database_cleaner-mongo](adapters/database_cleaner-mongo)
|
58
|
-
|
59
|
-
Redis
|
60
|
-
* [database_cleaner-redis](adapters/database_cleaner-redis)
|
61
|
-
* [database_cleaner-ohm](adapters/database_cleaner-ohm)
|
62
|
-
|
63
|
-
Neo4j
|
64
|
-
* [database_cleaner-neo4j](adapters/database_cleaner-neo4j)
|
65
|
-
|
66
|
-
More details on available configuration options can be found in the README for the specific adapter gem that you're using.
|
67
|
-
|
68
|
-
For support or to discuss development please use the [Google Group](https://groups.google.com/group/database_cleaner).
|
69
|
-
|
70
|
-
## How to use
|
71
|
-
|
72
|
-
```ruby
|
73
|
-
require 'database_cleaner/active_record'
|
74
|
-
|
75
|
-
DatabaseCleaner.strategy = :truncation
|
76
|
-
|
77
|
-
# then, whenever you need to clean the DB
|
78
|
-
DatabaseCleaner.clean
|
79
|
-
```
|
80
|
-
|
81
|
-
With the `:truncation` strategy you can also pass in options, for example:
|
82
|
-
|
83
|
-
```ruby
|
84
|
-
DatabaseCleaner.strategy = :truncation, {:only => %w[widgets dogs some_other_table]}
|
85
|
-
```
|
86
|
-
|
87
|
-
```ruby
|
88
|
-
DatabaseCleaner.strategy = :truncation, {:except => %w[widgets]}
|
89
|
-
```
|
90
|
-
|
91
|
-
(I should point out the truncation strategy will never truncate your schema_migrations table.)
|
92
|
-
|
93
|
-
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:
|
94
|
-
|
95
|
-
```ruby
|
96
|
-
require 'database_cleaner/active_record'
|
97
|
-
|
98
|
-
DatabaseCleaner.strategy = :transaction
|
99
|
-
|
100
|
-
DatabaseCleaner.start # usually this is called in setup of a test
|
101
|
-
|
102
|
-
dirty_the_db
|
103
|
-
|
104
|
-
DatabaseCleaner.clean # cleanup of the test
|
105
|
-
|
106
|
-
# OR
|
107
|
-
|
108
|
-
DatabaseCleaner.cleaning do
|
109
|
-
dirty_the_db
|
110
|
-
end
|
111
|
-
```
|
112
|
-
|
113
|
-
At times you may want to do a single clean with one strategy.
|
114
|
-
|
115
|
-
For example, you may want to start the process by truncating all the tables, but then use the faster transaction strategy the remaining time. To accomplish this you can say:
|
116
|
-
|
117
|
-
```ruby
|
118
|
-
require 'database_cleaner/active_record'
|
119
|
-
|
120
|
-
DatabaseCleaner.clean_with :truncation
|
121
|
-
|
122
|
-
DatabaseCleaner.strategy = :transaction
|
123
|
-
|
124
|
-
# then make the DatabaseCleaner.start and DatabaseCleaner.clean calls appropriately
|
125
|
-
```
|
126
|
-
|
127
|
-
## What strategy is fastest?
|
128
|
-
|
129
|
-
For the SQL libraries the fastest option will be to use `:transaction` as transactions are simply rolled back. If you can use this strategy you should. However, if you wind up needing to use multiple database connections in your tests (i.e. your tests run in a different process than your application) then using this strategy becomes a bit more difficult. You can get around the problem a number of ways.
|
130
|
-
|
131
|
-
One common approach is to force all processes to use the same database connection ([common ActiveRecord hack](http://blog.plataformatec.com.br/2011/12/three-tips-to-improve-the-performance-of-your-test-suite/)) however this approach has been reported to result in non-deterministic failures.
|
132
|
-
|
133
|
-
Another approach is to have the transactions rolled back in the application's process and relax the isolation level of the database (so the tests can read the uncommitted transactions).
|
134
|
-
|
135
|
-
An easier, but slower, solution is to use the `:truncation` or `:deletion` strategy.
|
136
|
-
|
137
|
-
So what is fastest out of `:deletion` and `:truncation`? Well, it depends on your table structure and what percentage of tables you populate in an average test. The reasoning is out of the scope of this README but here is a [good SO answer on this topic for Postgres](https://stackoverflow.com/questions/11419536/postgresql-truncation-speed/11423886#11423886).
|
138
|
-
|
139
|
-
Some people report much faster speeds with `:deletion` while others say `:truncation` is faster for them. The best approach therefore is it try all options on your test suite and see what is faster.
|
140
|
-
|
141
|
-
If you are using ActiveRecord then take a look at the [additional options](#additional-activerecord-options-for-truncation) available for `:truncation`.
|
142
|
-
|
143
|
-
Database Cleaner also includes a `null` strategy (that does no cleaning at all) which can be used with any ORM library.
|
144
|
-
You can also explicitly use it by setting your strategy to `nil`.
|
145
|
-
|
146
|
-
## Test Framework Examples
|
147
|
-
|
148
|
-
### RSpec Example
|
149
|
-
|
150
|
-
```ruby
|
151
|
-
RSpec.configure do |config|
|
152
|
-
|
153
|
-
config.before(:suite) do
|
154
|
-
DatabaseCleaner.strategy = :transaction
|
155
|
-
DatabaseCleaner.clean_with(:truncation)
|
156
|
-
end
|
157
|
-
|
158
|
-
config.around(:each) do |example|
|
159
|
-
DatabaseCleaner.cleaning do
|
160
|
-
example.run
|
161
|
-
end
|
162
|
-
end
|
163
|
-
|
164
|
-
end
|
165
|
-
```
|
166
|
-
|
167
|
-
### RSpec with Capybara Example
|
168
|
-
|
169
|
-
You'll typically discover a feature spec is incorrectly using transaction
|
170
|
-
instead of truncation strategy when the data created in the spec is not
|
171
|
-
visible in the app-under-test.
|
172
|
-
|
173
|
-
A frequently occurring example of this is when, after creating a user in a
|
174
|
-
spec, the spec mysteriously fails to login with the user. This happens because
|
175
|
-
the user is created inside of an uncommitted transaction on one database
|
176
|
-
connection, while the login attempt is made using a separate database
|
177
|
-
connection. This separate database connection cannot access the
|
178
|
-
uncommitted user data created over the first database connection due to
|
179
|
-
transaction isolation.
|
180
|
-
|
181
|
-
For feature specs using a Capybara driver for an external
|
182
|
-
JavaScript-capable browser (in practice this is all drivers except
|
183
|
-
`:rack_test`), the Rack app under test and the specs do not share a
|
184
|
-
database connection.
|
185
|
-
|
186
|
-
When a spec and app-under-test do not share a database connection,
|
187
|
-
you'll likely need to use the truncation strategy instead of the
|
188
|
-
transaction strategy.
|
189
|
-
|
190
|
-
See the suggested config below to temporarily enable truncation strategy
|
191
|
-
for affected feature specs only. This config continues to use transaction
|
192
|
-
strategy for all other specs.
|
193
|
-
|
194
|
-
It's also recommended to use `append_after` to ensure `DatabaseCleaner.clean`
|
195
|
-
runs *after* the after-test cleanup `capybara/rspec` installs.
|
196
|
-
|
197
|
-
```ruby
|
198
|
-
require 'capybara/rspec'
|
199
|
-
|
200
|
-
#...
|
201
|
-
|
202
|
-
RSpec.configure do |config|
|
203
|
-
|
204
|
-
config.use_transactional_fixtures = false
|
205
|
-
|
206
|
-
config.before(:suite) do
|
207
|
-
if config.use_transactional_fixtures?
|
208
|
-
raise(<<-MSG)
|
209
|
-
Delete line `config.use_transactional_fixtures = true` from rails_helper.rb
|
210
|
-
(or set it to false) to prevent uncommitted transactions being used in
|
211
|
-
JavaScript-dependent specs.
|
212
|
-
|
213
|
-
During testing, the app-under-test that the browser driver connects to
|
214
|
-
uses a different database connection to the database connection used by
|
215
|
-
the spec. The app's database connection would not be able to access
|
216
|
-
uncommitted transaction data setup over the spec's database connection.
|
217
|
-
MSG
|
218
|
-
end
|
219
|
-
DatabaseCleaner.clean_with(:truncation)
|
220
|
-
end
|
221
|
-
|
222
|
-
config.before(:each) do
|
223
|
-
DatabaseCleaner.strategy = :transaction
|
224
|
-
end
|
225
|
-
|
226
|
-
config.before(:each, type: :feature) do
|
227
|
-
# :rack_test driver's Rack app under test shares database connection
|
228
|
-
# with the specs, so continue to use transaction strategy for speed.
|
229
|
-
driver_shares_db_connection_with_specs = Capybara.current_driver == :rack_test
|
230
|
-
|
231
|
-
unless driver_shares_db_connection_with_specs
|
232
|
-
# Driver is probably for an external browser with an app
|
233
|
-
# under test that does *not* share a database connection with the
|
234
|
-
# specs, so use truncation strategy.
|
235
|
-
DatabaseCleaner.strategy = :truncation
|
236
|
-
end
|
237
|
-
end
|
238
|
-
|
239
|
-
config.before(:each) do
|
240
|
-
DatabaseCleaner.start
|
241
|
-
end
|
242
|
-
|
243
|
-
config.append_after(:each) do
|
244
|
-
DatabaseCleaner.clean
|
245
|
-
end
|
246
|
-
|
247
|
-
end
|
248
|
-
```
|
249
|
-
|
250
|
-
|
251
|
-
### Minitest Example
|
252
|
-
|
253
|
-
```ruby
|
254
|
-
DatabaseCleaner.strategy = :transaction
|
255
|
-
|
256
|
-
class Minitest::Spec
|
257
|
-
before :each do
|
258
|
-
DatabaseCleaner.start
|
259
|
-
end
|
260
|
-
|
261
|
-
after :each do
|
262
|
-
DatabaseCleaner.clean
|
263
|
-
end
|
264
|
-
end
|
265
|
-
|
266
|
-
# with the minitest-around gem, this may be used instead:
|
267
|
-
class Minitest::Spec
|
268
|
-
around do |tests|
|
269
|
-
DatabaseCleaner.cleaning(&tests)
|
270
|
-
end
|
271
|
-
end
|
272
|
-
```
|
273
|
-
|
274
|
-
### Cucumber Example
|
275
|
-
|
276
|
-
If you're using Cucumber with Rails, just use the generator that ships with cucumber-rails, and that will create all the code you need to integrate DatabaseCleaner into your Rails project.
|
277
|
-
|
278
|
-
Otherwise, to add DatabaseCleaner to your project by hand, create a file `features/support/database_cleaner.rb` that looks like this:
|
279
|
-
|
280
|
-
```ruby
|
281
|
-
require 'database_cleaner/active_record'
|
282
|
-
|
283
|
-
DatabaseCleaner.strategy = :truncation
|
284
|
-
|
285
|
-
Around do |scenario, block|
|
286
|
-
DatabaseCleaner.cleaning(&block)
|
287
|
-
end
|
288
|
-
```
|
289
|
-
|
290
|
-
This should cover the basics of tear down between scenarios and keeping your database clean.
|
291
|
-
|
292
|
-
For more examples see the section ["Why?"](#why).
|
293
|
-
|
294
|
-
## How to use with multiple ORMs
|
295
|
-
|
296
|
-
Sometimes you need to use multiple ORMs in your application.
|
297
|
-
|
298
|
-
You can use DatabaseCleaner to clean multiple ORMs, and multiple connections for those ORMs.
|
299
|
-
|
300
|
-
```ruby
|
301
|
-
require 'database_cleaner/active_record'
|
302
|
-
require 'database_cleaner/mongo_mapper'
|
303
|
-
|
304
|
-
# How to specify particular orms
|
305
|
-
DatabaseCleaner[:active_record].strategy = :transaction
|
306
|
-
DatabaseCleaner[:mongo_mapper].strategy = :truncation
|
307
|
-
|
308
|
-
# How to specify particular connections
|
309
|
-
DatabaseCleaner[:active_record, { :db => :two }]
|
310
|
-
|
311
|
-
# You may also pass in the model directly:
|
312
|
-
DatabaseCleaner[:active_record, { :db => ModelWithDifferentConnection }]
|
313
|
-
```
|
314
|
-
|
315
|
-
Usage beyond that remains the same with `DatabaseCleaner.start` calling any setup on the different configured connections, and `DatabaseCleaner.clean` executing afterwards.
|
316
|
-
|
317
|
-
## Why?
|
318
|
-
|
319
|
-
One of my motivations for writing this library was to have an easy way to turn on what Rails calls "transactional_fixtures" in my non-rails ActiveRecord projects.
|
320
|
-
|
321
|
-
After copying and pasting code to do this several times I decided to package it up as a gem and save everyone a bit of time.
|
322
|
-
|
323
|
-
## Common Errors
|
324
|
-
|
325
|
-
#### DatabaseCleaner is trying to use the wrong ORM
|
326
|
-
|
327
|
-
DatabaseCleaner has a deprecated autodetect mechanism where if you do not explicitly define your ORM it will use the first ORM it can detect that is loaded.
|
328
|
-
|
329
|
-
Since ActiveRecord is the most common ORM used that is the first one checked for.
|
330
|
-
|
331
|
-
Sometimes other libraries (e.g. ActiveAdmin) will load other ORMs (e.g. ActiveRecord) even though you are using a different ORM. This will result in DatabaseCleaner trying to use the wrong ORM (e.g. ActiveRecord) unless you explicitly require the correct adapter gem:
|
332
|
-
|
333
|
-
```ruby
|
334
|
-
# Gemfile
|
335
|
-
gem "database_cleaner-mongoid"
|
336
|
-
```
|
337
|
-
|
338
|
-
### STDERR is being flooded when using Postgres
|
339
|
-
|
340
|
-
If you are using Postgres and have foreign key constraints, the truncation strategy will cause a lot of extra noise to appear on STDERR (in the form of "NOTICE truncate cascades" messages).
|
341
|
-
|
342
|
-
To silence these warnings set the following log level in your `postgresql.conf` file:
|
343
|
-
|
344
|
-
```ruby
|
345
|
-
client_min_messages = warning
|
346
|
-
```
|
347
|
-
|
348
|
-
For ActiveRecord, you add the following parameter in your database.yml file:
|
349
|
-
|
350
|
-
<pre>
|
351
|
-
test:
|
352
|
-
adapter: postgresql
|
353
|
-
# ...
|
354
|
-
min_messages: WARNING
|
355
|
-
</pre>
|
356
|
-
|
357
|
-
## Safeguards
|
358
|
-
|
359
|
-
DatabaseCleaner comes with safeguards against:
|
360
|
-
|
361
|
-
* Running in production (checking for `ENV`, `RACK_ENV`, and `RAILS_ENV`)
|
362
|
-
* Running against a remote database (checking for a `DATABASE_URL` that does not include `localhost`, `.local` or `127.0.0.1`)
|
363
|
-
|
364
|
-
Both safeguards can be disabled separately as follows.
|
365
|
-
|
366
|
-
Using environment variables:
|
367
|
-
|
368
|
-
```
|
369
|
-
export DATABASE_CLEANER_ALLOW_PRODUCTION=true
|
370
|
-
export DATABASE_CLEANER_ALLOW_REMOTE_DATABASE_URL=true
|
371
|
-
```
|
372
|
-
|
373
|
-
In Ruby:
|
374
|
-
|
375
|
-
```ruby
|
376
|
-
DatabaseCleaner.allow_production = true
|
377
|
-
DatabaseCleaner.allow_remote_database_url = true
|
378
|
-
```
|
379
|
-
|
380
|
-
In Ruby, a URL whitelist can be specified. When specified, DatabaseCleaner will only allow `DATABASE_URL` to be equal
|
381
|
-
to one of the values specified in the url whitelist like so:
|
382
|
-
|
383
|
-
```ruby
|
384
|
-
DatabaseCleaner.url_whitelist = ['postgres://postgres@localhost', 'postgres://foo@bar']
|
385
|
-
```
|
386
|
-
|
387
|
-
## COPYRIGHT
|
388
|
-
|
389
|
-
See [LICENSE](LICENSE) for details.
|
data/Rakefile
DELETED
@@ -1,44 +0,0 @@
|
|
1
|
-
require "rubygems"
|
2
|
-
require "bundler"
|
3
|
-
require "bundler/gem_tasks"
|
4
|
-
Bundler.setup
|
5
|
-
|
6
|
-
require 'rake'
|
7
|
-
require 'rspec/core'
|
8
|
-
require 'rspec/core/rake_task'
|
9
|
-
RSpec::Core::RakeTask.new(:spec) do |spec|
|
10
|
-
spec.pattern = FileList['spec/**/*_spec.rb']
|
11
|
-
end
|
12
|
-
|
13
|
-
RSpec::Core::RakeTask.new(:rcov) do |spec|
|
14
|
-
spec.pattern = 'spec/**/*_spec.rb'
|
15
|
-
spec.rcov = true
|
16
|
-
end
|
17
|
-
|
18
|
-
require 'cucumber/rake/task'
|
19
|
-
Cucumber::Rake::Task.new(:features)
|
20
|
-
|
21
|
-
desc "Run adapter test suites"
|
22
|
-
task :adapters do
|
23
|
-
Dir["adapters/*"].each do |adapter_dir|
|
24
|
-
Dir.chdir adapter_dir do
|
25
|
-
sh "bundle exec rake"
|
26
|
-
end
|
27
|
-
end
|
28
|
-
end
|
29
|
-
|
30
|
-
task :default => [:spec, :features, :adapters]
|
31
|
-
|
32
|
-
desc "Cleans the project of any tmp file that should not be included in the gemspec."
|
33
|
-
task :clean do
|
34
|
-
["examples/config/database.yml", "examples/db/activerecord_one.db", "examples/db/activerecord_two.db", "examples/db/datamapper_default.db",
|
35
|
-
"examples/db/datamapper_one.db", "examples/db/datamapper_two.db"].each do |f|
|
36
|
-
FileUtils.rm_f(f)
|
37
|
-
end
|
38
|
-
%w[*.sqlite3 *.log #* *.swp *.swo].each do |pattern|
|
39
|
-
`find . -name "#{pattern}" -delete`
|
40
|
-
end
|
41
|
-
end
|
42
|
-
|
43
|
-
desc "Cleans the dir and builds the gem"
|
44
|
-
task :prep => [:clean, :gemspec, :build]
|