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 for mysql schema with spatial types/indexes #27813

Closed
wants to merge 6 commits into from

Conversation

Vasfed
Copy link
Contributor

@Vasfed Vasfed commented Jan 26, 2017

Summary

This allows one to have spatial fields and indexes in database without switching to sql schema format.

@rails-bot
Copy link

Thanks for the pull request, and welcome! The Rails team is excited to review your changes, and you should hear from @kaspth (or someone else) soon.

If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes.

This repository is being automatically checked for code quality issues using Code Climate. You can see results for this analysis in the PR status below. Newly introduced issues should be fixed before a Pull Request is considered ready to review.

Please see the contribution instructions for more information.

@jeremy
Copy link
Member

jeremy commented Jan 26, 2017

Cool!

@arthurnn
Copy link
Member

When was this added to MySQL?
Should we check the MySQL version before allowing these?
Is this something other adpters have support too?

Copy link
Member

@rafaelfranca rafaelfranca left a comment

Choose a reason for hiding this comment

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

Could you add a CHANGELOG entry?

@arthurnn I don't think we need to check the MySQL version. People needs to opt-in this feature and If people use a version that doesn't support it they will see when running the migration.

@Vasfed
Copy link
Contributor Author

Vasfed commented Feb 1, 2017

@arthurnn mysql added spatial extensions in 4.1, and as of 5.7.5 spatial indexing for InnoDB was added. Since we are supporting Mysql>=5.1.10 no additional checks are needed, except for index addition, but in this case one will get an error on migration.
Also Rails already has support for :spatial index type, but these were dumped incorrectly because spatial indexes cannot have prefixes, but report them (this PR fixes this).

@Vasfed Vasfed force-pushed the feature/support_mysql_spatial branch from 687fd5a to 6e05b69 Compare February 2, 2017 10:41
@Vasfed
Copy link
Contributor Author

Vasfed commented Feb 2, 2017

@rafaelfranca added changelog entry + rebased onto current master to resolve conflicts


def multi_polygon(*args, **options)
args.each { |name| column(name, :multi_polygon, options) }
end
Copy link
Contributor

@kaspth kaspth Feb 2, 2017

Choose a reason for hiding this comment

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

Don't think it makes sense exposing those multi columns both via an option and a method, I'd go with the option across the board:

def point(*args, multi: false, **options)
  type = multi ? :multi_point : :point
  args.each { |name| column(name, type, options) }
end

Then the same for the other versions.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@kaspth I agree. Pushed changes

@Vasfed Vasfed force-pushed the feature/support_mysql_spatial branch from 40f7e6d to e45bca8 Compare February 2, 2017 20:15
@@ -396,7 +404,7 @@ def indexes(table_name, name = nil) #:nodoc:
end

indexes.last.columns << row[:Column_name]
indexes.last.lengths.merge!(row[:Column_name] => row[:Sub_part].to_i) if row[:Sub_part]
indexes.last.lengths.merge!(row[:Column_name] => row[:Sub_part].to_i) if row[:Sub_part] && mysql_index_type != :spatial
Copy link
Contributor

Choose a reason for hiding this comment

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

@jeremy can you vet this change?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Mysql returns non-null Sub_part for spatial index, but does not allow to set it, thus previous behaviour resulted in a schema dump that could not be loaded back.

https://dev.mysql.com/doc/refman/5.7/en/create-index.html :

Spatial indexes (created using SPATIAL INDEX) have these characteristics:
...

  • Column prefix lengths are prohibited. The full width of each column is indexed.

@kaspth
Copy link
Contributor

kaspth commented Feb 2, 2017

Think this is the limit of what I can meaningfully contribute. Handing it over, thanks @Vasfed!

r? @jeremy

@rails-bot rails-bot assigned jeremy and unassigned kaspth Feb 2, 2017
@jeremy
Copy link
Member

jeremy commented Feb 2, 2017

How does this integrate with https://github.com/rgeo/rgeo or https://github.com/geokit/geokit-rails ?

I notice that other spatial adapters use distinct "multi" types rather than making it an option on the singular type, e.g. multi_polygon :regions instead of polygon :regions, multi: true. What's your feeling on the style difference?

@jeremy jeremy added this to the 5.1.0 milestone Feb 2, 2017
@Vasfed
Copy link
Contributor Author

Vasfed commented Feb 2, 2017

@jeremy postgresql adapter provides similar types by adding array: true modifier to a spatial column. While whole thing is completely db-dependent I think less of mysql implementation details can be leaked into interface.

geokit-rails does not use native spatial extensions, only mentions mysql2spatial adapter from rgeo as a synonym for regular mysql adapter for using mysql's math functions.

Will look closer into implementation of rgeo adapter, it's master branch is incompatible with AR 5 ( rgeo/activerecord-mysql2spatial-adapter#19 ) at the moment. I doubt that all heavy-lifting of parsing binary WKB geo data should be brought into rails, so better is to provide an integration point. First thing coming to mind is monkey-patching MysqlGeometry class from rgeo, what is current policy on this?

stadia pushed a commit to stadia/activerecord-mysql2rgeo-adapter that referenced this pull request Feb 6, 2017
@rafaelfranca rafaelfranca removed this from the 5.1.0 milestone Feb 23, 2017
@sikachu
Copy link
Member

sikachu commented May 16, 2018

Looks like this PR has been opened for more than a year now. Do you think it's worth rebasing this and pushing it forward to be included in 6.0?

@rails-bot
Copy link

rails-bot bot commented Dec 18, 2019

This pull request has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs.
Thank you for your contributions.

@rails-bot rails-bot bot added the stale label Dec 18, 2019
@rails-bot rails-bot bot closed this Dec 25, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants