rgeo / activerecord-mysql2spatial-adapter

ActiveRecord connection adapter for MySQL Spatial Extensions, based on mysql2 and rgeo
48 stars 64 forks source link

Undefined method spatial for ActiveRecord::ConnectionAdapters::TableDefinition #14

Closed januszm closed 9 years ago

januszm commented 9 years ago

Related to https://github.com/rgeo/activerecord-mysql2spatial-adapter/issues/9 ? @ronna-s

I see that this gem has a new maintainer and there are updates to make it working with ActiveRecord 4.2, great! (Thanks @benatkin and thanks @fjl82 for your recent updates).

It mostly works (can read data from db properly) but I can't run rake db:test:prepare because it fails on table creation. In fact, what fails is the rake db:schema:load task, it throws the error:

NoMethodError: undefined method `spatial' for #<ActiveRecord::ConnectionAdapters::TableDefinition:0x007f9f576a79f0>

this is what I have in db/schema.rb (just an example, auto generated with rake db:schema:dump)

t.spatial  "coordinates", limit: {:type=>"point"}, null: false

I tried with both:

I still get the NoMethodError: undefined method spatial though. I'd be happy to contribute to this project but I'd need some guidance from you. I've never worked on extending spatial adapters. I took a quick look at the activerecord-postgis-adapter , which is a 'postgresql' alternative for this one. It looks good, it dumps proper table information to the schema file (like t.point 'coordinates').

januszm commented 9 years ago

It seems that it is possible to load the database schema after applying this fix: http://www.rdlt.com/nomethoderror-undefined-method-spatial-using-rgeo-and-activerecord-mysql2spatial-adapter.html

basically it's about changing all occurrences of t.spatial to t.column and providing proper spatial 'type' as argument, eg.

- t.spatial  "coordinates", limit: {:type=>"point"}, null: false
+ t.column  "coordinates", :point, null: false

It's just a workaround but a permanent fix would be to make the schema dump working properly.

januszm commented 9 years ago

I've just updated this issue, I can confirm that this adapter also doesn't work properly with Rails 4.2 and rgeo-activerecord 2.x. I tried @fjl82 's fork .

januszm commented 9 years ago

I think I've found a solution to this issue, it's borrowed from activerecord-postgis-adapter .

In mysql2spatial-adapter.rb file

require 'active_record/connection_adapters/mysql2spatial_adapter/column_methods.rb'

And create required module (I've nested [:type] under [:limit] but this should be handled in a different way I think):

    module ActiveRecord
      module ConnectionAdapters
        module Mysql2SpatialAdapter
          module ColumnMethods
            def spatial(name, options = {})
              raise "You must set a type. For example: 't.spatial type: :point'" unless options[:limit][:type]
              column(name, options[:limit][:type], options)
            end
            # ... other shortcuts, like def point or def polygon ...
          end
          ConnectionAdapters::TableDefinition.send(:include, ColumnMethods)
        end
      end
januszm commented 9 years ago

See https://github.com/rgeo/activerecord-mysql2spatial-adapter/pull/15

januszm commented 9 years ago

Resolved