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

Trace import: remove activerecord-import gem #5038

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

mmd-osm
Copy link
Collaborator

@mmd-osm mmd-osm commented Aug 1, 2024

This PR removes the activerecord-import dependency and changes the tracepoint import to use import_all! instead.

I hope I didn't miss anything here. In particular Tracepoint.insert_all!(tracepoints.map(&:attributes)) would need a review if that's the proper way to import Tracepoint instances.

Fixes #4994

Copy link
Member

@tomhughes tomhughes left a comment

Choose a reason for hiding this comment

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

Overall the big difference between import! and insert_all! is that the former runs validations and the latter does not so maybe if we're going to change we should run validate! on each point after it is created?

Really the ideal way to use insert_all! would be not to build the models at all and just build a list of hashes but as it stands we need to run the save callbacks to fill in the tile number...

.rubocop.yml Outdated
@@ -71,6 +71,7 @@ Rails/SkipsModelValidations:
Exclude:
- 'db/migrate/*.rb'
- 'app/controllers/users_controller.rb'
- 'app/models/trace.rb'
Copy link
Member

Choose a reason for hiding this comment

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

Why do we need to exclude this?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Rubocop was complaining about the use of insert_all! because it's bypassing validations.

app/models/trace.rb Outdated Show resolved Hide resolved
@tomhughes
Copy link
Member

The main reason I haven't merged this is that I can't make up my mind if I like it... I understand that in theory moving to the built-in version is good but then it turned out they're not equivalent really so we have to jump through hoops and disable cops and it just starts to smell a little.

@mmd-osm
Copy link
Collaborator Author

mmd-osm commented Sep 12, 2024

Well, we could disable rubocop's skip model validation check for that single line only, in case this would be an improvement.

 Tracepoint.insert_all!(tracepoints.map(&:attributes))   # rubocop:disable Rails::SkipsModelValidations

@AntonKhorev
Copy link
Collaborator

Do we even need to build Tracepoint models? The only nontrivial thing there is tile calculation.

@mmd-osm
Copy link
Collaborator Author

mmd-osm commented Sep 24, 2024

The question is what happens with validations. Do we want to maintain them in two different places (Tracepoint model and trace.rb)?

@AntonKhorev
Copy link
Collaborator

Tracepoint model doesn't need validations because nothing is written using that model.

@mmd-osm
Copy link
Collaborator Author

mmd-osm commented Sep 25, 2024

Tracepoint.import! calls model validations internally, like we discussed above. Try to upload data without timestamp and see what happens.

@AntonKhorev
Copy link
Collaborator

Don't insert data without timestamps.

delete_all is already present in trace.rb. It doesn't call any hooks yet nobody is bothered by it. Might as well have insert_all and do all validations in trace.rb.

@mmd-osm
Copy link
Collaborator Author

mmd-osm commented Sep 26, 2024

My goal was to replace the activerecord-import gem by a built in equivalent method. Since the validations are not automatically executed anymore, I added the call to the validate method. Rubocop is not clever enough to figure out that we're validating the model contents earlier on, so we need an additional annotation. I really don't have an issue with that.

Now we could of course further change the implementation and get rid of the model instances. However, that's a second step, and not needed to replace activerecord-import. That's not to say that this isn't a valid discussion to have, it just doesn't fit well here.

@AntonKhorev
Copy link
Collaborator

This PR wasn't merged because

we have to jump through hoops and disable cops

and I'm saying that we shouldn't worry here about model validations and what Rubocop thinks about them.

@mmd-osm
Copy link
Collaborator Author

mmd-osm commented Sep 27, 2024

This comment still refers to the previous version where the whole file was added to .rubocop.yml.

@tomhughes hasn’t commented on the inline annotation variant yet, and I would suggest to wait for feedback before continuing.

Like I said, I don’t really have an issue with this annotation. Rubocop is reporting a false positive here and we mark it as such. There's no need to come up with workarounds simply to please the tool.

@AntonKhorev
Copy link
Collaborator

Rubocop is reporting a false positive here and we mark it as such.

Any use of insert_all etc that you want to keep is a false positive. You'll have to disable Rubocop for it one way or another.

@mmd-osm
Copy link
Collaborator Author

mmd-osm commented Sep 27, 2024

A few years back, I've introduced both the activerecord-import gem and import! method call to speed up Tracepoint imports. Alternative options weren't available then. Now with the recent Rails version, it's a good time to move to the core Rails code.

At least today, we have exactly one location where insert_all will be used. Nobody else was doing anything with that gem over the past few years. The only other attempt to use activerecord-import was a performance improved changeset upload implementation which never got merged. So it's somewhat unlikely that we will see this issue in many other places in the future.

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.

Retire activerecord-import gem?
3 participants