Module: SchemaPlus::ActiveRecord::Migration::CommandRecorder
- Includes:
- ColumnOptionsHandler
- Defined in:
- lib/schema_plus/active_record/migration/command_recorder.rb
Instance Attribute Summary collapse
-
#schema_plus_config ⇒ Object
:nodoc:.
Class Method Summary collapse
-
.included(base) ⇒ Object
:nodoc:.
Instance Method Summary collapse
-
#add_column_with_schema_plus(table_name, name, type, options = {}) ⇒ Object
:nodoc:.
- #add_foreign_key(*args) ⇒ Object
-
#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).
- #invert_add_foreign_key(args) ⇒ Object
- #invert_add_index_with_schema_plus(args) ⇒ Object
- #invert_remove_foreign_key(args) ⇒ Object
- #remove_foreign_key(*args) ⇒ Object
Methods included from ColumnOptionsHandler
#schema_plus_handle_column_options
Instance Attribute Details
#schema_plus_config ⇒ Object
: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 |
# 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 end end |
Instance Method Details
#add_column_with_schema_plus(table_name, name, type, options = {}) ⇒ Object
:nodoc:
17 18 19 20 21 |
# File 'lib/schema_plus/active_record/migration/command_recorder.rb', line 17 def add_column_with_schema_plus(table_name, name, type, = {}) #:nodoc: add_column_without_schema_plus(table_name, name, type, ) (table_name, name, , :config => schema_plus_config) self end |
#add_foreign_key(*args) ⇒ Object
64 65 66 |
# File 'lib/schema_plus/active_record/migration/command_recorder.rb', line 64 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
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/schema_plus/active_record/migration/command_recorder.rb', line 29 def add_reference_with_schema_plus(table_name, ref_name, = {}) #:nodoc: [:references] = nil if [: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, ) else # .. or copying and pasting the code? polymorphic = .delete(:polymorphic) = .delete(:index) add_column(table_name, "#{ref_name}_id", :integer, ) add_column(table_name, "#{ref_name}_type", :string, polymorphic.is_a?(Hash) ? polymorphic : ) if polymorphic add_index(table_name, polymorphic ? %w[id type].map{ |t| "#{ref_name}_#{t}" } : "#{ref_name}_id", .is_a?(Hash) ? : nil) if end self end |
#invert_add_foreign_key(args) ⇒ Object
68 69 70 71 |
# File 'lib/schema_plus/active_record/migration/command_recorder.rb', line 68 def invert_add_foreign_key(args) table_name, column_names, references_table_name, references_column_names, = args [:remove_foreign_key, [table_name, column_names, references_table_name, references_column_names, (||{}).merge(if_exists: true)]] end |
#invert_add_index_with_schema_plus(args) ⇒ Object
73 74 75 76 |
# File 'lib/schema_plus/active_record/migration/command_recorder.rb', line 73 def invert_add_index_with_schema_plus(args) table, columns, = *args [:remove_index, [table, (||{}).merge(column: columns, if_exists: true)]] end |
#invert_remove_foreign_key(args) ⇒ Object
82 83 84 |
# File 'lib/schema_plus/active_record/migration/command_recorder.rb', line 82 def invert_remove_foreign_key(args) [:add_foreign_key, args] end |
#remove_foreign_key(*args) ⇒ Object
78 79 80 |
# File 'lib/schema_plus/active_record/migration/command_recorder.rb', line 78 def remove_foreign_key(*args) record(:remove_foreign_key, args) end |