Module: SchemaPlus::ActiveRecord::ConnectionAdapters::AbstractAdapter

Defined in:
lib/schema_plus/active_record/connection_adapters/abstract_adapter.rb

Overview

SchemaPlus adds several methods to ActiveRecord::ConnectionAdapters::AbstractAdapter. In most cases you don’t call these directly, but rather the methods that define things are called by schema statements, and methods that query things are called by ActiveRecord::Base.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object

:nodoc:



14
15
16
17
# File 'lib/schema_plus/active_record/connection_adapters/abstract_adapter.rb', line 14

def self.included(base) #:nodoc:
  base.alias_method_chain :initialize, :schema_plus
  base.alias_method_chain :drop_table, :schema_plus
end

Instance Method Details

#add_foreign_key(table_name, column_names, references_table_name, references_column_names, options = {}) ⇒ Object

Define a foreign key constraint. Valid options are :on_update, :on_delete, and :deferrable, with values as described at ForeignKeyDefinition



75
76
77
78
# File 'lib/schema_plus/active_record/connection_adapters/abstract_adapter.rb', line 75

def add_foreign_key(table_name, column_names, references_table_name, references_column_names, options = {})
  foreign_key = ForeignKeyDefinition.new(options[:name], table_name, column_names, ::ActiveRecord::Migrator.proper_table_name(references_table_name), references_column_names, options[:on_update], options[:on_delete], options[:deferrable])
  execute "ALTER TABLE #{quote_table_name(table_name)} ADD #{foreign_key.to_sql}"
end

#create_view(view_name, definition) ⇒ Object

Create a view given the SQL definition



47
48
49
# File 'lib/schema_plus/active_record/connection_adapters/abstract_adapter.rb', line 47

def create_view(view_name, definition)
  execute "CREATE VIEW #{quote_table_name(view_name)} AS #{definition}"
end

#drop_table_with_schema_plus(name, options = {}) ⇒ Object

:nodoc:



85
86
87
88
89
90
# File 'lib/schema_plus/active_record/connection_adapters/abstract_adapter.rb', line 85

def drop_table_with_schema_plus(name, options = {}) #:nodoc:
  unless ::ActiveRecord::Base.connection.class.include?(SchemaPlus::ActiveRecord::ConnectionAdapters::Sqlite3Adapter)
    reverse_foreign_keys(name).each { |foreign_key| remove_foreign_key(foreign_key.table_name, foreign_key.name) }
  end
  drop_table_without_schema_plus(name, options)
end

#drop_view(view_name) ⇒ Object

Drop the named view



52
53
54
# File 'lib/schema_plus/active_record/connection_adapters/abstract_adapter.rb', line 52

def drop_view(view_name)
  execute "DROP VIEW #{quote_table_name(view_name)}"
end

#foreign_keys(table_name, name = nil) ⇒ Object

Return the ForeignKeyDefinition objects for foreign key constraints defined on this table (abstract)



66
# File 'lib/schema_plus/active_record/connection_adapters/abstract_adapter.rb', line 66

def foreign_keys(table_name, name = nil) [] end

#index_name_exists?(table_name, index_name, default) ⇒ Boolean

File activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb, line 403

Returns:

  • (Boolean)


101
102
103
104
105
# File 'lib/schema_plus/active_record/connection_adapters/abstract_adapter.rb', line 101

def index_name_exists?(table_name, index_name, default)
  return default unless respond_to?(:indexes)
  index_name = index_name.to_s
  indexes(table_name).detect { |i| i.name == index_name }
end

#initialize_with_schema_plus(*args) ⇒ Object

:nodoc:



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
# File 'lib/schema_plus/active_record/connection_adapters/abstract_adapter.rb', line 19

def initialize_with_schema_plus(*args) #:nodoc:
  initialize_without_schema_plus(*args)
  adapter = nil
  case adapter_name
    # name of MySQL adapter depends on mysql gem
    # * with mysql gem adapter is named MySQL
    # * with mysql2 gem adapter is named Mysql2
    # Here we handle this and hopefully futher adapter names
  when /^MySQL/i 
    adapter = 'MysqlAdapter'
  when 'PostgreSQL' 
    adapter = 'PostgresqlAdapter'
  when 'SQLite' 
    adapter = 'Sqlite3Adapter'
  end
  if adapter 
    adapter_module = SchemaPlus::ActiveRecord::ConnectionAdapters.const_get(adapter)
    self.class.send(:include, adapter_module) unless self.class.include?(adapter_module)
    self.post_initialize if self.respond_to? :post_initialize
    # rails 3.1 defines a separate Mysql2IndexDefinition which is
    # compatible with the monkey patches; but the definition only
    # appears once the adapter is loaded.  so wait til now to check
    # if that constant exists, then include the patches
    ::ActiveRecord::ConnectionAdapters::Mysql2IndexDefinition.send(:include, SchemaPlus::ActiveRecord::ConnectionAdapters::IndexDefinition) if defined? ::ActiveRecord::ConnectionAdapters::Mysql2IndexDefinition
  end
end

#remove_foreign_key(table_name, foreign_key_name) ⇒ Object

Remove a foreign key constraint



81
82
83
# File 'lib/schema_plus/active_record/connection_adapters/abstract_adapter.rb', line 81

def remove_foreign_key(table_name, foreign_key_name)
  execute "ALTER TABLE #{quote_table_name(table_name)} DROP CONSTRAINT #{foreign_key_name}"
end

#reverse_foreign_keys(table_name, name = nil) ⇒ Object

Return the ForeignKeyDefinition objects for foreign key constraints defined on other tables that reference this table (abstract)



70
# File 'lib/schema_plus/active_record/connection_adapters/abstract_adapter.rb', line 70

def reverse_foreign_keys(table_name, name = nil) [] end

#supports_partial_indexes?Boolean

Returns true if the database supports parital indexes (abstract; only Postgresql returns true)

Returns:

  • (Boolean)


94
95
96
# File 'lib/schema_plus/active_record/connection_adapters/abstract_adapter.rb', line 94

def supports_partial_indexes?
  false
end

#view_definition(view_name, name = nil) ⇒ Object

Returns the SQL definition of a given view (abstract)



63
# File 'lib/schema_plus/active_record/connection_adapters/abstract_adapter.rb', line 63

def view_definition(view_name, name = nil) end

#views(name = nil) ⇒ Object

– these are all expected to be defined by subclasses, listing them here only as templates. ++ Returns a list of all views (abstract)



61
# File 'lib/schema_plus/active_record/connection_adapters/abstract_adapter.rb', line 61

def views(name = nil) [] end