database_cleaner 0.9.1 → 1.0.0.RC1
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile.lock +127 -136
- data/History.txt +19 -1
- data/README.markdown +335 -0
- data/VERSION.yml +5 -5
- data/examples/Gemfile +18 -9
- data/examples/Gemfile.lock +127 -136
- data/examples/features/support/env.rb +3 -12
- data/lib/database_cleaner/active_record/base.rb +9 -9
- data/lib/database_cleaner/active_record/deletion.rb +5 -48
- data/lib/database_cleaner/active_record/transaction.rb +31 -9
- data/lib/database_cleaner/active_record/truncation.rb +23 -6
- data/lib/database_cleaner/data_mapper/truncation.rb +6 -2
- data/lib/database_cleaner/sequel/truncation.rb +1 -1
- data/spec/database_cleaner/active_record/base_spec.rb +32 -11
- data/spec/database_cleaner/active_record/transaction_spec.rb +101 -42
- data/spec/database_cleaner/active_record/truncation_spec.rb +1 -1
- metadata +28 -331
- data/README.textile +0 -247
data/Gemfile.lock
CHANGED
@@ -1,231 +1,222 @@
|
|
1
1
|
GEM
|
2
2
|
remote: http://rubygems.org/
|
3
3
|
specs:
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
activesupport (= 3.2.6)
|
4
|
+
actionpack (3.2.11)
|
5
|
+
activemodel (= 3.2.11)
|
6
|
+
activesupport (= 3.2.11)
|
8
7
|
builder (~> 3.0.0)
|
9
8
|
erubis (~> 2.7.0)
|
10
|
-
journey (~> 1.0.
|
9
|
+
journey (~> 1.0.4)
|
11
10
|
rack (~> 1.4.0)
|
12
11
|
rack-cache (~> 1.2)
|
13
12
|
rack-test (~> 0.6.1)
|
14
|
-
sprockets (~> 2.1
|
15
|
-
activemodel (3.2.
|
16
|
-
activesupport (= 3.2.
|
13
|
+
sprockets (~> 2.2.1)
|
14
|
+
activemodel (3.2.11)
|
15
|
+
activesupport (= 3.2.11)
|
17
16
|
builder (~> 3.0.0)
|
18
|
-
activerecord (3.2.
|
19
|
-
activemodel (= 3.2.
|
20
|
-
activesupport (= 3.2.
|
17
|
+
activerecord (3.2.11)
|
18
|
+
activemodel (= 3.2.11)
|
19
|
+
activesupport (= 3.2.11)
|
21
20
|
arel (~> 3.0.2)
|
22
21
|
tzinfo (~> 0.3.29)
|
23
|
-
activesupport (3.2.
|
22
|
+
activesupport (3.2.11)
|
24
23
|
i18n (~> 0.6)
|
25
24
|
multi_json (~> 1.0)
|
26
25
|
addressable (2.2.8)
|
27
26
|
arel (3.0.2)
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
27
|
+
bcrypt-ruby (3.0.1)
|
28
|
+
bson (1.8.2)
|
29
|
+
bson_ext (1.8.2)
|
30
|
+
bson (~> 1.8.2)
|
31
|
+
builder (3.0.4)
|
32
|
+
coderay (1.0.8)
|
33
|
+
couch_potato (0.7.1)
|
34
34
|
activemodel
|
35
35
|
couchrest (>= 1.0.1)
|
36
|
-
json
|
37
|
-
couchrest (1.
|
38
|
-
|
39
|
-
|
40
|
-
rest-client (
|
36
|
+
json (~> 1.6)
|
37
|
+
couchrest (1.1.3)
|
38
|
+
mime-types (~> 1.15)
|
39
|
+
multi_json (~> 1.0)
|
40
|
+
rest-client (~> 1.6.1)
|
41
41
|
cucumber (1.2.1)
|
42
42
|
builder (>= 2.1.2)
|
43
43
|
diff-lcs (>= 1.1.3)
|
44
44
|
gherkin (~> 2.11.0)
|
45
45
|
json (>= 1.4.6)
|
46
|
-
data_objects (0.10.
|
46
|
+
data_objects (0.10.11)
|
47
47
|
addressable (~> 2.1)
|
48
|
-
datamapper (1.
|
49
|
-
dm-aggregates (
|
50
|
-
dm-constraints (
|
51
|
-
dm-core (
|
52
|
-
dm-
|
53
|
-
dm-
|
54
|
-
dm-
|
55
|
-
dm-
|
56
|
-
dm-
|
57
|
-
dm-
|
58
|
-
dm-validations (= 1.0.0)
|
48
|
+
datamapper (1.2.0)
|
49
|
+
dm-aggregates (~> 1.2.0)
|
50
|
+
dm-constraints (~> 1.2.0)
|
51
|
+
dm-core (~> 1.2.0)
|
52
|
+
dm-migrations (~> 1.2.0)
|
53
|
+
dm-serializer (~> 1.2.0)
|
54
|
+
dm-timestamps (~> 1.2.0)
|
55
|
+
dm-transactions (~> 1.2.0)
|
56
|
+
dm-types (~> 1.2.0)
|
57
|
+
dm-validations (~> 1.2.0)
|
59
58
|
diff-lcs (1.1.3)
|
60
|
-
dm-aggregates (1.
|
61
|
-
dm-core (~> 1.
|
62
|
-
dm-constraints (1.
|
63
|
-
dm-core (~> 1.
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
dm-core (~> 1.
|
71
|
-
dm-
|
72
|
-
dm-core (~> 1.
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
dm-sqlite-adapter (1.
|
78
|
-
dm-do-adapter (~> 1.
|
79
|
-
do_sqlite3 (~> 0.10.
|
80
|
-
dm-timestamps (1.
|
81
|
-
dm-core (~> 1.
|
82
|
-
dm-transactions (1.
|
83
|
-
dm-core (~> 1.
|
84
|
-
dm-types (1.
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
59
|
+
dm-aggregates (1.2.0)
|
60
|
+
dm-core (~> 1.2.0)
|
61
|
+
dm-constraints (1.2.0)
|
62
|
+
dm-core (~> 1.2.0)
|
63
|
+
dm-core (1.2.0)
|
64
|
+
addressable (~> 2.2.6)
|
65
|
+
dm-do-adapter (1.2.0)
|
66
|
+
data_objects (~> 0.10.6)
|
67
|
+
dm-core (~> 1.2.0)
|
68
|
+
dm-migrations (1.2.0)
|
69
|
+
dm-core (~> 1.2.0)
|
70
|
+
dm-serializer (1.2.2)
|
71
|
+
dm-core (~> 1.2.0)
|
72
|
+
fastercsv (~> 1.5)
|
73
|
+
json (~> 1.6)
|
74
|
+
json_pure (~> 1.6)
|
75
|
+
multi_json (~> 1.0)
|
76
|
+
dm-sqlite-adapter (1.2.0)
|
77
|
+
dm-do-adapter (~> 1.2.0)
|
78
|
+
do_sqlite3 (~> 0.10.6)
|
79
|
+
dm-timestamps (1.2.0)
|
80
|
+
dm-core (~> 1.2.0)
|
81
|
+
dm-transactions (1.2.0)
|
82
|
+
dm-core (~> 1.2.0)
|
83
|
+
dm-types (1.2.2)
|
84
|
+
bcrypt-ruby (~> 3.0)
|
85
|
+
dm-core (~> 1.2.0)
|
86
|
+
fastercsv (~> 1.5)
|
87
|
+
json (~> 1.6)
|
88
|
+
multi_json (~> 1.0)
|
89
|
+
stringex (~> 1.4)
|
90
|
+
uuidtools (~> 2.1)
|
91
|
+
dm-validations (1.2.0)
|
92
|
+
dm-core (~> 1.2.0)
|
93
|
+
do_sqlite3 (0.10.11)
|
94
|
+
data_objects (= 0.10.11)
|
94
95
|
erubis (2.7.0)
|
95
|
-
extlib (0.9.15)
|
96
96
|
fastercsv (1.5.5)
|
97
|
-
|
98
|
-
gherkin (2.11.1)
|
97
|
+
gherkin (2.11.5)
|
99
98
|
json (>= 1.4.6)
|
100
99
|
git (1.2.5)
|
101
|
-
guard (1.
|
102
|
-
listen (>= 0.
|
100
|
+
guard (1.6.1)
|
101
|
+
listen (>= 0.6.0)
|
102
|
+
lumberjack (>= 1.0.2)
|
103
|
+
pry (>= 0.9.10)
|
103
104
|
thor (>= 0.14.6)
|
104
|
-
guard-rspec (
|
105
|
+
guard-rspec (2.3.3)
|
105
106
|
guard (>= 1.1)
|
107
|
+
rspec (~> 2.11)
|
106
108
|
hike (1.2.1)
|
107
|
-
i18n (0.6.
|
109
|
+
i18n (0.6.1)
|
108
110
|
jeweler (1.8.4)
|
109
111
|
bundler (~> 1.0)
|
110
112
|
git (>= 1.2.5)
|
111
113
|
rake
|
112
114
|
rdoc
|
113
115
|
journey (1.0.4)
|
114
|
-
json (1.7.
|
115
|
-
json_pure (1.
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
rb-fchange (~> 0.0.5)
|
120
|
-
rb-fsevent (~> 0.9.1)
|
121
|
-
rb-inotify (~> 0.8.8)
|
116
|
+
json (1.7.6)
|
117
|
+
json_pure (1.7.6)
|
118
|
+
listen (0.7.1)
|
119
|
+
lumberjack (1.0.2)
|
120
|
+
method_source (0.8.1)
|
122
121
|
mime-types (1.19)
|
123
|
-
mongo (1.
|
124
|
-
bson (~> 1.
|
122
|
+
mongo (1.8.2)
|
123
|
+
bson (~> 1.8.2)
|
125
124
|
mongo_ext (0.19.3)
|
126
|
-
mongo_mapper (0.
|
125
|
+
mongo_mapper (0.12.0)
|
127
126
|
activemodel (~> 3.0)
|
128
127
|
activesupport (~> 3.0)
|
129
|
-
plucky (~> 0.
|
130
|
-
mongoid (3.0.
|
128
|
+
plucky (~> 0.5.2)
|
129
|
+
mongoid (3.0.16)
|
131
130
|
activemodel (~> 3.1)
|
132
|
-
moped (~> 1.1
|
133
|
-
origin (~> 1.0
|
131
|
+
moped (~> 1.1)
|
132
|
+
origin (~> 1.0)
|
134
133
|
tzinfo (~> 0.3.22)
|
135
|
-
moped (1.
|
136
|
-
multi_json (1.
|
134
|
+
moped (1.3.2)
|
135
|
+
multi_json (1.5.0)
|
137
136
|
mysql (2.8.1)
|
138
137
|
mysql2 (0.3.11)
|
139
|
-
origin (1.0.
|
140
|
-
pg (0.14.
|
141
|
-
plucky (0.
|
138
|
+
origin (1.0.11)
|
139
|
+
pg (0.14.1)
|
140
|
+
plucky (0.5.2)
|
142
141
|
mongo (~> 1.5)
|
143
|
-
|
142
|
+
pry (0.9.10)
|
143
|
+
coderay (~> 1.0.5)
|
144
|
+
method_source (~> 0.8)
|
145
|
+
slop (~> 3.3.1)
|
146
|
+
rack (1.4.3)
|
144
147
|
rack-cache (1.2)
|
145
148
|
rack (>= 0.4)
|
146
149
|
rack-ssl (1.3.2)
|
147
150
|
rack
|
148
|
-
rack-test (0.6.
|
151
|
+
rack-test (0.6.2)
|
149
152
|
rack (>= 1.0)
|
150
|
-
railties (3.2.
|
151
|
-
actionpack (= 3.2.
|
152
|
-
activesupport (= 3.2.
|
153
|
+
railties (3.2.11)
|
154
|
+
actionpack (= 3.2.11)
|
155
|
+
activesupport (= 3.2.11)
|
153
156
|
rack-ssl (~> 1.3.2)
|
154
157
|
rake (>= 0.8.7)
|
155
158
|
rdoc (~> 3.4)
|
156
159
|
thor (>= 0.14.6, < 2.0)
|
157
|
-
rake (0.
|
158
|
-
rb-fchange (0.0.5)
|
159
|
-
ffi
|
160
|
-
rb-fsevent (0.9.1)
|
161
|
-
rb-inotify (0.8.8)
|
162
|
-
ffi (>= 0.5.0)
|
163
|
-
rbx-require-relative (0.0.9)
|
164
|
-
rcov (1.0.0)
|
160
|
+
rake (10.0.3)
|
165
161
|
rdoc (3.12)
|
166
162
|
json (~> 1.4)
|
167
163
|
rest-client (1.6.7)
|
168
164
|
mime-types (>= 1.16)
|
169
|
-
|
170
|
-
|
171
|
-
rspec-
|
172
|
-
rspec-
|
173
|
-
|
174
|
-
rspec-
|
175
|
-
rspec-expectations (2.11.1)
|
165
|
+
rspec (2.12.0)
|
166
|
+
rspec-core (~> 2.12.0)
|
167
|
+
rspec-expectations (~> 2.12.0)
|
168
|
+
rspec-mocks (~> 2.12.0)
|
169
|
+
rspec-core (2.12.2)
|
170
|
+
rspec-expectations (2.12.1)
|
176
171
|
diff-lcs (~> 1.1.3)
|
177
|
-
rspec-mocks (2.
|
178
|
-
rspec-rails (2.
|
172
|
+
rspec-mocks (2.12.1)
|
173
|
+
rspec-rails (2.12.1)
|
179
174
|
actionpack (>= 3.0)
|
180
175
|
activesupport (>= 3.0)
|
181
176
|
railties (>= 3.0)
|
182
|
-
rspec (~> 2.
|
183
|
-
|
184
|
-
|
185
|
-
ruby-debug-base (~> 0.10.4.0)
|
186
|
-
ruby-debug-base (0.10.4)
|
187
|
-
linecache (>= 0.3)
|
177
|
+
rspec-core (~> 2.12.0)
|
178
|
+
rspec-expectations (~> 2.12.0)
|
179
|
+
rspec-mocks (~> 2.12.0)
|
188
180
|
sequel (3.21.0)
|
189
|
-
|
181
|
+
slop (3.3.3)
|
182
|
+
sprockets (2.2.2)
|
190
183
|
hike (~> 1.2)
|
184
|
+
multi_json (~> 1.0)
|
191
185
|
rack (~> 1.0)
|
192
186
|
tilt (~> 1.1, != 1.3.0)
|
193
187
|
sqlite3 (1.3.6)
|
194
188
|
sqlite3-ruby (1.3.3)
|
195
189
|
sqlite3 (>= 1.3.3)
|
196
|
-
stringex (1.1
|
197
|
-
thor (0.
|
190
|
+
stringex (1.5.1)
|
191
|
+
thor (0.16.0)
|
198
192
|
tilt (1.3.3)
|
199
|
-
tzinfo (0.3.
|
193
|
+
tzinfo (0.3.35)
|
200
194
|
uuidtools (2.1.3)
|
201
195
|
|
202
196
|
PLATFORMS
|
203
197
|
ruby
|
204
198
|
|
205
199
|
DEPENDENCIES
|
206
|
-
ZenTest
|
207
200
|
activerecord
|
208
201
|
bson_ext
|
209
202
|
bundler
|
210
203
|
couch_potato
|
211
204
|
cucumber
|
212
|
-
datamapper
|
213
|
-
dm-migrations
|
214
|
-
dm-sqlite-adapter
|
205
|
+
datamapper
|
206
|
+
dm-migrations
|
207
|
+
dm-sqlite-adapter
|
215
208
|
guard-rspec
|
216
209
|
jeweler
|
217
210
|
json_pure
|
218
211
|
mongo_ext
|
219
212
|
mongo_mapper
|
220
213
|
mongoid
|
221
|
-
mysql
|
214
|
+
mysql (~> 2.8.1)
|
222
215
|
mysql2
|
223
216
|
pg
|
224
217
|
rake
|
225
|
-
rcov
|
226
|
-
rspactor
|
227
218
|
rspec-rails
|
228
|
-
ruby-debug
|
229
219
|
sequel (~> 3.21.0)
|
220
|
+
sqlite3
|
230
221
|
sqlite3-ruby
|
231
222
|
tzinfo
|
data/History.txt
CHANGED
@@ -1,4 +1,22 @@
|
|
1
|
-
== 0.
|
1
|
+
== 1.0.0 2012-03-xx
|
2
|
+
|
3
|
+
=== New Features/Changes
|
4
|
+
|
5
|
+
* Dropping support for Ruby 1.8.x; Only 1.9.x and beyond will be supported going forward.
|
6
|
+
* Now supporting (and testing against 2.0.x).
|
7
|
+
* Adds support for AR 4.0 by using `begin_transaction` (David Chelimsky and Steve Madsen)
|
8
|
+
* Adds Rails 4 support for SQLite3Adapter
|
9
|
+
|
10
|
+
* CI Improvements (Jan Vlnas, Murahashi Sanemat Kenichi, Samer Masry, Jordan Hollinger)
|
11
|
+
* README/Documentation improvements (Marcelo Cajueiro, Donald Ball, TJ Chambers, Nick Huanca
|
12
|
+
|
13
|
+
=== Bug Fixes
|
14
|
+
* Fixes transaction errors when using `after_commit` hooks in AR.
|
15
|
+
* Fixes truncation error with SQLite (Daniel White)
|
16
|
+
* Fixes `pre_count` logic in AR Postgres. (Jordan Hollinger)
|
17
|
+
* Sequel fix to normalize all table names to strings. (Lauri Peltola)
|
18
|
+
|
19
|
+
== 0.9.1 2012-10-11 (0.9.0 was released first but was yanked due to bad gemspec)
|
2
20
|
|
3
21
|
=== New Features
|
4
22
|
|
data/README.markdown
ADDED
@@ -0,0 +1,335 @@
|
|
1
|
+
# Database Cleaner
|
2
|
+
|
3
|
+
Database Cleaner is a set of strategies for cleaning your database in Ruby.
|
4
|
+
|
5
|
+
The original use case was to ensure a clean state during tests.
|
6
|
+
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
|
+
|
8
|
+
ActiveRecord, DataMapper, Sequel, MongoMapper, Mongoid, and CouchPotato are supported.
|
9
|
+
|
10
|
+
[![Build Status](https://secure.travis-ci.org/bmabey/database_cleaner.png)](http://travis-ci.org/bmabey/database_cleaner)
|
11
|
+
|
12
|
+
Here is an overview of the strategies supported for each library:
|
13
|
+
|
14
|
+
<table>
|
15
|
+
<tbody>
|
16
|
+
<tr>
|
17
|
+
<th>ORM</th>
|
18
|
+
<th>Truncation</th>
|
19
|
+
<th>Transaction</th>
|
20
|
+
<th>Deletion</th>
|
21
|
+
</tr>
|
22
|
+
<tr>
|
23
|
+
<td> ActiveRecord </td>
|
24
|
+
<td> Yes</td>
|
25
|
+
<td> <b>Yes</b></td>
|
26
|
+
<td> Yes</td>
|
27
|
+
</tr>
|
28
|
+
<tr>
|
29
|
+
<td> DataMapper</td>
|
30
|
+
<td> Yes</td>
|
31
|
+
<td> <b>Yes</b></td>
|
32
|
+
<td> No</td>
|
33
|
+
</tr>
|
34
|
+
<tr>
|
35
|
+
<td> CouchPotato</td>
|
36
|
+
<td> <b>Yes</b></td>
|
37
|
+
<td> No</td>
|
38
|
+
<td> No</td>
|
39
|
+
</tr>
|
40
|
+
<tr>
|
41
|
+
<td> MongoMapper</td>
|
42
|
+
<td> <b>Yes</b></td>
|
43
|
+
<td> No</td>
|
44
|
+
<td> No</td>
|
45
|
+
</tr>
|
46
|
+
<tr>
|
47
|
+
<td> Mongoid</td>
|
48
|
+
<td> <b>Yes</b></td>
|
49
|
+
<td> No</td>
|
50
|
+
<td> No</td>
|
51
|
+
</tr>
|
52
|
+
<tr>
|
53
|
+
<td> Sequel</td>
|
54
|
+
<td> <b>Yes</b></td>
|
55
|
+
<td> Yes</td>
|
56
|
+
<td> No</td>
|
57
|
+
</tr>
|
58
|
+
</tbody>
|
59
|
+
</table>
|
60
|
+
|
61
|
+
<table>
|
62
|
+
<tbody>
|
63
|
+
<tr>
|
64
|
+
<th>Driver</th>
|
65
|
+
<th>Truncation</th>
|
66
|
+
<th>Transaction</th>
|
67
|
+
<th>Deletion</th>
|
68
|
+
</tr>
|
69
|
+
<tr>
|
70
|
+
<td> Mongo</td>
|
71
|
+
<td> Yes</td>
|
72
|
+
<td> No</td>
|
73
|
+
<td> No</td>
|
74
|
+
</tr>
|
75
|
+
</tbody>
|
76
|
+
</table>
|
77
|
+
|
78
|
+
(Default strategy for each library is denoted in bold)
|
79
|
+
|
80
|
+
Database Cleaner also includes a `null` strategy (that does no cleaning at all) which can be used with any ORM library.
|
81
|
+
You can also explicitly use it by setting your strategy to `nil`.
|
82
|
+
|
83
|
+
For support or to discuss development please use the [Google Group](http://groups.google.com/group/database_cleaner).
|
84
|
+
|
85
|
+
## What strategy is fastest?
|
86
|
+
|
87
|
+
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.
|
88
|
+
|
89
|
+
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.
|
90
|
+
|
91
|
+
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 uncommited transactions).
|
92
|
+
|
93
|
+
An easier, but slower, solution is to use the `:truncation` or `:deletion` strategy.
|
94
|
+
|
95
|
+
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 the the scope of this README but here is a [good SO answer on this topic for Postgres](http://stackoverflow.com/questions/11419536/postgresql-truncation-speed/11423886#11423886).
|
96
|
+
|
97
|
+
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.
|
98
|
+
|
99
|
+
If you are using ActiveRecord then take a look at the [additional options](#additional-activerecord-options-for-truncation) available for `:truncation`.
|
100
|
+
|
101
|
+
## Dependencies
|
102
|
+
|
103
|
+
Because database_cleaner supports multiple ORMs, it doesn't make sense to include all the dependencies for each one in the gemspec. However, the DataMapper adapter does depend on dm-transactions. Therefore, if you use DataMapper, you must include dm-transactions in your Gemfile/bundle/gemset manually.
|
104
|
+
|
105
|
+
## How to use
|
106
|
+
|
107
|
+
```ruby
|
108
|
+
require 'database_cleaner'
|
109
|
+
|
110
|
+
DatabaseCleaner.strategy = :truncation
|
111
|
+
|
112
|
+
# then, whenever you need to clean the DB
|
113
|
+
DatabaseCleaner.clean
|
114
|
+
```
|
115
|
+
|
116
|
+
With the `:truncation` strategy you can also pass in options, for example:
|
117
|
+
|
118
|
+
```ruby
|
119
|
+
DatabaseCleaner.strategy = :truncation, {:only => %w[widgets dogs some_other_table]}
|
120
|
+
```
|
121
|
+
|
122
|
+
```ruby
|
123
|
+
DatabaseCleaner.strategy = :truncation, {:except => %w[widgets]}
|
124
|
+
```
|
125
|
+
|
126
|
+
(I should point out the truncation strategy will never truncate your schema_migrations table.)
|
127
|
+
|
128
|
+
Some strategies require that you call `DatabaseCleaner.start` before calling `clean` (for example the `:transaction` one needs to know to open up a transaction). So you would have:
|
129
|
+
|
130
|
+
```ruby
|
131
|
+
require 'database_cleaner'
|
132
|
+
|
133
|
+
DatabaseCleaner.strategy = :transaction
|
134
|
+
|
135
|
+
DatabaseCleaner.start # usually this is called in setup of a test
|
136
|
+
|
137
|
+
dirty_the_db
|
138
|
+
|
139
|
+
DatabaseCleaner.clean # cleanup of the test
|
140
|
+
```
|
141
|
+
|
142
|
+
At times you may want to do a single clean with one strategy.
|
143
|
+
|
144
|
+
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:
|
145
|
+
|
146
|
+
```ruby
|
147
|
+
require 'database_cleaner'
|
148
|
+
|
149
|
+
DatabaseCleaner.clean_with :truncation
|
150
|
+
|
151
|
+
DatabaseCleaner.strategy = :transaction
|
152
|
+
|
153
|
+
# then make the DatabaseCleaner.start and DatabaseCleaner.clean calls appropriately
|
154
|
+
```
|
155
|
+
|
156
|
+
### Additional ActiveRecord options for Truncation
|
157
|
+
|
158
|
+
The following options are available for ActiveRecord's `:truncation` strategy _only_ for MySQL and Postgres.
|
159
|
+
|
160
|
+
* `:pre_count` - When set to `true` this will check each table for existing rows before truncating it. This can speed up test suites when many of the tables to be truncated are never populated. Defaults to `:false`. (Also, see the section on [What strategy is fastest?](#what-strategy-is-fastest))
|
161
|
+
* `:reset_ids` - This only matters when `:pre_count` is used, and it will make sure that a tables auto-incrementing id is reset even if there are no rows in the table (e.g. records were created in the test but also removed before DatabaseCleaner gets to it). Defaults to `true`.
|
162
|
+
|
163
|
+
|
164
|
+
### RSpec Example
|
165
|
+
|
166
|
+
```ruby
|
167
|
+
RSpec.configure do |config|
|
168
|
+
|
169
|
+
config.before(:suite) do
|
170
|
+
DatabaseCleaner.strategy = :transaction
|
171
|
+
DatabaseCleaner.clean_with(:truncation)
|
172
|
+
end
|
173
|
+
|
174
|
+
config.before(:each) do
|
175
|
+
DatabaseCleaner.start
|
176
|
+
end
|
177
|
+
|
178
|
+
config.after(:each) do
|
179
|
+
DatabaseCleaner.clean
|
180
|
+
end
|
181
|
+
|
182
|
+
end
|
183
|
+
```
|
184
|
+
|
185
|
+
### Minitest Example
|
186
|
+
|
187
|
+
```ruby
|
188
|
+
DatabaseCleaner.strategy = :transaction
|
189
|
+
|
190
|
+
class MiniTest::Spec
|
191
|
+
before :each do
|
192
|
+
DatabaseCleaner.start
|
193
|
+
end
|
194
|
+
|
195
|
+
after :each do
|
196
|
+
DatabaseCleaner.clean
|
197
|
+
end
|
198
|
+
end
|
199
|
+
```
|
200
|
+
|
201
|
+
### Cucumber Example
|
202
|
+
|
203
|
+
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.
|
204
|
+
|
205
|
+
Otherwise, to add DatabaseCleaner to your project by hand, create a file `features/support/database_cleaner.rb` that looks like this:
|
206
|
+
|
207
|
+
```ruby
|
208
|
+
begin
|
209
|
+
require 'database_cleaner'
|
210
|
+
require 'database_cleaner/cucumber'
|
211
|
+
|
212
|
+
DatabaseCleaner.strategy = :truncation
|
213
|
+
rescue NameError
|
214
|
+
raise "You need to add database_cleaner to your Gemfile (in the :test group) if you wish to use it."
|
215
|
+
end
|
216
|
+
|
217
|
+
Before do
|
218
|
+
DatabaseCleaner.start
|
219
|
+
end
|
220
|
+
|
221
|
+
After do |scenario|
|
222
|
+
DatabaseCleaner.clean
|
223
|
+
end
|
224
|
+
```
|
225
|
+
|
226
|
+
This should cover the basics of tear down between scenarios and keeping your database clean.
|
227
|
+
|
228
|
+
For more examples see the section ["Why?"](#why).
|
229
|
+
|
230
|
+
## How to use with multiple ORM's
|
231
|
+
|
232
|
+
Sometimes you need to use multiple ORMs in your application.
|
233
|
+
|
234
|
+
You can use DatabaseCleaner to clean multiple ORMs, and multiple connections for those ORMs.
|
235
|
+
|
236
|
+
```ruby
|
237
|
+
#How to specify particular orms
|
238
|
+
DatabaseCleaner[:active_record].strategy = :transaction
|
239
|
+
DatabaseCleaner[:mongo_mapper].strategy = :truncation
|
240
|
+
|
241
|
+
#How to specify particular connections
|
242
|
+
DatabaseCleaner[:active_record,{:connection => :two}]
|
243
|
+
|
244
|
+
# You may also pass in the model directly:
|
245
|
+
DatabaseCleaner[:active_record,{:model => ModelWithDifferentConnection}]
|
246
|
+
```
|
247
|
+
|
248
|
+
Usage beyond that remains the same with `DatabaseCleaner.start` calling any setup on the different configured connections, and `DatabaseCleaner.clean` executing afterwards.
|
249
|
+
|
250
|
+
### Configuration options
|
251
|
+
|
252
|
+
<table>
|
253
|
+
<tbody>
|
254
|
+
<tr>
|
255
|
+
<th>ORM</th>
|
256
|
+
<th>How to access</th>
|
257
|
+
<th>Notes</th>
|
258
|
+
</tr>
|
259
|
+
<tr>
|
260
|
+
<td> Active Record </td>
|
261
|
+
<td> <code>DatabaseCleaner[:active_record]</code></td>
|
262
|
+
<td> Connection specified as <code>:symbol</code> keys, loaded from <code>config/database.yml</code>. You may also pass in the ActiveRecord model under the <code>:model</code> key.</td>
|
263
|
+
</tr>
|
264
|
+
<tr>
|
265
|
+
<td> Data Mapper</td>
|
266
|
+
<td> <code>DatabaseCleaner[:data_mapper]</code></td>
|
267
|
+
<td> Connection specified as <code>:symbol</code> keys, loaded via Datamapper repositories </td>
|
268
|
+
</tr>
|
269
|
+
<tr>
|
270
|
+
<td> Mongo Mapper</td>
|
271
|
+
<td> <code>DatabaseCleaner[:mongo_mapper]</code></td>
|
272
|
+
<td> Multiple connections not yet supported</td>
|
273
|
+
</tr>
|
274
|
+
<tr>
|
275
|
+
<td> Mongoid</td>
|
276
|
+
<td> <code>DatabaseCleaner[:mongoid]</code></td>
|
277
|
+
<td> Multiple databases supported for Mongoid 3. Specify <code>DatabaseCleaner[:mongoid, {:connection => :db_name}]</code> </td>
|
278
|
+
</tr>
|
279
|
+
<tr>
|
280
|
+
<td> Couch Potato</td>
|
281
|
+
<td> <code>DatabaseCleaner[:couch_potato]</code></td>
|
282
|
+
<td> Multiple connections not yet supported</td>
|
283
|
+
</tr>
|
284
|
+
<tr>
|
285
|
+
<td> Sequel</td>
|
286
|
+
<td> <code>DatabaseCleaner[:sequel]</code></td>
|
287
|
+
<td> Multiple databases supported; specify <code>Databasecleaner[:sequel, {:connection => Sequel.connect(uri)}]</code></td>
|
288
|
+
</tr>
|
289
|
+
</tbody>
|
290
|
+
</table>
|
291
|
+
|
292
|
+
## Why?
|
293
|
+
|
294
|
+
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.
|
295
|
+
|
296
|
+
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.
|
297
|
+
|
298
|
+
## Common Errors
|
299
|
+
|
300
|
+
#### DatabaseCleaner is trying to use the wrong ORM
|
301
|
+
|
302
|
+
DatabaseCleaner has an autodetect mechanism where if you do not explicitly define your ORM it will use the first ORM it can detect that is loaded.
|
303
|
+
|
304
|
+
Since ActiveRecord is the most common ORM used that is the first one checked for.
|
305
|
+
|
306
|
+
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 define your ORM like so:
|
307
|
+
|
308
|
+
```ruby
|
309
|
+
# How to setup your ORM explicitly
|
310
|
+
DatabaseCleaner[:mongoid].strategy = :truncation
|
311
|
+
```
|
312
|
+
|
313
|
+
### STDERR is being flooded when using Postgres
|
314
|
+
|
315
|
+
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). To silence these warnings set the following log level in your `postgresql.conf` file:
|
316
|
+
|
317
|
+
```ruby
|
318
|
+
client_min_messages = warning
|
319
|
+
```
|
320
|
+
|
321
|
+
|
322
|
+
## Debugging
|
323
|
+
|
324
|
+
In rare cases DatabaseCleaner will encounter errors that it will log. By default it uses STDOUT set to the ERROR level but you can configure this to use whatever Logger you desire.
|
325
|
+
|
326
|
+
Here's an example of using the `Rails.logger` in `env.rb`:
|
327
|
+
|
328
|
+
```ruby
|
329
|
+
DatabaseCleaner.logger = Rails.logger
|
330
|
+
```
|
331
|
+
|
332
|
+
|
333
|
+
## COPYRIGHT
|
334
|
+
|
335
|
+
Copyright (c) 2009 Ben Mabey. See LICENSE for details.
|