5

I have a few migrations within identical helpers

  private

  def add_earthdistance_index table_name, options = {}
    execute "CREATE INDEX %s_earthdistance_ix ON %s USING gist (ll_to_earth(%s, %s));" %
      [table_name, table_name, 'latitude', 'longitude']
  end

  def remove_earthdistance_index table_name
    execute "DROP INDEX %s_earthdistance_ix;" % [table_name]
  end

And I'm trying to avoid copy-paste them every time. Is there any way to share code between migrations without monkey-patching the base class? I want to find something like concerns for models.

Timur Shtatland
  • 12,024
  • 2
  • 30
  • 47
Arsen
  • 10,815
  • 2
  • 34
  • 46

2 Answers2

8

Solution

Add config.autoload_paths += Dir["#{config.root}/db/migrate/concerns/**/"] to config/application.rb

Create db/migrate/concerns/earthdistanceable.rb file within

module Earthdistanceable
  extend ActiveSupport::Concern

  def add_earthdistance_index table_name, options = {}
    execute "CREATE INDEX %s_earthdistance_ix ON %s USING gist (ll_to_earth(%s, %s));" %
      [table_name, table_name, 'latitude', 'longitude']
  end

  def remove_earthdistance_index table_name
    execute "DROP INDEX %s_earthdistance_ix;" % [table_name]
  end

end

Use it:

class CreateRequests < ActiveRecord::Migration[5.0]
  include Earthdistanceable

  def up
    ...
    add_earthdistance_index :requests
  end

  def down
    remove_earthdistance_index :requests

    drop_table :requests
  end

end
Arsen
  • 10,815
  • 2
  • 34
  • 46
4

I think you could do something like:

# lib/helper.rb
module Helper
  def always_used_on_migrations
    'this helps' 
  end
end

Migration

include Helper
class DoStuff < ActiveRecord::Migration
  def self.up
    p always_used_on_migrations
  end

  def self.down
    p always_used_on_migrations
  end
end
fabriciofreitag
  • 2,843
  • 22
  • 26