database_cleaner icon indicating copy to clipboard operation
database_cleaner copied to clipboard

Getter method for strategy (to help with debugging)

Open rlue opened this issue 7 years ago • 4 comments

It appears that DatabaseCleaner does not currently support a getter method for determining which strategy it currently employs:

>> DatabaseCleaner.strategy
NoMethodError: undefined method `strategy' for DatabaseCleaner:Module
Did you mean?  strategy=
from (pry):1:in `block (2 levels) in <top (required)>'

It would be nice to have this just so I could confirm, in my debugger, that I have DatabaseCleaner set up the way I had intended.

Is there another way to do this that I've missed?

rlue avatar Nov 16 '17 13:11 rlue

@rlue I think it's a good idea but it's not a trivial change because of this:

https://github.com/DatabaseCleaner/database_cleaner/blob/f052d64d3be9351a98a99e932a59be528ccc561c/lib/database_cleaner/configuration.rb#L64-L67

If your application is using more than one connection, then that could return an Array (or a Hash?)

Are you interested in working on a PR for this?

etagwerker avatar Nov 22 '17 03:11 etagwerker

I am, but it's going to be a while before I have time to familiarize myself with the codebase enough to know what the implications of my changes will be.

I'm not sure I understand — the ::strategy= method appears to unconditionally set the strategy on all connections. Is there another way that an individual connection's strategy might be changed? Otherwise, wouldn't we expect all connections to share the same strategy?

rlue avatar Nov 22 '17 04:11 rlue

@etagwerker I finally got around to taking a look at the code. Here's my understanding — please correct me if I'm mistaken:

  • Each “connection” is an element of the @connections instance variable on the DatabaseCleaner module, and
  • These connections are instances of the DatabaseCleaner::Base class.

What would you think of adding the #strategy getter method on the DatabaseCleaner::Base class? That way, DatabaseCleaner[].strategy would return the strategy for the default connection, and DatabaseCleaner[:mongoid].strategy would return the strategy for that one. Then, for some syntactic sugar, we could add a DatabaseCleaner.strategy method that aliases DatabaseCleaner[].strategy.

On the other hand, judging from the comments, it looks like you plan on doing a deeper redesign of the class hierarchy database_cleaner. Thoughts?

rlue avatar Jan 02 '18 05:01 rlue

Sorry, disregard my last comment. 1) I hadn't noticed that DatabaseCleaner::Base#strategy is already defined, and 2) I misunderstood how DatabaseCleaner::[] works.

I'm submitting a PR according to your previous suggestion.

rlue avatar Jan 06 '18 14:01 rlue