Class: DeclareSchema::Model::ForeignKeyDefinition
- Inherits:
-
Object
- Object
- DeclareSchema::Model::ForeignKeyDefinition
- Includes:
- Comparable
- Defined in:
- lib/declare_schema/model/foreign_key_definition.rb
Instance Attribute Summary collapse
-
#child_table_name ⇒ Object
readonly
Returns the value of attribute child_table_name.
-
#constraint_name ⇒ Object
readonly
Returns the value of attribute constraint_name.
-
#dependent ⇒ Object
readonly
Returns the value of attribute dependent.
-
#foreign_key_column ⇒ Object
readonly
Returns the value of attribute foreign_key_column.
-
#parent_table_name ⇒ Object
readonly
Returns the value of attribute parent_table_name.
Class Method Summary collapse
Instance Method Summary collapse
- #<=>(rhs) ⇒ Object
- #equivalent?(rhs) ⇒ Boolean
-
#initialize(foreign_key_column, constraint_name: nil, child_table: nil, parent_table: nil, class_name: nil, dependent: nil) ⇒ ForeignKeyDefinition
constructor
Caller needs to pass either constraint_name or child_table.
- #key ⇒ Object
Constructor Details
#initialize(foreign_key_column, constraint_name: nil, child_table: nil, parent_table: nil, class_name: nil, dependent: nil) ⇒ ForeignKeyDefinition
Caller needs to pass either constraint_name or child_table. The child_table is remembered, but it is not part of the key; it is just used to compute the default constraint_name if no constraint_name is given.
14 15 16 17 18 19 20 21 |
# File 'lib/declare_schema/model/foreign_key_definition.rb', line 14 def initialize(foreign_key_column, constraint_name: nil, child_table: nil, parent_table: nil, class_name: nil, dependent: nil) @foreign_key_column = foreign_key_column&.to_s or raise ArgumentError "foreign key must not be empty: #{foreign_key_column.inspect}" @constraint_name = constraint_name&.to_s.presence || ::DeclareSchema::Model::IndexDefinition.default_index_name(child_table, [@foreign_key_column]) @child_table_name = child_table&.to_s or raise ArgumentError, "child_table must not be nil" @parent_table_name = parent_table&.to_s || infer_parent_table_name_from_class(class_name) || infer_parent_table_name_from_foreign_key_column(@foreign_key_column) dependent.in?([nil, :delete]) or raise ArgumentError, "dependent: must be nil or :delete" @dependent = dependent end |
Instance Attribute Details
#child_table_name ⇒ Object (readonly)
Returns the value of attribute child_table_name.
10 11 12 |
# File 'lib/declare_schema/model/foreign_key_definition.rb', line 10 def child_table_name @child_table_name end |
#constraint_name ⇒ Object (readonly)
Returns the value of attribute constraint_name.
10 11 12 |
# File 'lib/declare_schema/model/foreign_key_definition.rb', line 10 def constraint_name @constraint_name end |
#dependent ⇒ Object (readonly)
Returns the value of attribute dependent.
10 11 12 |
# File 'lib/declare_schema/model/foreign_key_definition.rb', line 10 def dependent @dependent end |
#foreign_key_column ⇒ Object (readonly)
Returns the value of attribute foreign_key_column.
10 11 12 |
# File 'lib/declare_schema/model/foreign_key_definition.rb', line 10 def foreign_key_column @foreign_key_column end |
#parent_table_name ⇒ Object (readonly)
Returns the value of attribute parent_table_name.
10 11 12 |
# File 'lib/declare_schema/model/foreign_key_definition.rb', line 10 def parent_table_name @parent_table_name end |
Class Method Details
.for_table(table_name, connection, dependent: nil) ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/declare_schema/model/foreign_key_definition.rb', line 24 def for_table(table_name, connection, dependent: nil) show_create_table = connection.select_rows("show create table #{connection.quote_table_name(table_name)}").first.last constraints = show_create_table.split("\n").map { |line| line.strip if line['CONSTRAINT'] }.compact constraints.map do |fkc| constraint_name, foreign_key_column, parent_table = fkc.match(/CONSTRAINT `([^`]*)` FOREIGN KEY \(`([^`]*)`\) REFERENCES `([^`]*)`/).captures dependent_value = :delete if dependent || fkc['ON DELETE CASCADE'] new(foreign_key_column, constraint_name: constraint_name, child_table: table_name, parent_table: parent_table, dependent: dependent_value) end end |
Instance Method Details
#<=>(rhs) ⇒ Object
41 42 43 |
# File 'lib/declare_schema/model/foreign_key_definition.rb', line 41 def <=>(rhs) key <=> rhs.key end |
#equivalent?(rhs) ⇒ Boolean
47 48 49 |
# File 'lib/declare_schema/model/foreign_key_definition.rb', line 47 def equivalent?(rhs) self == rhs end |
#key ⇒ Object
37 38 39 |
# File 'lib/declare_schema/model/foreign_key_definition.rb', line 37 def key @key ||= [@parent_table_name, @foreign_key_column, @dependent].freeze end |