Module: AlsoMigrate::Migration::ClassMethods

Defined in:
lib/also_migrate/migration.rb

Instance Method Summary collapse

Instance Method Details

#method_missing_with_also_migrate(method, *arguments, &block) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/also_migrate/migration.rb', line 18

def method_missing_with_also_migrate(method, *arguments, &block)
  args = Marshal.load(Marshal.dump(arguments))
  return_value = method_missing_without_also_migrate(method, *arguments, &block)

  supported = [
    :add_column, :add_index, :add_timestamps, :change_column,
    :change_column_default, :change_table, :create_table,
    :drop_table, :remove_column, :remove_columns,
    :remove_timestamps, :rename_column, :rename_table
  ]

  if !args.empty? && supported.include?(method)
    connection = ActiveRecord::Base.connection
    table_name = ActiveRecord::Migrator.proper_table_name(args[0])
    
    # Find models
    (::AlsoMigrate.configuration || []).each do |config|
      next unless config[:source].to_s == table_name
  
      # Don't change ignored columns
      [ config[:ignore] ].flatten.compact.each do |column|
        next if args.include?(column) || args.include?(column.intern)
      end

      # Run migration
      if method == :create_table
        ActiveRecord::Migrator::AlsoMigrate.create_tables(config)
      elsif method == :add_index && !config[:indexes].nil?
        next
      else
        [ config[:destination] ].flatten.compact.each do |table|
          if connection.table_exists?(table)
            args[0] = table
            begin
              connection.send(method, *args, &block)
            rescue Exception => e
              puts "(also_migrate warning) #{e.message}"
            end
          end
        end
      end
    end
  end
  
  return return_value
end