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

Use model class DB connection in have_db_index matcher #1200

Merged
merged 3 commits into from
May 24, 2019
Merged

Use model class DB connection in have_db_index matcher #1200

merged 3 commits into from
May 24, 2019

Conversation

jooeycheng
Copy link
Contributor

@jooeycheng jooeycheng commented Apr 13, 2019

Fix #1051. This PR is WIP.

Hey @mcmire, lemme know what you think.

I'm thinking to rename ProductionRecord to something else, as this naming might cause confusion.

@@ -3,7 +3,8 @@
describe Shoulda::Matchers::ActiveRecord::HaveDbIndexMatcher, type: :model do
context 'have_db_index' do
it 'accepts an existing index' do
expect(with_index_on(:age)).to have_db_index(:age)
expect(with_index_on(:age1)).to have_db_index(:age1)
expect(with_index_on(:age2, parent_class: ProductionRecord)).to have_db_index(:age2)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Hey sorry it took me so long to get back to you. Very nice solution overall to this. I think the only thing I'd say here is that it might be beneficial to be a little more explicit in this particular case. It might be lost on the reader that ActiveRecord::Base is being used by default for the first assertion here and that it defaults to using the development database. Perhaps for symmetry's sake it would be better to also create a DevelopmentRecord (which would essentially do nothing) and then be explicit about that here:

expect(with_index_on(:age1, parent_class: DevelopmentRecord)).to have_db_index(:age1)
expect(with_index_on(:age2, parent_class: ProductionRecord)).to have_db_index(:age2)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That makes sense. I have updated accordingly.

@mcmire mcmire added this to the v4.1.0 milestone Apr 25, 2019
* In current database.yml config, all database environments are sharing same database. Suffix database name with relevant environment name to ensure database uniqueness
* Update Rails migration command to `db:drop:all` and `db:create:all`, so all db listed in config/database.yml is dropped/created accordingly
* Write a base ActiveRecord model that connects to different database (production database)
* Write a base ActiveRecord model that connects to default database (development database), this is a dummy model, just for symmetry's sake
* create_table and define_model_class accepts optional `connection' parameter for custom db connections
* clear_column_caches and drop_created_tables for ProductionRecord
@jooeycheng
Copy link
Contributor Author

There're still some failing specs. I'll look into those over the weekend.

@mcmire
Copy link
Collaborator

mcmire commented Apr 26, 2019

@jooeycheng I looked into this a bit for you. I think the problem is that both of the models you're creating in that test have the same class name. The method that creates classes will (unhelpfully, now, I realize) detect whether the class it's creating already exists and remove it before adding its version. After the tests are run, it will then make sure that class is removed. So the second model is overwriting the first model, however, when it goes to remove that class after the test is finished running, it gets very confused. If you give with_index_on the ability to specify a class name and then pass that down, I think it should work a lot better.

@jooeycheng
Copy link
Contributor Author

jooeycheng commented May 2, 2019

@mcmire Ahh, that makes sense. I have updated with_index_on to accept table_name instead of model_name, bcos i found that converting from snake_case to PascalCase is easier than PascalCase to snake_case.

I just noticed you pushed this commit e716a5b. I think it's structured better, with clearer naming. I have updated to follow it.

@mcmire
Copy link
Collaborator

mcmire commented May 21, 2019

@jooeycheng Oops, I noticed you updated this just now. Skimming over it again I think it looks good, but I'll take a closer look when I get a chance.

@mcmire mcmire merged commit ab69fab into thoughtbot:master May 24, 2019
@mcmire
Copy link
Collaborator

mcmire commented May 24, 2019

Thanks so much for this!

@jooeycheng
Copy link
Contributor Author

@mcmire Thanks for taking the time to guide me through it 🙆‍♂️. Your comment was super helpful, and I've really learnt a lot from reading the codebase. I'm impressed that it creates a Rails application on-the-fly in test.

@mcmire
Copy link
Collaborator

mcmire commented May 24, 2019

@jooeycheng Yeah there's definitely some complex parts to the gem that not many people know about but I'm glad I was able to help you navigate them. If you find something else let me know and I will happily take another PR of yours :)

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