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

Support ActiveRecord 7.0 #14

Merged
merged 3 commits into from
Jun 17, 2022
Merged

Support ActiveRecord 7.0 #14

merged 3 commits into from
Jun 17, 2022

Conversation

osanay
Copy link
Contributor

@osanay osanay commented May 27, 2022

In ActiveRecord 7, the initialize_type_map method has been moved to class method by rails/rails#42773 and armg no longer works correctly.

Therefore, I have added a monkey patch to the type_map method. Because ActiveRecord 7 defines the type map statically when loaded, monkey patching class method does not make sense.

https://github.com/rails/rails/blob/v7.0.3/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb#L616-L619

The type_map method is defined in the base class ActiveRecord::ConnectionAdapters::AbstractAdapter. It is the same for all versions of ActiveRecord supported by armg. And this fix works in any version.

In ActiveRecord 7, the `initialize_type_map` method has been moved to
class method by rails/rails#42773 and armg no
longer works correctly.

Therefore, I have added a monkey patch to the `type_map` method.
Because ActiveRecord 7 defines the type map statically when loaded,
monkey patching class method does not make sense.

https://github.com/rails/rails/blob/v7.0.3/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb#L616-L619

The `type_map` method is defined in the base class
`ActiveRecord::ConnectionAdapters::AbstractAdapter`. It is the same for
all versions of ActiveRecord supported by armg. And this fix works in
any version.
Copy link
Member

@s-osa s-osa left a comment

Choose a reason for hiding this comment

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

In old Rails (< 7.0), it seems like the geometry type is registered twice. The first time is when the old initialize_type_map has been called, and the second time is when the new type_map has been called. Is that right?

I think this behavior does not occur any problems because ActiveRecord::Type::TypeMap#register_type is idempotent.
https://github.com/rails/rails/blob/6-0-stable/activerecord/lib/active_record/type/type_map.rb#L32

But the monkey-patched initialized_type_map is no longer necessary and should be removed.

@@ -28,7 +28,7 @@ def reset
def dump
buf = StringIO.new
ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, buf)
buf = buf.string.sub(/\A.*\bActiveRecord::Schema\.define\(version: \d+\) do/m, '').sub(/end\s*\z/, '')
buf = buf.string.sub(/\A.*\bActiveRecord::Schema(?:\[[\d.]+?\])?\.define\(version: \d+\) do/m, '').sub(/end\s*\z/, '')
Copy link
Member

Choose a reason for hiding this comment

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

Can we write this regular expression strict and simpler?

  • Replace . (any character) with \. (dot itself)
  • Remove the first? because the second ? matches the case of [7.0] missing
Suggested change
buf = buf.string.sub(/\A.*\bActiveRecord::Schema(?:\[[\d.]+?\])?\.define\(version: \d+\) do/m, '').sub(/end\s*\z/, '')
buf = buf.string.sub(/\A.*\bActiveRecord::Schema(?:\[[\d\.]+\])?\.define\(version: \d+\) do/m, '').sub(/end\s*\z/, '')

Copy link
Contributor Author

Choose a reason for hiding this comment

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

  • Replace . (any character) with \. (dot itself)

Escaping the dot is redundant, and this will match only the dot as expected.

  • Remove the first? because the second ? matches the case of [7.0] missing

Added a commit to remove it.

No longer necessary because geometry type is registered in monkey-patched
`type_map` method.
Because it is unnecessary.
@osanay
Copy link
Contributor Author

osanay commented Jun 17, 2022

But the monkey-patched initialized_type_map is no longer necessary and should be removed.

You are right.
I've added a commit to remove initialized_type_map method as it is no longer necessary.

@s-osa s-osa merged commit 893e7f2 into cookpad:master Jun 17, 2022
@s-osa s-osa mentioned this pull request Jun 17, 2022
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.

2 participants