Module: SchemaPlus::ForeignKeys::ActiveRecord::ConnectionAdapters::Mysql2Adapter

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

Overview

SchemaPlus::ForeignKeys includes a MySQL implementation of the AbstractAdapter extensions.

Instance Method Summary collapse

Instance Method Details

#foreign_keys(table_name, name = nil) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/schema_plus/foreign_keys/active_record/connection_adapters/mysql2_adapter.rb', line 32

def foreign_keys(table_name, name = nil)
  results = select_all("SHOW CREATE TABLE #{quote_table_name(table_name)}", name)

  table_name = table_name.to_s
  namespace_prefix = table_namespace_prefix(table_name)

  foreign_keys = []

  results.each do |result|
    create_table_sql = result["Create Table"]
    create_table_sql.lines.each do |line|
      if line =~ /^  CONSTRAINT [`"](.+?)[`"] FOREIGN KEY \([`"](.+?)[`"]\) REFERENCES [`"](.+?)[`"] \((.+?)\)( ON DELETE (.+?))?( ON UPDATE (.+?))?,?$/
        name = $1
        columns = $2
        to_table = $3
        to_table = namespace_prefix + to_table if table_namespace_prefix(to_table).blank?
        primary_keys = $4
        on_update = $8
        on_delete = $6
        on_update = ForeignKeyDefinition::ACTION_LOOKUP[on_update] || :restrict
        on_delete = ForeignKeyDefinition::ACTION_LOOKUP[on_delete] || :restrict

        options = { name: name,
                    on_delete: on_delete,
                    on_update: on_update,
                    column: columns.gsub('`', '').split(', '),
                    primary_key: primary_keys.gsub('`', '').split(', ')
        }

        foreign_keys << ::ActiveRecord::ConnectionAdapters::ForeignKeyDefinition.new(
          namespace_prefix + table_name,
          to_table,
          options)
      end
    end
  end

  foreign_keys
end

#remove_column(table_name, column_name, type = nil, **options) ⇒ Object

:enddoc:



12
13
14
15
16
17
# File 'lib/schema_plus/foreign_keys/active_record/connection_adapters/mysql2_adapter.rb', line 12

def remove_column(table_name, column_name, type=nil, **options)
  foreign_keys(table_name).select { |foreign_key| Array.wrap(foreign_key.column).include?(column_name.to_s) }.each do |foreign_key|
    remove_foreign_key(table_name, name: foreign_key.name)
  end
  super table_name, column_name, type, **options
end

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



19
20
21
22
23
24
25
26
# File 'lib/schema_plus/foreign_keys/active_record/connection_adapters/mysql2_adapter.rb', line 19

def remove_foreign_key(from_table, to_table = nil, **options)
  if options[:if_exists]
    foreign_key_name = get_foreign_key_name(from_table, to_table, options)
    return if !foreign_key_name or not foreign_keys(from_table).detect{|fk| fk.name == foreign_key_name}
  end
  options.delete(:if_exists)
  super from_table, to_table, **options
end

#remove_foreign_key_sql(*args) ⇒ Object



28
29
30
# File 'lib/schema_plus/foreign_keys/active_record/connection_adapters/mysql2_adapter.rb', line 28

def remove_foreign_key_sql(*args)
  super&.sub(/DROP CONSTRAINT/, 'DROP FOREIGN KEY')
end

#reverse_foreign_keys(table_name, name = nil) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/schema_plus/foreign_keys/active_record/connection_adapters/mysql2_adapter.rb', line 72

def reverse_foreign_keys(table_name, name = nil)
  results = select_all(<<-SQL, name)
SELECT constraint_name, table_name, column_name, referenced_table_name, referenced_column_name
  FROM information_schema.key_column_usage
 WHERE table_schema = #{table_schema_sql(table_name)}
   AND referenced_table_schema = table_schema
 ORDER BY constraint_name, ordinal_position;
  SQL

  constraints = results.to_a.group_by do |r|
    r.values_at('constraint_name', 'table_name', 'referenced_table_name')
  end

  from_table_constraints = constraints.select do |(_, _, to_table), _|
    table_name_without_namespace(table_name).casecmp(to_table) == 0
  end

  from_table_constraints.map do |(constraint_name, from_table, to_table), columns|
    from_table = table_namespace_prefix(from_table) + from_table
    to_table = table_namespace_prefix(to_table) + to_table

    options = {
      name: constraint_name,
      column: columns.map { |row| row['column_name'] },
      primary_key: columns.map { |row| row['referenced_column_name'] }
    }

    ::ActiveRecord::ConnectionAdapters::ForeignKeyDefinition.new(from_table, to_table, options)
  end
end