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 the newer serialize signature of Rails 7.1 if available #776

Merged
merged 1 commit into from
Feb 27, 2023

Conversation

casperisfine
Copy link

Ref: rails/rails#47463

On 7.1 serialize properly split coder and type arguments instead of taking a single positional argument and try to figure out which is which.

While we're at it, we can explictly define we're using YAML here instead of relying on the default that is now configurable.

Copy link
Member

@etiennebarrie etiennebarrie left a comment

Choose a reason for hiding this comment

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

I'm going to raise the issue in Rails to see if it's the best place to put that config.

@@ -46,8 +46,13 @@ class Run < ApplicationRecord

attr_readonly :task_name

serialize :backtrace
serialize :arguments, JSON
if respond_to?(:default_column_serializer) # Rails 7.1+
Copy link
Member

Choose a reason for hiding this comment

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

I'm surprised this is defined on ActiveRecord::Base, I thought configurations were being moved to ActiveRecord. rails/rails#42445
It's not a mattr_accessor, but still confusing and if it's kept at the ActiveRecord::Base level, we could also just set the value, as it wouldn't be global anyway.

self.default_column_serializer = YAML if respond_to?(:default_column_serializer=)

Also the accessor on ActiveRecord::Base is not documented, only as a configuration on config.active_record.default_column_serializer, but these never explicitly say where they're being copied.

serialize :arguments, JSON
if respond_to?(:default_column_serializer) # Rails 7.1+
serialize :backtrace, coder: YAML
serialize :arguments, coder: JSON
Copy link
Member

Choose a reason for hiding this comment

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

Maybe if we're calling the method differently based on its parameters, we might as well use that as to make the distinction:

if method(:serialize).parameters.include?([:key, :coder])

or even just call it and rescue ArgumentError.

Or maybe just a Active Record version check?

Copy link
Author

Choose a reason for hiding this comment

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

I'm fine with either. Let me know which you prefer.

Copy link
Member

Choose a reason for hiding this comment

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

Version check is the safest and the easiest to grep for down the line when we want to remove that?

Copy link
Contributor

Choose a reason for hiding this comment

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

Version check sounds good to me as well 👍

Copy link
Contributor

@adrianna-chang-shopify adrianna-chang-shopify left a comment

Choose a reason for hiding this comment

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

I'm also curious about having this defined as a class attribute on on ActiveRecord::Base vs an attr on ActiveRecord's singleton 😄

Otherwise switching to the version check SGTM.

serialize :arguments, JSON
if respond_to?(:default_column_serializer) # Rails 7.1+
serialize :backtrace, coder: YAML
serialize :arguments, coder: JSON
Copy link
Contributor

Choose a reason for hiding this comment

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

Version check sounds good to me as well 👍

@@ -46,8 +46,13 @@ class Run < ApplicationRecord

attr_readonly :task_name

serialize :backtrace
serialize :arguments, JSON
if Rails.gem_version >= Gem::Version.new("7.1")
Copy link
Member

Choose a reason for hiding this comment

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

We need to check against alpha to also include the current main.

Suggested change
if Rails.gem_version >= Gem::Version.new("7.1")
if Rails.gem_version >= Gem::Version.new("7.1.alpha")

Copy link
Author

Choose a reason for hiding this comment

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

Duh, I always forget that.

On 7.1 serialize properly split `coder` and `type` arguments
instead of taking a single positional argument and try to figure
out which is which.

While we're at it, we can explictly define we're using YAML here
instead of relying on the default that is now configurable.
@etiennebarrie etiennebarrie changed the title Use the newer serialize signature if available Use the newer serialize signature of Rails 7.1 if available Feb 27, 2023
@etiennebarrie etiennebarrie merged commit c4d8a8a into main Feb 27, 2023
@etiennebarrie etiennebarrie deleted the default-serializers branch February 27, 2023 15:44
@shopify-shipit shopify-shipit bot temporarily deployed to rubygems February 27, 2023 15:53 Inactive
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.

4 participants