database_cleaner 1.8.5 → 2.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (91) hide show
  1. checksums.yaml +4 -4
  2. data/lib/database_cleaner.rb +1 -47
  3. metadata +19 -480
  4. data/CONTRIBUTE.markdown +0 -27
  5. data/Gemfile.lock +0 -325
  6. data/History.rdoc +0 -497
  7. data/LICENSE +0 -20
  8. data/README.markdown +0 -389
  9. data/Rakefile +0 -44
  10. data/TODO +0 -3
  11. data/adapters/database_cleaner-active_record/lib/database_cleaner/active_record/base.rb +0 -101
  12. data/adapters/database_cleaner-active_record/lib/database_cleaner/active_record/deletion.rb +0 -108
  13. data/adapters/database_cleaner-active_record/lib/database_cleaner/active_record/transaction.rb +0 -59
  14. data/adapters/database_cleaner-active_record/lib/database_cleaner/active_record/truncation.rb +0 -280
  15. data/adapters/database_cleaner-active_record/lib/database_cleaner/active_record/version.rb +0 -5
  16. data/adapters/database_cleaner-active_record/lib/database_cleaner/active_record.rb +0 -6
  17. data/adapters/database_cleaner-active_record/lib/database_cleaner-active_record.rb +0 -1
  18. data/adapters/database_cleaner-couch_potato/lib/database_cleaner/couch_potato/base.rb +0 -7
  19. data/adapters/database_cleaner-couch_potato/lib/database_cleaner/couch_potato/truncation.rb +0 -28
  20. data/adapters/database_cleaner-couch_potato/lib/database_cleaner/couch_potato/version.rb +0 -5
  21. data/adapters/database_cleaner-couch_potato/lib/database_cleaner/couch_potato.rb +0 -11
  22. data/adapters/database_cleaner-couch_potato/lib/database_cleaner-couch_potato.rb +0 -1
  23. data/adapters/database_cleaner-data_mapper/lib/database_cleaner/data_mapper/base.rb +0 -25
  24. data/adapters/database_cleaner-data_mapper/lib/database_cleaner/data_mapper/transaction.rb +0 -28
  25. data/adapters/database_cleaner-data_mapper/lib/database_cleaner/data_mapper/truncation.rb +0 -173
  26. data/adapters/database_cleaner-data_mapper/lib/database_cleaner/data_mapper/version.rb +0 -5
  27. data/adapters/database_cleaner-data_mapper/lib/database_cleaner/data_mapper.rb +0 -4
  28. data/adapters/database_cleaner-data_mapper/lib/database_cleaner-data_mapper.rb +0 -1
  29. data/adapters/database_cleaner-mongo/lib/database_cleaner/mongo/base.rb +0 -16
  30. data/adapters/database_cleaner-mongo/lib/database_cleaner/mongo/truncation.rb +0 -64
  31. data/adapters/database_cleaner-mongo/lib/database_cleaner/mongo/truncation_mixin.rb +0 -26
  32. data/adapters/database_cleaner-mongo/lib/database_cleaner/mongo/version.rb +0 -5
  33. data/adapters/database_cleaner-mongo/lib/database_cleaner/mongo.rb +0 -10
  34. data/adapters/database_cleaner-mongo/lib/database_cleaner-mongo.rb +0 -1
  35. data/adapters/database_cleaner-mongo_mapper/lib/database_cleaner/mongo_mapper/base.rb +0 -24
  36. data/adapters/database_cleaner-mongo_mapper/lib/database_cleaner/mongo_mapper/truncation.rb +0 -35
  37. data/adapters/database_cleaner-mongo_mapper/lib/database_cleaner/mongo_mapper/version.rb +0 -5
  38. data/adapters/database_cleaner-mongo_mapper/lib/database_cleaner/mongo_mapper.rb +0 -4
  39. data/adapters/database_cleaner-mongo_mapper/lib/database_cleaner-mongo_mapper.rb +0 -1
  40. data/adapters/database_cleaner-mongoid/lib/database_cleaner/mongoid/base.rb +0 -20
  41. data/adapters/database_cleaner-mongoid/lib/database_cleaner/mongoid/mongo1_truncation_mixin.rb +0 -26
  42. data/adapters/database_cleaner-mongoid/lib/database_cleaner/mongoid/mongo2_truncation_mixin.rb +0 -38
  43. data/adapters/database_cleaner-mongoid/lib/database_cleaner/mongoid/mongoid_truncation_mixin.rb +0 -65
  44. data/adapters/database_cleaner-mongoid/lib/database_cleaner/mongoid/truncation.rb +0 -49
  45. data/adapters/database_cleaner-mongoid/lib/database_cleaner/mongoid/version.rb +0 -5
  46. data/adapters/database_cleaner-mongoid/lib/database_cleaner/mongoid.rb +0 -10
  47. data/adapters/database_cleaner-mongoid/lib/database_cleaner-mongoid.rb +0 -1
  48. data/adapters/database_cleaner-moped/lib/database_cleaner/moped/base.rb +0 -39
  49. data/adapters/database_cleaner-moped/lib/database_cleaner/moped/truncation.rb +0 -9
  50. data/adapters/database_cleaner-moped/lib/database_cleaner/moped/truncation_base.rb +0 -44
  51. data/adapters/database_cleaner-moped/lib/database_cleaner/moped/version.rb +0 -5
  52. data/adapters/database_cleaner-moped/lib/database_cleaner/moped.rb +0 -10
  53. data/adapters/database_cleaner-moped/lib/database_cleaner-moped.rb +0 -1
  54. data/adapters/database_cleaner-neo4j/lib/database_cleaner/neo4j/base.rb +0 -66
  55. data/adapters/database_cleaner-neo4j/lib/database_cleaner/neo4j/deletion.rb +0 -16
  56. data/adapters/database_cleaner-neo4j/lib/database_cleaner/neo4j/transaction.rb +0 -35
  57. data/adapters/database_cleaner-neo4j/lib/database_cleaner/neo4j/truncation.rb +0 -9
  58. data/adapters/database_cleaner-neo4j/lib/database_cleaner/neo4j/version.rb +0 -5
  59. data/adapters/database_cleaner-neo4j/lib/database_cleaner/neo4j.rb +0 -6
  60. data/adapters/database_cleaner-neo4j/lib/database_cleaner-neo4j.rb +0 -1
  61. data/adapters/database_cleaner-ohm/lib/database_cleaner/ohm/truncation.rb +0 -24
  62. data/adapters/database_cleaner-ohm/lib/database_cleaner/ohm/version.rb +0 -5
  63. data/adapters/database_cleaner-ohm/lib/database_cleaner/ohm.rb +0 -12
  64. data/adapters/database_cleaner-ohm/lib/database_cleaner-ohm.rb +0 -1
  65. data/adapters/database_cleaner-redis/lib/database_cleaner/redis/base.rb +0 -41
  66. data/adapters/database_cleaner-redis/lib/database_cleaner/redis/truncation.rb +0 -26
  67. data/adapters/database_cleaner-redis/lib/database_cleaner/redis/version.rb +0 -5
  68. data/adapters/database_cleaner-redis/lib/database_cleaner/redis.rb +0 -4
  69. data/adapters/database_cleaner-redis/lib/database_cleaner-redis.rb +0 -1
  70. data/adapters/database_cleaner-sequel/lib/database_cleaner/sequel/base.rb +0 -26
  71. data/adapters/database_cleaner-sequel/lib/database_cleaner/sequel/deletion.rb +0 -47
  72. data/adapters/database_cleaner-sequel/lib/database_cleaner/sequel/transaction.rb +0 -40
  73. data/adapters/database_cleaner-sequel/lib/database_cleaner/sequel/truncation.rb +0 -79
  74. data/adapters/database_cleaner-sequel/lib/database_cleaner/sequel/version.rb +0 -5
  75. data/adapters/database_cleaner-sequel/lib/database_cleaner/sequel.rb +0 -6
  76. data/adapters/database_cleaner-sequel/lib/database_cleaner-sequel.rb +0 -1
  77. data/cucumber.yml +0 -1
  78. data/lib/database_cleaner/base.rb +0 -153
  79. data/lib/database_cleaner/configuration.rb +0 -135
  80. data/lib/database_cleaner/cucumber.rb +0 -3
  81. data/lib/database_cleaner/deprecation.rb +0 -26
  82. data/lib/database_cleaner/generic/base.rb +0 -29
  83. data/lib/database_cleaner/generic/transaction.rb +0 -11
  84. data/lib/database_cleaner/generic/truncation.rb +0 -40
  85. data/lib/database_cleaner/null_strategy.rb +0 -20
  86. data/lib/database_cleaner/orm_autodetector.rb +0 -41
  87. data/lib/database_cleaner/safeguard.rb +0 -107
  88. data/lib/database_cleaner/spec/database_helper.rb +0 -82
  89. data/lib/database_cleaner/spec/shared_examples.rb +0 -15
  90. data/lib/database_cleaner/spec.rb +0 -2
  91. data/lib/database_cleaner/version.rb +0 -3
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, { :connection => :two }]
310
-
311
- # You may also pass in the model directly:
312
- DatabaseCleaner[:active_record, { :model => 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]
data/TODO DELETED
@@ -1,3 +0,0 @@
1
- Could be more Datamapper
2
- MongoMapper multiple db support
3
- CouchDB multiple db support
@@ -1,101 +0,0 @@
1
- require 'active_record'
2
- require 'database_cleaner/generic/base'
3
- require 'erb'
4
- require 'yaml'
5
-
6
- module DatabaseCleaner
7
- module ActiveRecord
8
- def self.available_strategies
9
- %w[truncation transaction deletion]
10
- end
11
-
12
- def self.default_strategy
13
- :transaction
14
- end
15
-
16
- def self.config_file_location=(path)
17
- @config_file_location = path
18
- end
19
-
20
- def self.config_file_location
21
- @config_file_location ||= begin
22
- # Has DC.app_root been set? Check in this intrusive way to avoid triggering deprecation warnings if it hasn't.
23
- app_root = DatabaseCleaner.send(:configuration).instance_variable_get(:@app_root)
24
- root = app_root || Dir.pwd
25
- "#{root}/config/database.yml"
26
- end
27
- end
28
-
29
- module Base
30
- include ::DatabaseCleaner::Generic::Base
31
-
32
- attr_accessor :connection_hash
33
-
34
- def db=(desired_db)
35
- @db = desired_db
36
- load_config
37
- end
38
-
39
- def db
40
- @db ||= super
41
- end
42
-
43
- def load_config
44
- if self.db != :default && self.db.is_a?(Symbol) && File.file?(ActiveRecord.config_file_location)
45
- connection_details = YAML::load(ERB.new(IO.read(ActiveRecord.config_file_location)).result)
46
- @connection_hash = valid_config(connection_details)[self.db.to_s]
47
- end
48
- end
49
-
50
- def valid_config(connection_file)
51
- if !::ActiveRecord::Base.configurations.nil? && !::ActiveRecord::Base.configurations.empty?
52
- if connection_file != ::ActiveRecord::Base.configurations
53
- return ::ActiveRecord::Base.configurations
54
- end
55
- end
56
- connection_file
57
- end
58
-
59
- def connection_class
60
- @connection_class ||= if db && !db.is_a?(Symbol)
61
- db
62
- elsif connection_hash
63
- lookup_from_connection_pool || establish_connection
64
- else
65
- ::ActiveRecord::Base
66
- end
67
- end
68
-
69
- def self.migration_table_name
70
- if ::ActiveRecord::VERSION::MAJOR < 5
71
- ::ActiveRecord::Migrator.schema_migrations_table_name
72
- else
73
- ::ActiveRecord::SchemaMigration.table_name
74
- end
75
- end
76
-
77
- def self.exclusion_condition(column_name)
78
- result = " #{column_name} <> '#{::DatabaseCleaner::ActiveRecord::Base.migration_table_name}' "
79
- if ::ActiveRecord::VERSION::MAJOR >= 5
80
- result += " AND #{column_name} <> '#{::ActiveRecord::Base.internal_metadata_table_name}' "
81
- end
82
- result
83
- end
84
-
85
- private
86
-
87
- def lookup_from_connection_pool
88
- if ::ActiveRecord::Base.respond_to?(:descendants)
89
- database_name = connection_hash["database"] || connection_hash[:database]
90
- models = ::ActiveRecord::Base.descendants
91
- models.select(&:connection_pool).detect { |m| m.connection_pool.spec.config[:database] == database_name }
92
- end
93
- end
94
-
95
- def establish_connection
96
- ::ActiveRecord::Base.establish_connection(connection_hash)
97
- ::ActiveRecord::Base
98
- end
99
- end
100
- end
101
- end
@@ -1,108 +0,0 @@
1
- require 'active_record'
2
- require 'active_record/connection_adapters/abstract_adapter'
3
- require "database_cleaner/generic/truncation"
4
- require 'database_cleaner/active_record/truncation'
5
-
6
- module DatabaseCleaner
7
- module ConnectionAdapters
8
- module AbstractDeleteAdapter
9
- def delete_table(table_name)
10
- raise NotImplementedError
11
- end
12
- end
13
-
14
- module GenericDeleteAdapter
15
- def delete_table(table_name)
16
- execute("DELETE FROM #{quote_table_name(table_name)};")
17
- end
18
- end
19
-
20
- module OracleDeleteAdapter
21
- def delete_table(table_name)
22
- execute("DELETE FROM #{quote_table_name(table_name)}")
23
- end
24
- end
25
- end
26
- end
27
-
28
- module ActiveRecord
29
- module ConnectionAdapters
30
- AbstractAdapter.class_eval { include DatabaseCleaner::ConnectionAdapters::AbstractDeleteAdapter }
31
-
32
- JdbcAdapter.class_eval { include ::DatabaseCleaner::ConnectionAdapters::GenericDeleteAdapter } if defined?(JdbcAdapter)
33
- AbstractMysqlAdapter.class_eval { include ::DatabaseCleaner::ConnectionAdapters::GenericDeleteAdapter } if defined?(AbstractMysqlAdapter)
34
- Mysql2Adapter.class_eval { include ::DatabaseCleaner::ConnectionAdapters::GenericDeleteAdapter } if defined?(Mysql2Adapter)
35
- SQLiteAdapter.class_eval { include ::DatabaseCleaner::ConnectionAdapters::GenericDeleteAdapter } if defined?(SQLiteAdapter)
36
- SQLite3Adapter.class_eval { include ::DatabaseCleaner::ConnectionAdapters::GenericDeleteAdapter } if defined?(SQLite3Adapter)
37
- PostgreSQLAdapter.class_eval { include ::DatabaseCleaner::ConnectionAdapters::GenericDeleteAdapter } if defined?(PostgreSQLAdapter)
38
- IBM_DBAdapter.class_eval { include ::DatabaseCleaner::ConnectionAdapters::GenericDeleteAdapter } if defined?(IBM_DBAdapter)
39
- SQLServerAdapter.class_eval { include ::DatabaseCleaner::ConnectionAdapters::GenericDeleteAdapter } if defined?(SQLServerAdapter)
40
- OracleEnhancedAdapter.class_eval { include ::DatabaseCleaner::ConnectionAdapters::OracleDeleteAdapter } if defined?(OracleEnhancedAdapter)
41
- end
42
- end
43
-
44
- module DatabaseCleaner::ActiveRecord
45
- module SelectiveTruncation
46
- def tables_to_truncate(connection)
47
- if information_schema_exists?(connection)
48
- (@only || tables_with_new_rows(connection)) - @tables_to_exclude
49
- else
50
- super
51
- end
52
- end
53
-
54
- def tables_with_new_rows(connection)
55
- stats = table_stats_query(connection)
56
- if stats != ''
57
- connection.select_values(stats)
58
- else
59
- []
60
- end
61
- end
62
-
63
- def table_stats_query(connection)
64
- @table_stats_query ||= build_table_stats_query(connection)
65
- ensure
66
- @table_stats_query = nil unless @cache_tables
67
- end
68
-
69
- def build_table_stats_query(connection)
70
- tables = connection.select_values(<<-SQL)
71
- SELECT table_name
72
- FROM information_schema.tables
73
- WHERE table_schema = database()
74
- AND #{::DatabaseCleaner::ActiveRecord::Base.exclusion_condition('table_name')};
75
- SQL
76
- queries = tables.map do |table|
77
- "(SELECT #{connection.quote(table)} FROM #{connection.quote_table_name(table)} LIMIT 1)"
78
- end
79
- queries.join(' UNION ALL ')
80
- end
81
-
82
- def information_schema_exists? connection
83
- return false unless connection.is_a? ActiveRecord::ConnectionAdapters::Mysql2Adapter
84
- @information_schema_exists ||=
85
- begin
86
- connection.execute("SELECT 1 FROM information_schema.tables")
87
- true
88
- rescue
89
- false
90
- end
91
- end
92
- end
93
-
94
- class Deletion < Truncation
95
- if defined?(ActiveRecord::ConnectionAdapters::Mysql2Adapter)
96
- include SelectiveTruncation
97
- end
98
-
99
- def clean
100
- connection = connection_class.connection
101
- connection.disable_referential_integrity do
102
- tables_to_truncate(connection).each do |table_name|
103
- connection.delete_table table_name
104
- end
105
- end
106
- end
107
- end
108
- end