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
-
#_build_foreign_key(from_table, to_table, options = {}) ⇒ Object
:nodoc:.
-
#add_foreign_key(from_table, to_table, **options) ⇒ Object
Define a foreign key constraint.
-
#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 { … }.
-
#foreign_keys(table_name, name = nil) ⇒ Object
(abstract) Return the ForeignKeyDefinition objects for foreign key constraints defined on this table.
- #get_foreign_key_name(from_table, to_table, options) ⇒ Object
-
#remove_foreign_key(from_table, to_table = nil, **options) ⇒ Object
Remove a foreign key constraint.
- #remove_foreign_key_sql(from_table, to_table, options) ⇒ Object
-
#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.
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, = {}) #:nodoc: ::ActiveRecord::ConnectionAdapters::ForeignKeyDefinition.new(from_table, AbstractAdapter.proper_table_name(to_table), ) 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, **) # (table_name, column, to_table, primary_key, options = {}) = .dup [:column] ||= foreign_key_column_for(to_table) foreign_key_sql = add_foreign_key_sql(from_table, to_table, ) 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, = {}) #:nodoc: foreign_key = ::ActiveRecord::ConnectionAdapters::ForeignKeyDefinition.new(from_table, AbstractAdapter.proper_table_name(to_table), ) "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, ) return [:name] if [: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, ) 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, ].inspect}" unless [: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,
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, **) [:column] ||= foreign_key_column_for(to_table) if sql = remove_foreign_key_sql(from_table, to_table, ) 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, ) if foreign_key_name = get_foreign_key_name(from_table, to_table, ) "DROP CONSTRAINT #{[: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 |