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

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

Overview

SchemaPlus::ForeignKeys 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

.proper_table_name(name) ⇒ Object



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

def self.proper_table_name(name)
  ::ActiveRecord::Migration.new.proper_table_name(name)
end

Instance Method Details

#_build_foreign_key(from_table, to_table, options = {}) ⇒ Object

:nodoc:



42
43
44
# File 'lib/schema_plus/foreign_keys/active_record/connection_adapters/abstract_adapter.rb', line 42

def _build_foreign_key(from_table, to_table, options = {}) #:nodoc:
  ::ActiveRecord::ConnectionAdapters::ForeignKeyDefinition.new(from_table, AbstractAdapter.proper_table_name(to_table), options)
end

#add_foreign_key(from_table, to_table, **options) ⇒ Object

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

(NOTE: Sqlite3 does not support altering a table to add foreign-key constraints; they must be included in the table specification when it’s created. If you’re using Sqlite3, this method will raise an error.)



27
28
29
30
31
32
33
# File 'lib/schema_plus/foreign_keys/active_record/connection_adapters/abstract_adapter.rb', line 27

def add_foreign_key(from_table, to_table, **options) # (table_name, column, to_table, primary_key, options = {})
  options = options.dup
  options[:column] ||= foreign_key_column_for(to_table)

  foreign_key_sql = add_foreign_key_sql(from_table, to_table, options)
  execute "ALTER TABLE #{quote_table_name(from_table)} #{foreign_key_sql}"
end

#add_foreign_key_sql(from_table, to_table, options = {}) ⇒ Object

called directly by AT’s bulk_change_table, for migration change_table :name, bulk: true { … }



37
38
39
40
# File 'lib/schema_plus/foreign_keys/active_record/connection_adapters/abstract_adapter.rb', line 37

def add_foreign_key_sql(from_table, to_table, options = {}) #:nodoc:
  foreign_key = ::ActiveRecord::ConnectionAdapters::ForeignKeyDefinition.new(from_table, AbstractAdapter.proper_table_name(to_table), options)
  "ADD #{foreign_key.to_sql}"
end

#foreign_keys(table_name, name = nil) ⇒ Object

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



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

def foreign_keys(table_name, name = nil) raise "Internal Error: Connection adapter didn't override abstract function"; [] end

#get_foreign_key_name(from_table, to_table, options) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/schema_plus/foreign_keys/active_record/connection_adapters/abstract_adapter.rb', line 67

def get_foreign_key_name(from_table, to_table, options)
  return options[:name] if options[:name]

  fks = foreign_keys(from_table)
  if fks.detect { |it| it.name == to_table }
    ActiveSupport::Deprecation.warn "remove_foreign_key(table, name) is deprecated.  use remove_foreign_key(table, name: name)"
    return to_table
  end
  test_fk = _build_foreign_key(from_table, to_table, options)
  if fk = fks.detect { |fk| fk.match(test_fk) }
    fk.name
  else
    raise "SchemaPlus::ForeignKeys: no foreign key constraint found on #{from_table.inspect} matching #{[to_table, options].inspect}" unless options[:if_exists]
    nil
  end
end

#remove_foreign_key(from_table, to_table = nil, **options) ⇒ Object

Remove a foreign key constraint

Arguments are the same as for add_foreign_key, or by name:

remove_foreign_key table_name, to_table, options
remove_foreign_key table_name, name: constraint_name

(NOTE: Sqlite3 does not support altering a table to remove foreign-key constraints. If you’re using Sqlite3, this method will raise an error.)



60
61
62
63
64
65
# File 'lib/schema_plus/foreign_keys/active_record/connection_adapters/abstract_adapter.rb', line 60

def remove_foreign_key(from_table, to_table = nil, **options)
  options[:column] ||= foreign_key_column_for(to_table)
  if sql = remove_foreign_key_sql(from_table, to_table, options)
    execute "ALTER TABLE #{quote_table_name(from_table)} #{sql}"
  end
end

#remove_foreign_key_sql(from_table, to_table, options) ⇒ Object



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

def remove_foreign_key_sql(from_table, to_table, options)
  if foreign_key_name = get_foreign_key_name(from_table, to_table, options)
    "DROP CONSTRAINT #{options[:if_exists] ? "IF EXISTS" : ""} #{foreign_key_name}"
  end
end

#reverse_foreign_keys(table_name, name = nil) ⇒ Object

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



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

def reverse_foreign_keys(table_name, name = nil) raise "Internal Error: Connection adapter didn't override abstract function"; [] end