Module: SchemaPlus::ActiveRecord::Migration::CommandRecorder

Includes:
ColumnOptionsHandler
Defined in:
lib/schema_plus/active_record/migration/command_recorder.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ColumnOptionsHandler

#schema_plus_handle_column_options, #schema_plus_normalize_column_options

Instance Attribute Details

#schema_plus_configObject

:nodoc:


7
8
9
# File 'lib/schema_plus/active_record/migration/command_recorder.rb', line 7

def schema_plus_config
  @schema_plus_config
end

Class Method Details

.included(base) ⇒ Object

:nodoc:


9
10
11
12
13
14
15
16
# File 'lib/schema_plus/active_record/migration/command_recorder.rb', line 9

def self.included(base) #:nodoc:
  base.class_eval do
    alias_method_chain :add_column, :schema_plus
    alias_method_chain :add_reference, :schema_plus unless ::ActiveRecord::VERSION::MAJOR.to_i < 4
    alias_method_chain :invert_add_index, :schema_plus
    alias_method_chain :invert_add_foreign_key, :schema_plus if "#{::ActiveRecord::VERSION::MAJOR}.#{::ActiveRecord::VERSION::MINOR}".to_r >= "4.2".to_r
  end
end

Instance Method Details

#add_column_with_schema_plus(table_name, name, type, options = {}) ⇒ Object

:nodoc:


18
19
20
21
22
23
# File 'lib/schema_plus/active_record/migration/command_recorder.rb', line 18

def add_column_with_schema_plus(table_name, name, type, options = {}) #:nodoc:
  schema_plus_normalize_column_options(options)
  add_column_without_schema_plus(table_name, name, type, options)
  revertable_schema_plus_handle_column_options(table_name, name, options, :config => schema_plus_config)
  self
end

#add_foreign_key(*args) ⇒ Object


66
67
68
# File 'lib/schema_plus/active_record/migration/command_recorder.rb', line 66

def add_foreign_key(*args)
  record(:add_foreign_key, args)
end

#add_reference_with_schema_plus(table_name, ref_name, options = {}) ⇒ Object

seems like this is fixing a rails bug:

change_table foo, :bulk => true { |t| t.references :bar }

results in an 'unknown method :add_reference_sql' (with mysql2)

should track it down separately and submit a patch/fix to rails


31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/schema_plus/active_record/migration/command_recorder.rb', line 31

def add_reference_with_schema_plus(table_name, ref_name, options = {}) #:nodoc:
  options[:references] = nil if options[:polymorphic]
  # which is the worse hack...?
  if RUBY_VERSION >= "2.0.0" and self.delegate.respond_to? :add_reference_sql
    # .. rebinding a method from a different module?  (can't do this in ruby 1.9.3)
    ::ActiveRecord::ConnectionAdapters::SchemaStatements.instance_method(:add_reference).bind(self).call(table_name, ref_name, options)
  else
    # .. or copying and pasting the code?
    polymorphic = options.delete(:polymorphic)
    index_options = options.delete(:index)
    add_column(table_name, "#{ref_name}_id", :integer, options)
    add_column(table_name, "#{ref_name}_type", :string, polymorphic.is_a?(Hash) ? polymorphic : options) if polymorphic
    add_index(table_name, polymorphic ? %w[id type].map{ |t| "#{ref_name}_#{t}" } : "#{ref_name}_id", index_options.is_a?(Hash) ? index_options : {}) if index_options
  end

  self
end

#invert_add_foreign_key(args) ⇒ Object Also known as: invert_add_foreign_key_with_schema_plus


70
71
72
73
# File 'lib/schema_plus/active_record/migration/command_recorder.rb', line 70

def invert_add_foreign_key(args)
  table_name, column_names, references_table_name, references_column_names, options = args
  [:remove_foreign_key, [table_name, column_names, references_table_name, references_column_names, (options||{}).merge(if_exists: true)]]
end

#invert_add_index_with_schema_plus(args) ⇒ Object


76
77
78
79
# File 'lib/schema_plus/active_record/migration/command_recorder.rb', line 76

def invert_add_index_with_schema_plus(args)
  table, columns, options = *args
  [:remove_index, [table, (options||{}).merge(column: columns, if_exists: true)]]
end

#invert_remove_foreign_key(args) ⇒ Object


85
86
87
# File 'lib/schema_plus/active_record/migration/command_recorder.rb', line 85

def invert_remove_foreign_key(args)
  [:add_foreign_key, args]
end

#remove_foreign_key(*args) ⇒ Object


81
82
83
# File 'lib/schema_plus/active_record/migration/command_recorder.rb', line 81

def remove_foreign_key(*args)
  record(:remove_foreign_key, args)
end