Class: SchemaPlus::ActiveRecord::ConnectionAdapters::ForeignKeyDefinition
- Inherits:
-
Object
- Object
- SchemaPlus::ActiveRecord::ConnectionAdapters::ForeignKeyDefinition
- Defined in:
- lib/schema_plus/active_record/connection_adapters/foreign_key_definition.rb
Overview
Instances of this class are returned by the queries ActiveRecord::Base#foreign_keys and ActiveRecord::Base#reverse_foreign_keys (via AbstractAdapter#foreign_keys and AbstractAdapter#reverse_foreign_keys)
The on_update and on_delete attributes can take on the following values:
:cascade
:restrict
:set_null
:set_default
:no_action
The deferrable attribute can take on the following values:
true
:initially_deferred
Constant Summary collapse
- ACTIONS =
:enddoc:
{ :cascade => "CASCADE", :restrict => "RESTRICT", :set_null => "SET NULL", :set_default => "SET DEFAULT", :no_action => "NO ACTION" }.freeze
Instance Attribute Summary collapse
-
#column_names ⇒ Object
readonly
The list of column names that are constrained (as strings).
-
#deferrable ⇒ Object
readonly
True if the constraint is deferrable.
-
#name ⇒ Object
readonly
The name of the foreign key constraint.
-
#on_delete ⇒ Object
readonly
The ON_DELETE behavior for the constraint.
-
#on_update ⇒ Object
readonly
The ON_UPDATE behavior for the constraint.
-
#references_column_names ⇒ Object
readonly
The list of column names (as strings) of the foreign table that are referenced by the constraint.
-
#references_table_name ⇒ Object
readonly
The foreign table that is referenced by the constraint.
-
#table_name ⇒ Object
readonly
The name of the table the constraint is defined on.
Class Method Summary collapse
- .auto_index_name(table_name, column_name) ⇒ Object
- .default_name(table_name, column_names) ⇒ Object
- .fixup_schema_name(table_name) ⇒ Object
Instance Method Summary collapse
-
#==(other) ⇒ Object
note equality test ignores :name and options.
- #__unquote(value) ⇒ Object
-
#initialize(name, table_name, column_names, references_table_name, references_column_names, on_update = nil, on_delete = nil, deferrable = nil) ⇒ ForeignKeyDefinition
constructor
A new instance of ForeignKeyDefinition.
- #quoted_column_names ⇒ Object
- #quoted_references_column_names ⇒ Object
- #quoted_references_table_name ⇒ Object
-
#to_dump(opts = {}) ⇒ Object
Dumps a definition of foreign key.
- #to_sql ⇒ Object
- #unquote(names) ⇒ Object
Constructor Details
#initialize(name, table_name, column_names, references_table_name, references_column_names, on_update = nil, on_delete = nil, deferrable = nil) ⇒ ForeignKeyDefinition
Returns a new instance of ForeignKeyDefinition.
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/schema_plus/active_record/connection_adapters/foreign_key_definition.rb', line 49 def initialize(name, table_name, column_names, references_table_name, references_column_names, on_update = nil, on_delete = nil, deferrable = nil) @name = name @table_name = unquote(table_name) @column_names = unquote(Array.wrap(column_names)) @references_table_name = unquote(references_table_name) @references_column_names = unquote(Array.wrap(references_column_names)) @on_update = on_update @on_delete = on_delete @deferrable = deferrable ACTIONS.has_key?(on_update) or raise(ArgumentError, "invalid :on_update action: #{on_update.inspect}") if on_update ACTIONS.has_key?(on_delete) or raise(ArgumentError, "invalid :on_delete action: #{on_delete.inspect}") if on_delete if ::ActiveRecord::Base.connection.adapter_name =~ /^mysql/i raise(NotImplementedError, "MySQL does not support ON UPDATE SET DEFAULT") if on_update == :set_default raise(NotImplementedError, "MySQL does not support ON DELETE SET DEFAULT") if on_delete == :set_default end end |
Instance Attribute Details
#column_names ⇒ Object (readonly)
The list of column names that are constrained (as strings).
25 26 27 |
# File 'lib/schema_plus/active_record/connection_adapters/foreign_key_definition.rb', line 25 def column_names @column_names end |
#deferrable ⇒ Object (readonly)
True if the constraint is deferrable
43 44 45 |
# File 'lib/schema_plus/active_record/connection_adapters/foreign_key_definition.rb', line 43 def deferrable @deferrable end |
#name ⇒ Object (readonly)
The name of the foreign key constraint
19 20 21 |
# File 'lib/schema_plus/active_record/connection_adapters/foreign_key_definition.rb', line 19 def name @name end |
#on_delete ⇒ Object (readonly)
The ON_DELETE behavior for the constraint. See above for the possible values.
40 41 42 |
# File 'lib/schema_plus/active_record/connection_adapters/foreign_key_definition.rb', line 40 def on_delete @on_delete end |
#on_update ⇒ Object (readonly)
The ON_UPDATE behavior for the constraint. See above for the possible values.
36 37 38 |
# File 'lib/schema_plus/active_record/connection_adapters/foreign_key_definition.rb', line 36 def on_update @on_update end |
#references_column_names ⇒ Object (readonly)
The list of column names (as strings) of the foreign table that are referenced by the constraint
32 33 34 |
# File 'lib/schema_plus/active_record/connection_adapters/foreign_key_definition.rb', line 32 def references_column_names @references_column_names end |
#references_table_name ⇒ Object (readonly)
The foreign table that is referenced by the constraint
28 29 30 |
# File 'lib/schema_plus/active_record/connection_adapters/foreign_key_definition.rb', line 28 def references_table_name @references_table_name end |
#table_name ⇒ Object (readonly)
The name of the table the constraint is defined on
22 23 24 |
# File 'lib/schema_plus/active_record/connection_adapters/foreign_key_definition.rb', line 22 def table_name @table_name end |
Class Method Details
.auto_index_name(table_name, column_name) ⇒ Object
118 119 120 |
# File 'lib/schema_plus/active_record/connection_adapters/foreign_key_definition.rb', line 118 def self.auto_index_name(table_name, column_name) "fk__#{fixup_schema_name(table_name)}_#{Array.wrap(column_name).join('_and_')}" end |
.default_name(table_name, column_names) ⇒ Object
114 115 116 |
# File 'lib/schema_plus/active_record/connection_adapters/foreign_key_definition.rb', line 114 def self.default_name(table_name, column_names) "fk_#{fixup_schema_name(table_name)}_#{Array.wrap(column_names).join('_and_')}" end |
.fixup_schema_name(table_name) ⇒ Object
122 123 124 125 |
# File 'lib/schema_plus/active_record/connection_adapters/foreign_key_definition.rb', line 122 def self.fixup_schema_name(table_name) # replace . with _ table_name.to_s.gsub(/[.]/, '_') end |
Instance Method Details
#==(other) ⇒ Object
note equality test ignores :name and options
127 128 129 130 131 132 133 |
# File 'lib/schema_plus/active_record/connection_adapters/foreign_key_definition.rb', line 127 def ==(other) # note equality test ignores :name and options [:table_name, :column_names, :references_table_name, :references_column_names ].all? { |attr| self.send(attr) == other.send(attr) } end |
#__unquote(value) ⇒ Object
110 111 112 |
# File 'lib/schema_plus/active_record/connection_adapters/foreign_key_definition.rb', line 110 def __unquote(value) value.to_s.sub(/^["`](.*)["`]$/, '\1') end |
#quoted_column_names ⇒ Object
90 91 92 |
# File 'lib/schema_plus/active_record/connection_adapters/foreign_key_definition.rb', line 90 def quoted_column_names Array(column_names).collect { |name| ::ActiveRecord::Base.connection.quote_column_name(name) } end |
#quoted_references_column_names ⇒ Object
94 95 96 |
# File 'lib/schema_plus/active_record/connection_adapters/foreign_key_definition.rb', line 94 def quoted_references_column_names Array(references_column_names).collect { |name| ::ActiveRecord::Base.connection.quote_column_name(name) } end |
#quoted_references_table_name ⇒ Object
98 99 100 |
# File 'lib/schema_plus/active_record/connection_adapters/foreign_key_definition.rb', line 98 def quoted_references_table_name ::ActiveRecord::Base.connection.quote_table_name(references_table_name) end |
#to_dump(opts = {}) ⇒ Object
Dumps a definition of foreign key.
68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/schema_plus/active_record/connection_adapters/foreign_key_definition.rb', line 68 def to_dump(opts={}) dump = (opts[:inline] ? " t.foreign_key" : "add_foreign_key #{table_name.inspect},") dump << " [#{Array(column_names).collect{ |name| name.inspect }.join(', ')}]" dump << ", #{references_table_name.inspect}, [#{Array(references_column_names).collect{ |name| name.inspect }.join(', ')}]" dump << ", :on_update => #{on_update.inspect}" if on_update dump << ", :on_delete => #{on_delete.inspect}" if on_delete dump << ", :deferrable => #{deferrable.inspect}" if deferrable dump << ", :name => #{name.inspect}" if name dump << "\n" dump end |
#to_sql ⇒ Object
80 81 82 83 84 85 86 87 88 |
# File 'lib/schema_plus/active_record/connection_adapters/foreign_key_definition.rb', line 80 def to_sql sql = name ? "CONSTRAINT #{name} " : "" sql << "FOREIGN KEY (#{quoted_column_names.join(", ")}) REFERENCES #{quoted_references_table_name} (#{quoted_references_column_names.join(", ")})" sql << " ON UPDATE #{ACTIONS[on_update]}" if on_update sql << " ON DELETE #{ACTIONS[on_delete]}" if on_delete sql << " DEFERRABLE" if deferrable sql << " INITIALLY DEFERRED" if deferrable == :initially_deferred sql end |
#unquote(names) ⇒ Object
102 103 104 105 106 107 108 |
# File 'lib/schema_plus/active_record/connection_adapters/foreign_key_definition.rb', line 102 def unquote(names) if names.is_a?(Array) names.collect { |name| __unquote(name) } else __unquote(names) end end |