Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add DatabaseCleaner::strategy method #518

Merged
merged 1 commit into from
Oct 19, 2021
Merged

Add DatabaseCleaner::strategy method #518

merged 1 commit into from
Oct 19, 2021

Conversation

rlue
Copy link
Contributor

@rlue rlue commented Jan 6, 2018

This PR adds a DatabaseCleaner::strategy getter method as discussed in issue #511.

Since there may be multiple connections, and different strategies for each connection, this getter method returns a hash of strategies.

The guard clause may be unnecessary, since the ::connections method already attempts to autodetect an ORM and populate the @connections array with it, but I figured better safe than sorry.

Copy link
Member

@etagwerker etagwerker left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rlue Thanks for your first contribution. It looks good!

Could you take a stab at adding coverage to this new method?

You could do that by adding another block to this spec: https://github.com/DatabaseCleaner/database_cleaner/blob/master/spec/database_cleaner/configuration_spec.rb

@rlue
Copy link
Contributor Author

rlue commented Jan 7, 2018

Whoops, I can't get the mysql gem to install on my machine. It's been more or less unmaintained for the past three years, and according to this SO answer, it's not compatible with Ruby > 2.3.

Any chance you'd be open to switching to mysql2 to enable development in more modern versions of Ruby?

@rlue
Copy link
Contributor Author

rlue commented Jan 8, 2018

Ayayay, never mind; I see that mysql2 is already a development dependency as well.

Are you able to work on this library in a currently-supported version of Ruby? I went ahead and installed Ruby 2.3.0, but now bundle install is borking on json v1.8.1:

An error occurred while installing json (1.8.1), and Bundler cannot continue.
Make sure that `gem install json -v '1.8.1'` succeeds before bundling.

In Gemfile:
  couch_potato was resolved to 1.3.0, which depends on
    couchrest was resolved to 1.2.0, which depends on
      rest-client was resolved to 1.6.8, which depends on
        rdoc was resolved to 4.1.2, which depends on
          json

and according to this SO answer, Ruby 2.2 broke json v1.8.1 (thus json >= 1.8.2 is required for Ruby >= 2.2).

I'm open to making any necessary modifications to the library to bring it up to date, but I'm reluctant to undertake the work without your blessing.

@botandrose
Copy link
Contributor

@rlue Things have finally improved here, with regards to the dependency issues you were having. If you try to implement this again on current master, you should have a much easier time.

@rlue
Copy link
Contributor Author

rlue commented Mar 2, 2020

Thanks guys! It's been a while since I've looked at this code; any thoughts on this proposal?

@rlue rlue requested a review from etagwerker March 2, 2020 08:33
@botandrose
Copy link
Contributor

@rlue Hello! Thank you for taking the time to rebase this and add some tests!

My personal thoughts are that its a bit awkward for this one getter method to return so many different types of values, but I think that's a natural consequence of the configuration API itself being so awkward. So my first inclination is to merge it.

However, there's another consideration: once the dust settles on the v2.0 release, I'd like to pursue a new, non-awkward configuration API (more details in #546), and I imagine that this work might result in us wanting to make breaking changes to this new #strategy method, and we would not be able to without releasing v3.0.

So I'm a bit torn. @etagwerker do you have any thoughts?

spec/database_cleaner/configuration_spec.rb Outdated Show resolved Hide resolved
@rlue
Copy link
Contributor Author

rlue commented Mar 4, 2020

I felt the same way, but tbh I couldn't figure out a better way, so I just submitted what I had. It does feel like a violation of the robustness principle (specifically, the first half—"be conservative in what you send").

If the intention is to modify it soon in the future, I can just put this PR on hold until the 2.0 release. That way, I won't be increasing the API surface that you guys are responsible for maintaining. (It's not like these changes are desperately important, anyway.)

@botandrose
Copy link
Contributor

@rlue okay, cool! I want to make improving the configuration API the next order of business after the v2.0 release is out the door, so if you're cool with postponing this API addition so that it can be a part of that endeavor, I think that could work out pretty well. Let's aim for v2.1!

Copy link
Member

@etagwerker etagwerker left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rlue Great idea! I agree with @botandrose here.

@@ -13,6 +13,19 @@ def [](orm, opts = {})
fetch([orm, opts]) { add_cleaner(orm, opts) }
end

def strategy
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rlue I think it would be great if you can document this method. It might be unexpected for users that the strategy method can return nil, a strategy object, or an array.

@etagwerker
Copy link
Member

@rlue Now that v2.0 is out, could you please look into this PR? I think it is a good idea but there are now some conflicts with the main branch.

Base automatically changed from master to main March 8, 2021 15:19
Issue #511 proposes the addition of a ::strategy getter
on the DatabaseCleaner module to aid new users with debugging.
This commit introduces that method.

Like the ::strategy= setter method, ::strategy is actually defined
as an instance method on DatabaseCleaner::Cleaners,
and is made available to the top-level module by delegation.

Depending on the complexity of the user's configuration,
this method may return three different kinds of values:

1. nil (if no strategies have been set on any cleaners);
2. a single strategy (if all cleaners share the same strategy); or
3. a hash of cleaner IDs and the strategies that go with them
   (if multiple strategies have been set on multiple cleaners).

The first of these three cases has not explicitly been tested.

=== Note

This commit also restores a spec context that was removed in c542fee:

    DatabaseCleaner::Cleaners
      top level api methods
        multiple cleaners
          multiple orm proxy methods
            with differing orms and dbs

This spec context included a single example testing the #orm= method,
which was removed in the aforementioned commit.
Now, a modified version of the same context is used
to test the #strategy method introduced here.
@rlue
Copy link
Contributor Author

rlue commented Jul 30, 2021

Hi folks! Sorry for the delay; hope there is still interest in this PR!

My personal thoughts are that its a bit awkward for this one getter method to return so many different types of values, but I think that's a natural consequence of the configuration API itself being so awkward. So my first inclination is to merge it.

I have not had the chance to really reflect on this concern, but I think it's valid. If we want to follow the robustness principle (that is, to be strict about/consistent in the output that we provide), then rather than supplying potentially three different kinds of output (nil, symbol, or hash), we could always return a hash; i.e., simply

def strategy
  strategies = transform_values(&:strategy)
end

I chose the approach I did based on the assumption that this method would be used primarily for debugging, and thus valued eronomics over consistency. Happy to make amendments as you see fit.

@etagwerker etagwerker merged commit 933df38 into DatabaseCleaner:main Oct 19, 2021
@etagwerker
Copy link
Member

@rlue Thanks for your input! I decided to go with what you said:

we could always return a hash

I just tweaked that and merged your changes. It should be released in v2.1.0 of database_cleaner. .cc @botandrose

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants