Sha256: 415d3facabbdb3ba8b9caf9182eb68396e22f53dce1fd712adc72ab31b354a26

Contents?: true

Size: 2 KB

Versions: 29

Compression:

Stored size: 2 KB

Contents

class AddEarthdistance < ActiveRecord::Migration
  def self.up
    
    # comented out to avoid PG::InsufficientPrivilege-ERROR
    # add with cap postgresql:add_extensions
    # => execute "CREATE EXTENSION IF NOT EXISTS cube"
    # => execute "CREATE EXTENSION IF NOT EXISTS earthdistance"
    
    if Rails.env.development? || Rails.env.test?
      enable_extension "cube"
      enable_extension "earthdistance"
    end
    
    ## ActiveRecord indexes .. doesnt include earthdistance, because its build directly in postgres!
    # => address_indexes = ActiveRecord::Base.connection.indexes(:mgca_addresses).map{ |x| x.name }
    # => index_exists?(:mgca_addresses, :earthdistance, name: "mgca_addresses_earthdistance_ix")
    
    ## Postgres-Indexes .. thanks to: http://www.alberton.info/postgresql_meta_info.html#p13
    address_indexes = execute( "SELECT c.relname AS index_name FROM pg_class AS a JOIN pg_index AS b ON (a.oid = b.indrelid) JOIN pg_class AS c ON (c.oid = b.indexrelid) WHERE a.relname = 'mgca_addresses';").map{ |that| that["index_name"] }
    
    unless address_indexes.include?("mgca_addresses_earthdistance_ix")
      add_earthdistance_index :mgca_addresses, lat: 'latitude', lng: 'longitude'
    end
    
  end

  def self.down
    # => execute "DROP EXTENSION IF EXISTS earthdistance"
    # => execute "DROP EXTENSION IF EXISTS cube"
    # comented out in staging and production to avoid PG::InsufficientPrivilege-ERROR .. add with cap postgresql:remove_extensions
    if Rails.env.development? || Rails.env.test?
      disable_extension "earthdistance"
      disable_extension "cube"
    end
    
    ## Postgres-Indexes
    address_indexes = execute( "SELECT c.relname AS index_name FROM pg_class AS a JOIN pg_index AS b ON (a.oid = b.indrelid) JOIN pg_class AS c ON (c.oid = b.indexrelid) WHERE a.relname = 'mgca_addresses';").map{ |that| that["index_name"] }
    
    if address_indexes.include?("mgca_addresses_earthdistance_ix")
      remove_earthdistance_index :mgca_addresses
    end
    
    
  end
end

Version data entries

29 entries across 29 versions & 1 rubygems

Version Path
magic_addresses-0.0.30 lib/generators/magic_addresses/templates/earthdistance_migration.rb
magic_addresses-0.0.29 lib/generators/magic_addresses/templates/earthdistance_migration.rb
magic_addresses-0.0.28 lib/generators/magic_addresses/templates/earthdistance_migration.rb
magic_addresses-0.0.27 lib/generators/magic_addresses/templates/earthdistance_migration.rb
magic_addresses-0.0.26 lib/generators/magic_addresses/templates/earthdistance_migration.rb
magic_addresses-0.0.25 lib/generators/magic_addresses/templates/earthdistance_migration.rb
magic_addresses-0.0.24 lib/generators/magic_addresses/templates/earthdistance_migration.rb
magic_addresses-0.0.23 lib/generators/magic_addresses/templates/earthdistance_migration.rb
magic_addresses-0.0.22 lib/generators/magic_addresses/templates/earthdistance_migration.rb