Class: ActiveRecord::ConnectionAdapters::MysqlConstraint

Inherits:
AbstractTableConstraint show all
Defined in:
lib/connection_adapters/mysql_adapter.rb

Overview

A representation of a referential constraint or key in MySQL Specifically: Primary, Foreign, Unique Key definitions

Constant Summary

Constants inherited from AbstractTableConstraint

AbstractTableConstraint::CHECK_CONSTRAINT_TYPE, AbstractTableConstraint::FOREIGN_KEY_TYPE, AbstractTableConstraint::PRIMARY_KEY_TYPE, AbstractTableConstraint::UNIQUE_KEY_TYPE

Instance Attribute Summary

Attributes inherited from AbstractTableConstraint

#column_names, #constraint_name, #constraint_type, #referenced_column_names, #referenced_table_name, #table_name, #table_schema

Instance Method Summary collapse

Methods inherited from AbstractTableConstraint

#foreign_key?, #primary_key?, #raise_subclass_responsibility_error, #unique_key?

Constructor Details

#initialize(constraint_name, table_schema, table_name, column_name, constraint_type, referenced_table_name, referenced_column_name) ⇒ MysqlConstraint

Returns a new instance of MysqlConstraint.



29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/connection_adapters/mysql_adapter.rb', line 29

def initialize(constraint_name, table_schema, table_name, column_name, constraint_type,
  referenced_table_name, referenced_column_name)
  @constraint_name = constraint_name
  @table_schema = table_schema
  @table_name = table_name
		if constraint_type.nil? && table_name != referenced_table_name 
		  @constraint_type = FOREIGN_KEY_TYPE
		else
    @constraint_type = constraint_type
		end
  @column_names = [column_name]
  @referenced_table_name = referenced_table_name
  @referenced_column_names = [referenced_column_name] if referenced_column_name
end

Instance Method Details

#is_foreign_constraint?(table_name) ⇒ Boolean

Returns:

  • (Boolean)


44
45
46
47
48
# File 'lib/connection_adapters/mysql_adapter.rb', line 44

def is_foreign_constraint?(table_name)
  !@referenced_table_name.nil? && 
  @referenced_table_name.upcase == table_name.upcase &&
  @table_name.upcase != table_name.upcase
end