database_cleaner 1.7.0 → 2.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (52) hide show
  1. checksums.yaml +4 -4
  2. data/lib/database_cleaner.rb +1 -12
  3. metadata +24 -418
  4. data/CONTRIBUTE.markdown +0 -28
  5. data/Gemfile.lock +0 -286
  6. data/History.rdoc +0 -453
  7. data/LICENSE +0 -20
  8. data/README.markdown +0 -541
  9. data/Rakefile +0 -40
  10. data/TODO +0 -3
  11. data/VERSION.yml +0 -4
  12. data/cucumber.yml +0 -1
  13. data/lib/database_cleaner/active_record/base.rb +0 -92
  14. data/lib/database_cleaner/active_record/deletion.rb +0 -108
  15. data/lib/database_cleaner/active_record/transaction.rb +0 -59
  16. data/lib/database_cleaner/active_record/truncation.rb +0 -275
  17. data/lib/database_cleaner/base.rb +0 -166
  18. data/lib/database_cleaner/configuration.rb +0 -131
  19. data/lib/database_cleaner/couch_potato/base.rb +0 -7
  20. data/lib/database_cleaner/couch_potato/truncation.rb +0 -28
  21. data/lib/database_cleaner/cucumber.rb +0 -3
  22. data/lib/database_cleaner/data_mapper/base.rb +0 -21
  23. data/lib/database_cleaner/data_mapper/transaction.rb +0 -28
  24. data/lib/database_cleaner/data_mapper/truncation.rb +0 -172
  25. data/lib/database_cleaner/generic/base.rb +0 -29
  26. data/lib/database_cleaner/generic/transaction.rb +0 -11
  27. data/lib/database_cleaner/generic/truncation.rb +0 -40
  28. data/lib/database_cleaner/mongo/base.rb +0 -16
  29. data/lib/database_cleaner/mongo/truncation.rb +0 -62
  30. data/lib/database_cleaner/mongo/truncation_mixin.rb +0 -26
  31. data/lib/database_cleaner/mongo2/base.rb +0 -16
  32. data/lib/database_cleaner/mongo2/truncation_mixin.rb +0 -39
  33. data/lib/database_cleaner/mongo_mapper/base.rb +0 -20
  34. data/lib/database_cleaner/mongo_mapper/truncation.rb +0 -19
  35. data/lib/database_cleaner/mongoid/base.rb +0 -20
  36. data/lib/database_cleaner/mongoid/truncation.rb +0 -49
  37. data/lib/database_cleaner/moped/base.rb +0 -39
  38. data/lib/database_cleaner/moped/truncation.rb +0 -9
  39. data/lib/database_cleaner/moped/truncation_base.rb +0 -40
  40. data/lib/database_cleaner/neo4j/base.rb +0 -62
  41. data/lib/database_cleaner/neo4j/deletion.rb +0 -16
  42. data/lib/database_cleaner/neo4j/transaction.rb +0 -35
  43. data/lib/database_cleaner/neo4j/truncation.rb +0 -9
  44. data/lib/database_cleaner/null_strategy.rb +0 -20
  45. data/lib/database_cleaner/ohm/truncation.rb +0 -15
  46. data/lib/database_cleaner/redis/base.rb +0 -37
  47. data/lib/database_cleaner/redis/truncation.rb +0 -26
  48. data/lib/database_cleaner/safeguard.rb +0 -72
  49. data/lib/database_cleaner/sequel/base.rb +0 -22
  50. data/lib/database_cleaner/sequel/deletion.rb +0 -47
  51. data/lib/database_cleaner/sequel/transaction.rb +0 -40
  52. data/lib/database_cleaner/sequel/truncation.rb +0 -78
