Module: SchemaPlus::ActiveRecord::ConnectionAdapters::SchemaStatements
- Defined in:
- lib/schema_plus/active_record/connection_adapters/schema_statements.rb
Defined Under Namespace
Modules: AddIndex
Class Method Summary collapse
-
.add_index_exception_handler(connection, table, columns, options, e) ⇒ Object
:nodoc:.
-
.included(base) ⇒ Object
:nodoc:.
Instance Method Summary collapse
- #add_index_options_with_schema_plus(table_name, column_name, options = {}) ⇒ Object
-
#add_reference_with_schema_plus(table_name, ref_name, options = {}) ⇒ Object
:nodoc:.
-
#create_table_with_schema_plus(table, options = {}) ⇒ Object
:method: create_table.
Class Method Details
.add_index_exception_handler(connection, table, columns, options, e) ⇒ Object
:nodoc:
61 62 63 64 65 66 67 68 |
# File 'lib/schema_plus/active_record/connection_adapters/schema_statements.rb', line 61 def self.add_index_exception_handler(connection, table, columns, , e) #:nodoc: raise unless e..match(/["']([^"']+)["'].*already exists/) name = $1 existing = connection.indexes(table).find{|i| i.name == name} attempted = ::ActiveRecord::ConnectionAdapters::IndexDefinition.new(table, columns, .merge(:name => name)) raise if attempted != existing ::ActiveRecord::Base.logger.warn "[schema_plus] Index name #{name.inspect}' on table #{table.inspect} already exists. Skipping." end |
.included(base) ⇒ Object
:nodoc:
4 5 6 7 8 9 10 11 |
# File 'lib/schema_plus/active_record/connection_adapters/schema_statements.rb', line 4 def self.included(base) #:nodoc: base.class_eval do alias_method_chain :create_table, :schema_plus alias_method_chain :add_reference, :schema_plus unless ::ActiveRecord::VERSION::MAJOR.to_i < 4 alias_method_chain :add_index_options, :schema_plus if "#{::ActiveRecord::VERSION::MAJOR}.#{::ActiveRecord::VERSION::MINOR}".to_r >= "4.2".to_r include AddIndex end end |
Instance Method Details
#add_index_options_with_schema_plus(table_name, column_name, options = {}) ⇒ Object
56 57 58 59 |
# File 'lib/schema_plus/active_record/connection_adapters/schema_statements.rb', line 56 def (table_name, column_name, = {}) columns = .delete(:with) { |_| [] } (table_name, Array(column_name).concat(Array(columns).map(&:to_s)), ) end |
#add_reference_with_schema_plus(table_name, ref_name, options = {}) ⇒ Object
:nodoc:
13 14 15 16 17 |
# File 'lib/schema_plus/active_record/connection_adapters/schema_statements.rb', line 13 def add_reference_with_schema_plus(table_name, ref_name, = {}) #:nodoc: [:references] = nil if [:polymorphic] [:_index] = .delete(:index) unless [:polymorphic] # usurp index creation from AR add_reference_without_schema_plus(table_name, ref_name, ) end |
#create_table_with_schema_plus(table, options = {}) ⇒ Object
:method: create_table
SchemaPlus extends SchemaStatements::create_table to allow you to specify configuration options per table. Pass them in as a hash keyed by configuration set (see SchemaPlus::Config), for example:
create_table :widgets, :foreign_keys => {:auto_create => true, :on_delete => :cascade} do |t|
...
end
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 |
# File 'lib/schema_plus/active_record/connection_adapters/schema_statements.rb', line 28 def create_table_with_schema_plus(table, = {}) = .dup = {} .keys.each { |key| [key] = .delete(key) if SchemaPlus.config.class.attributes.include? key } # override rails' :force to cascade drop_table(table, if_exists: true, cascade: true) if .delete(:force) if ::ActiveRecord::VERSION::MAJOR.to_i < 4 indexes = [] end create_table_without_schema_plus(table, ) do |table_definition| table_definition.schema_plus_config = SchemaPlus.config.merge() if ::ActiveRecord::VERSION::MAJOR.to_i < 4 table_definition.name = table end yield table_definition if block_given? if ::ActiveRecord::VERSION::MAJOR.to_i < 4 indexes = table_definition.indexes end end if ::ActiveRecord::VERSION::MAJOR.to_i < 4 indexes.each do |index| add_index(table, index.columns, index.opts) end end end |