Skip to content

Rails 7 support#33

Merged
noelrappin merged 7 commits intomainfrom
rails_7_support
Jan 31, 2024
Merged

Rails 7 support#33
noelrappin merged 7 commits intomainfrom
rails_7_support

Conversation

@noelrappin
Copy link
Copy Markdown
Collaborator

@noelrappin noelrappin commented Jan 25, 2024

Creates a new version just to support Rails 7.

In Rails 7, the type maps are created as class methods, not instance methods, and for our purposes that was a problem because it meant (I think) that the maps were loaded before our monkey patch was added.

In this version (based on Invoca/activerecord-mysql-enum#31), we explicitly call the type map and register the enum type.

This PR incorporates a couple of minor code cleanups from previous PRs that I closed rather than add them to the older version. (#32 from @tjwallace and #16 from @dpep)


module ActiveRecord
module ConnectionAdapters
class AbstractMysqlAdapter
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

I think this is reopening the original adapter, is that right? Is that because there are effectively no hooks we could use? Or would this potentially be a good time to move to a proper initializer that hooks the adapter at that point?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

I wonder if maybe the thing to do is keep the register_enum_type method here (there are other, similar methods in the adapter already) and move the call of it to a Railtie or something

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

My inclination is to get this PR in and then try to mess with that separately?

class << self
def register_enum_type(mapping)
mapping.register_type(%r(enum)i) do |sql_type|
Type::Enum.new(limit: sql_type.scan(/'(.*?)'/).flatten)
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Do you understand this limit code? I know it's copy/pasted, but the limit is quite opaque to me.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

The third party tool does this: limit = sql_type.sub(/^enum\('(.+)'\)/i, '\1').split("','").map { |v| v.to_sym } which is actually different (our code ends up with strings, the other goes into symbols)

I think the sql_type comes in here as a string formatted like enum('low', 'normal', 'high') and this is pulling the enum values into a Ruby array. I'm not 100% sure why you need to do that, but I think it's what makes the ActiveRecord conversion work.

Comment thread sql_enum.gemspec
Comment thread spec/sql_enum_spec.rb
priority: [:enum, limit: priorities]) do
sql_enum :status
sql_enum :priority, _prefix: false, _suffix: true
sql_enum :status # duplicate should be no-op
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Is there a way to directly test that this is in fact a no-op? I'm worried about the possibility of someone re-declaring using different options, then not understanding why their declaration doesn't work.

In fact, should this raise?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Open to suggestions, this was from @dpep's PR, so maybe he wants to opine...

@noelrappin noelrappin requested a review from dpep January 25, 2024 17:47
@noelrappin noelrappin merged commit 89ef76e into main Jan 31, 2024
@noelrappin noelrappin deleted the rails_7_support branch January 31, 2024 20:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

4 participants