data/README.markdown DELETED
@@ -1,541 +0,0 @@
1
- # Database Cleaner
2
-
3
- [![Build Status](https://travis-ci.org/DatabaseCleaner/database_cleaner.svg?branch=master)](https://travis-ci.org/DatabaseCleaner/database_cleaner)
4
- [![Code Climate](https://codeclimate.com/github/DatabaseCleaner/database_cleaner/badges/gpa.svg)](https://codeclimate.com/github/DatabaseCleaner/database_cleaner)
5
-
6
- Database Cleaner is a set of strategies for cleaning your database in Ruby.
7
-
8
- The original use case was to ensure a clean state during tests.
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.
10
-
11
- ## Gem Setup
12
-
13
- ```ruby
14
- # Gemfile
15
- group :test do
16
- gem 'database_cleaner'
17
- end
18
- ```
19
-
20
- ## Supported Databases, Libraries and Strategies
21
-
22
- ActiveRecord, DataMapper, Sequel, MongoMapper, Mongoid, CouchPotato, Ohm and Redis are supported.
23
-
24
- Here is an overview of the strategies supported for each library:
25
-
26
- <table>
27
- <tbody>
28
- <tr>
29
- <th>ORM</th>
30
- <th>Truncation</th>
31
- <th>Transaction</th>
32
- <th>Deletion</th>
33
- </tr>
34
- <tr>
35
- <td> ActiveRecord </td>
36
- <td> Yes</td>
37
- <td> <b>Yes</b></td>
38
- <td> Yes</td>
39
- </tr>
40
- <tr>
41
- <td> DataMapper</td>
42
- <td> Yes</td>
43
- <td> <b>Yes</b></td>
44
- <td> No</td>
45
- </tr>
46
- <tr>
47
- <td> CouchPotato</td>
48
- <td> <b>Yes</b></td>
49
- <td> No</td>
50
- <td> No</td>
51
- </tr>
52
- <tr>
53
- <td> MongoMapper</td>
54
- <td> <b>Yes</b></td>
55
- <td> No</td>
56
- <td> No</td>
57
- </tr>
58
- <tr>
59
- <td> Mongoid</td>
60
- <td> <b>Yes</b></td>
61
- <td> No</td>
62
- <td> No</td>
63
- </tr>
64
- <tr>
65
- <td> Sequel</td>
66
- <td> <b>Yes</b></td>
67
- <td> Yes</td>
68
- <td> Yes</td>
69
- </tr>
70
- <tr>
71
- <td>Redis</td>
72
- <td><b>Yes</b></td>
73
- <td>No</td>
74
- <td>No</td>
75
- </tr>
76
- <tr>
77
- <td>Ohm</td>
78
- <td><b>Yes</b></td>
79
- <td>No</td>
80
- <td>No</td>
81
- </tr>
82
- <tr>
83
- <td>Neo4j</td>
84
- <td>Yes</td>
85
- <td>Yes*</td>
86
- <td>Yes*</td>
87
- </tr>
88
- </tbody>
89
- </table>
90
-
91
- \* Truncation and Deletion strategies for Neo4j will just delete all nodes and relationships from the database.
92
-
93
- <table>
94
- <tbody>
95
- <tr>
96
- <th>Driver</th>
97
- <th>Truncation</th>
98
- <th>Transaction</th>
99
- <th>Deletion</th>
100
- </tr>
101
- <tr>
102
- <td> Mongo</td>
103
- <td> Yes</td>
104
- <td> No</td>
105
- <td> No</td>
106
- </tr>
107
- <tr>
108
- <td> Moped</td>
109
- <td> Yes</td>
110
- <td> No</td>
111
- <td> No</td>
112
- </tr>
113
- </tbody>
114
- </table>
115
-
116
- (Default strategy for each library is denoted in bold)
117
-
118
- Database Cleaner also includes a `null` strategy (that does no cleaning at all) which can be used with any ORM library.
119
- You can also explicitly use it by setting your strategy to `nil`.
120
-
121
- For support or to discuss development please use the [Google Group](http://groups.google.com/group/database_cleaner).
122
-
123
- ## What strategy is fastest?
124
-
125
- 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.
126
-
127
- 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.
128
-
129
- 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).
130
-
131
- An easier, but slower, solution is to use the `:truncation` or `:deletion` strategy.
132
-
133
- 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](http://stackoverflow.com/questions/11419536/postgresql-truncation-speed/11423886#11423886).
134
-
135
- 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.
136
-
137
- If you are using ActiveRecord then take a look at the [additional options](#additional-activerecord-options-for-truncation) available for `:truncation`.
138
-
139
- ## Dependencies
140
-
141
- 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.
142
-
143
- ## How to use
144
-
145
- ```ruby
146
- require 'database_cleaner'
147
-
148
- DatabaseCleaner.strategy = :truncation
149
-
150
- # then, whenever you need to clean the DB
151
- DatabaseCleaner.clean
152
- ```
153
-
154
- With the `:truncation` strategy you can also pass in options, for example:
155
-
156
- ```ruby
157
- DatabaseCleaner.strategy = :truncation, {:only => %w[widgets dogs some_other_table]}
158
- ```
159
-
160
- ```ruby
161
- DatabaseCleaner.strategy = :truncation, {:except => %w[widgets]}
162
- ```
163
-
164
- With Ohm and Redis, `:only` and `:except` take a list of strings to be
165
- passed to [`keys`](http://redis.io/commands/keys)).
166
-
167
- (I should point out the truncation strategy will never truncate your schema_migrations table.)
168
-
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:
170
-
171
- ```ruby
172
- require 'database_cleaner'
173
-
174
- DatabaseCleaner.strategy = :transaction
175
-
176
- DatabaseCleaner.start # usually this is called in setup of a test
177
-
178
- dirty_the_db
179
-
180
- DatabaseCleaner.clean # cleanup of the test
181
-
182
- # OR
183
-
184
- DatabaseCleaner.cleaning do
185
- dirty_the_db
186
- end
187
- ```
188
-
189
- At times you may want to do a single clean with one strategy.
190
-
191
- 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:
192
-
193
- ```ruby
194
- require 'database_cleaner'
195
-
196
- DatabaseCleaner.clean_with :truncation
197
-
198
- DatabaseCleaner.strategy = :transaction
199
-
200
- # then make the DatabaseCleaner.start and DatabaseCleaner.clean calls appropriately
201
- ```
202
-
203
- ### Additional ActiveRecord options for Truncation
204
-
205
- The following options are available for ActiveRecord's `:truncation` strategy _only_ for MySQL and Postgres.
206
-
207
- * `: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))
208
- * `: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`.
209
-
210
- The following option is available for ActiveRecord's `:truncation` and `:deletion` strategy for any DB.
211
-
212
- * `:cache_tables` - When set to `true` the list of tables to truncate or delete from will only be read from the DB once, otherwise it will be read before each cleanup run. Set this to `false` if (1) you create and drop tables in your tests, or (2) you change Postgres schemas (`ActiveRecord::Base.connection.schema_search_path`) in your tests (for example, in a multitenancy setup with each tenant in a different Postgres schema). Defaults to `true`.
213
-
214
-
215
- ### RSpec Example
216
-
217
- ```ruby
218
- RSpec.configure do |config|
219
-
220
- config.before(:suite) do
221
- DatabaseCleaner.strategy = :transaction
222
- DatabaseCleaner.clean_with(:truncation)
223
- end
224
-
225
- config.around(:each) do |example|
226
- DatabaseCleaner.cleaning do
227
- example.run
228
- end
229
- end
230
-
231
- end
232
- ```
233
-
234
- ### RSpec with Capybara Example
235
-
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.
263
-
264
- ```ruby
265
- require 'capybara/rspec'
266
-
267
- #...
268
-
269
- RSpec.configure do |config|
270
-
271
- config.use_transactional_fixtures = false
272
-
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
286
- DatabaseCleaner.clean_with(:truncation)
287
- end
288
-
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
- unless 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
307
- DatabaseCleaner.start
308
- end
309
-
310
- config.append_after(:each) do
311
- DatabaseCleaner.clean
312
- end
313
-
314
- end
315
- ```
316
-
317
-
318
- ### Minitest Example
319
-
320
- ```ruby
321
- DatabaseCleaner.strategy = :transaction
322
-
323
- class Minitest::Spec
324
- before :each do
325
- DatabaseCleaner.start
326
- end
327
-
328
- after :each do
329
- DatabaseCleaner.clean
330
- end
331
- end
332
-
333
- # with the minitest-around gem, this may be used instead:
334
- class Minitest::Spec
335
- around do |tests|
336
- DatabaseCleaner.cleaning(&tests)
337
- end
338
- end
339
- ```
340
-
341
- ### Cucumber Example
342
-
343
- 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.
344
-
345
- Otherwise, to add DatabaseCleaner to your project by hand, create a file `features/support/database_cleaner.rb` that looks like this:
346
-
347
- ```ruby
348
- begin
349
- require 'database_cleaner'
350
- require 'database_cleaner/cucumber'
351
-
352
- DatabaseCleaner.strategy = :truncation
353
- rescue NameError
354
- raise "You need to add database_cleaner to your Gemfile (in the :test group) if you wish to use it."
355
- end
356
-
357
- Around do |scenario, block|
358
- DatabaseCleaner.cleaning(&block)
359
- end
360
- ```
361
-
362
- This should cover the basics of tear down between scenarios and keeping your database clean.
363
-
364
- For more examples see the section ["Why?"](#why).
365
-
366
- ## How to use with multiple ORMs
367
-
368
- Sometimes you need to use multiple ORMs in your application.
369
-
370
- You can use DatabaseCleaner to clean multiple ORMs, and multiple connections for those ORMs.
371
-
372
- ```ruby
373
- #How to specify particular orms
374
- DatabaseCleaner[:active_record].strategy = :transaction
375
- DatabaseCleaner[:mongo_mapper].strategy = :truncation
376
-
377
- #How to specify particular connections
378
- DatabaseCleaner[:active_record, { :connection => :two }]
379
-
380
- # You may also pass in the model directly:
381
- DatabaseCleaner[:active_record, { :model => ModelWithDifferentConnection }]
382
- ```
383
-
384
- Usage beyond that remains the same with `DatabaseCleaner.start` calling any setup on the different configured connections, and `DatabaseCleaner.clean` executing afterwards.
385
-
386
- ### Configuration options
387
-
388
- <table>
389
- <tbody>
390
- <tr>
391
- <th>ORM</th>
392
- <th>How to access</th>
393
- <th>Notes</th>
394
- </tr>
395
- <tr>
396
- <td> Active Record </td>
397
- <td> <code>DatabaseCleaner[:active_record]</code></td>
398
- <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>
399
- </tr>
400
- <tr>
401
- <td> Data Mapper</td>
402
- <td> <code>DatabaseCleaner[:data_mapper]</code></td>
403
- <td> Connection specified as <code>:symbol</code> keys, loaded via Datamapper repositories </td>
404
- </tr>
405
- <tr>
406
- <td> Mongo Mapper</td>
407
- <td> <code>DatabaseCleaner[:mongo_mapper]</code></td>
408
- <td> Multiple connections not yet supported</td>
409
- </tr>
410
- <tr>
411
- <td> Mongoid</td>
412
- <td> <code>DatabaseCleaner[:mongoid]</code></td>
413
- <td> Multiple databases supported for Mongoid 3. Specify <code>DatabaseCleaner[:mongoid, {:connection =&gt; :db_name}]</code> </td>
414
- </tr>
415
- <tr>
416
- <td> Moped</td>
417
- <td> <code>DatabaseCleaner[:moped]</code></td>
418
- <td> It is necessary to configure database name with <code>DatabaseCleaner[:moped].db = db_name</code> otherwise name `default` will be used.</td>
419
- </tr>
420
- <tr>
421
- <td> Couch Potato</td>
422
- <td> <code>DatabaseCleaner[:couch_potato]</code></td>
423
- <td> Multiple connections not yet supported</td>
424
- </tr>
425
- <tr>
426
- <td> Sequel</td>
427
- <td> <code>DatabaseCleaner[:sequel]</code></td>
428
- <td> Multiple databases supported; specify <code>DatabaseCleaner[:sequel, {:connection =&gt; Sequel.connect(uri)}]</code></td>
429
- </tr>
430
- <tr>
431
- <td>Redis</td>
432
- <td><code>DatabaseCleaner[:redis]</code></td>
433
- <td>Connection specified as Redis URI</td>
434
- </tr>
435
- <tr>
436
- <td>Ohm</td>
437
- <td><code>DatabaseCleaner[:ohm]</code></td>
438
- <td>Connection specified as Redis URI</td>
439
- </tr>
440
- <tr>
441
- <td>Neo4j</td>
442
- <td><code>DatabaseCleaner[:neo4j]</code></td>
443
- <td>Database type and path(URI) <code>DatabaseCleaner[:neo4j, connection: {type: :server_db, path: 'http://localhost:7475'}].</code></td>
444
- </tr>
445
- </tbody>
446
- </table>
447
-
448
- ## Why?
449
-
450
- 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.
451
-
452
- 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.
453
-
454
- ## Common Errors
455
-
456
- #### DatabaseCleaner is trying to use the wrong ORM
457
-
458
- 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.
459
-
460
- Since ActiveRecord is the most common ORM used that is the first one checked for.
461
-
462
- 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:
463
-
464
- ```ruby
465
- # How to setup your ORM explicitly
466
- DatabaseCleaner[:mongoid].strategy = :truncation
467
- ```
468
-
469
- ### STDERR is being flooded when using Postgres
470
-
471
- 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).
472
-
473
- To silence these warnings set the following log level in your `postgresql.conf` file:
474
-
475
- ```ruby
476
- client_min_messages = warning
477
- ```
478
-
479
- For ActiveRecord, you add the following parameter in your database.yml file:
480
-
481
- <pre>
482
- test:
483
- adapter: postgresql
484
- # ...
485
- min_messages: WARNING
486
- </pre>
487
-
488
- ### Nothing happens in JRuby with Sequel using transactions
489
-
490
- Due to an inconsistency in JRuby's implementation of Fibers, Sequel gives a different connection to `DatabaseCleaner.start` than is used for tests run between `.start` and `.clean`. This can be worked around by running your tests in a block like `DatabaseCleaner.cleaning { run_my_tests }` instead, which does not use Fibers.
491
-
492
- ### Model fails to load with Neo4j using transactions
493
-
494
- When you are using [neo4j](https://github.com/neo4jrb/neo4j) gem it creates schema and reads indexes upon loading models. These operations can't be done during a transaction. You have to preload your models before DatabaseCleaner starts a transaction.
495
-
496
- Add to your rails_helper or spec_helper after requiring database_cleaner:
497
-
498
- ```ruby
499
- require 'database_cleaner'
500
- Dir["#{Rails.root}/app/models/**/*.rb"].each do |model|
501
- load model
502
- end
503
- ```
504
-
505
- ## Safeguards
506
-
507
- DatabaseCleaner comes with safeguards against:
508
-
509
- * Running in production (checking for `ENV`, `RACK_ENV`, and `RAILS_ENV`)
510
- * Running against a remote database (checking for a `DATABASE_URL` that does not include `localhost`)
511
-
512
- Both safeguards can be disabled separately as follows.
513
-
514
- Using environment variables:
515
-
516
- ```
517
- export DATABASE_CLEANER_ALLOW_PRODUCTION=true
518
- export DATABASE_CLEANER_ALLOW_REMOTE_DATABASE_URL=true
519
- ```
520
-
521
- In Ruby:
522
-
523
- ```ruby
524
- DatabaseCleaner.allow_production = true
525
- DatabaseCleaner.allow_remote_database_url = true
526
- ```
527
-
528
- ## Debugging
529
-
530
- 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.
531
-
532
- Here's an example of using the `Rails.logger` in `env.rb`:
533
-
534
- ```ruby
535
- DatabaseCleaner.logger = Rails.logger
536
- ```
537
-
538
-
539
- ## COPYRIGHT
540
-
541
- Copyright (c) 2014 Ben Mabey. See LICENSE for details.
data/Rakefile DELETED
@@ -1,40 +0,0 @@
1
- require "rubygems"
2
- require "bundler"
3
- Bundler.setup
4
-
5
- require 'rake'
6
- require 'rspec/core'
7
- require 'rspec/core/rake_task'
8
- RSpec::Core::RakeTask.new(:spec) do |spec|
9
-
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
- begin
19
- require 'cucumber/rake/task'
20
- Cucumber::Rake::Task.new(:features)
21
- rescue LoadError
22
- puts "Cucumber is not available. In order to run features, you must: sudo gem install cucumber"
23
- end
24
-
25
- task :default => [:spec, :features]
26
-
27
-
28
- desc "Cleans the project of any tmp file that should not be included in the gemspec."
29
- task :clean do
30
- ["examples/config/database.yml", "examples/db/activerecord_one.db", "examples/db/activerecord_two.db", "examples/db/datamapper_default.db",
31
- "examples/db/datamapper_one.db", "examples/db/datamapper_two.db"].each do |f|
32
- FileUtils.rm_f(f)
33
- end
34
- %w[*.sqlite3 *.log #* *.swp *.swo].each do |pattern|
35
- `find . -name "#{pattern}" -delete`
36
- end
37
- end
38
-
39
- desc "Cleans the dir and builds the gem"
40
- task :prep => [:clean, :gemspec, :build]
data/TODO DELETED
@@ -1,3 +0,0 @@
1
- Could be more Datamapper
2
- MongoMapper multiple db support
3
- CouchDB multiple db support
data/VERSION.yml DELETED
@@ -1,4 +0,0 @@
1
- ---
2
- :major: 1
3
- :minor: 7
4
- :patch: 0
data/cucumber.yml DELETED
@@ -1 +0,0 @@
1
- default: features
@@ -1,92 +0,0 @@
1
- require 'database_cleaner/generic/base'
2
- require 'active_record'
3
- require 'erb'
4
-
5
- module DatabaseCleaner
6
- module ActiveRecord
7
-
8
- def self.available_strategies
9
- %w[truncation transaction deletion]
10
- end
11
-
12
- def self.config_file_location=(path)
13
- @config_file_location = path
14
- end
15
-
16
- def self.config_file_location
17
- @config_file_location ||= "#{DatabaseCleaner.app_root}/config/database.yml"
18
- end
19
-
20
- module Base
21
- include ::DatabaseCleaner::Generic::Base
22
-
23
- attr_accessor :connection_hash
24
-
25
- def db=(desired_db)
26
- @db = desired_db
27
- load_config
28
- end
29
-
30
- def db
31
- @db ||= super
32
- end
33
-
34
- def load_config
35
- if self.db != :default && self.db.is_a?(Symbol) && File.file?(ActiveRecord.config_file_location)
36
- connection_details = YAML::load(ERB.new(IO.read(ActiveRecord.config_file_location)).result)
37
- @connection_hash = valid_config(connection_details)[self.db.to_s]
38
- end
39
- end
40
-
41
- def valid_config(connection_file)
42
- if !::ActiveRecord::Base.configurations.nil? && !::ActiveRecord::Base.configurations.empty?
43
- if connection_file != ::ActiveRecord::Base.configurations
44
- return ::ActiveRecord::Base.configurations
45
- end
46
- end
47
- connection_file
48
- end
49
-
50
- def connection_class
51
- @connection_class ||= if db && !db.is_a?(Symbol)
52
- db
53
- elsif connection_hash
54
- lookup_from_connection_pool || establish_connection
55
- else
56
- ::ActiveRecord::Base
57
- end
58
- end
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
-
68
- def self.exclusion_condition(column_name)
69
- result = " #{column_name} <> '#{::DatabaseCleaner::ActiveRecord::Base.migration_table_name}' "
70
- if ::ActiveRecord::VERSION::MAJOR >= 5
71
- result += " AND #{column_name} <> '#{::ActiveRecord::Base.internal_metadata_table_name}' "
72
- end
73
- result
74
- end
75
-
76
- private
77
-
78
- def lookup_from_connection_pool
79
- if ::ActiveRecord::Base.respond_to?(:descendants)
80
- database_name = connection_hash["database"] || connection_hash[:database]
81
- models = ::ActiveRecord::Base.descendants
82
- models.detect { |m| m.connection_pool.spec.config[:database] == database_name }
83
- end
84
- end
85
-
86
- def establish_connection
87
- ::ActiveRecord::Base.establish_connection(connection_hash)
88
- ::ActiveRecord::Base
89
- end
90
- end
91
- end
92
- end