Module: RedhillonrailsCore::ActiveRecord::ConnectionAdapters::MysqlAdapter

Includes:
MysqlAdapterFixes
Defined in:
lib/redhillonrails_core/active_record/connection_adapters/mysql_adapter.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



7
8
9
10
11
12
# File 'lib/redhillonrails_core/active_record/connection_adapters/mysql_adapter.rb', line 7

def self.included(base)
  super
  base.class_eval do
    alias_method_chain :remove_column, :redhillonrails_core
  end
end

Instance Method Details

#foreign_keys(table_name, name = nil) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/redhillonrails_core/active_record/connection_adapters/mysql_adapter.rb', line 25

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

  foreign_keys = []

  results.each do |row|
    row[1].lines.each do |line|
      if line =~ /^  CONSTRAINT [`"](.+?)[`"] FOREIGN KEY \([`"](.+?)[`"]\) REFERENCES [`"](.+?)[`"] \((.+?)\)( ON DELETE (.+?))?( ON UPDATE (.+?))?,?$/
        name = $1
        column_names = $2
        references_table_name = $3
        references_column_names = $4
        on_update = $8
        on_delete = $6
        on_update = on_update.downcase.gsub(' ', '_').to_sym if on_update
        on_delete = on_delete.downcase.gsub(' ', '_').to_sym if on_delete

        foreign_keys << ForeignKeyDefinition.new(name,
                                                 table_name, column_names.gsub('`', '').split(', '),
                                                 references_table_name, references_column_names.gsub('`', '').split(', '),
                                                 on_update, on_delete)
      end
    end
  end

  foreign_keys
end

#remove_column_with_redhillonrails_core(table_name, column_name) ⇒ Object



18
19
20
21
22
23
# File 'lib/redhillonrails_core/active_record/connection_adapters/mysql_adapter.rb', line 18

def remove_column_with_redhillonrails_core(table_name, column_name)
  foreign_keys(table_name).select { |foreign_key| foreign_key.column_names.include?(column_name.to_s) }.each do |foreign_key|
    remove_foreign_key(table_name, foreign_key.name)
  end
  remove_column_without_redhillonrails_core(table_name, column_name)
end

#remove_foreign_key(table_name, foreign_key_name, options = {}) ⇒ Object



14
15
16
# File 'lib/redhillonrails_core/active_record/connection_adapters/mysql_adapter.rb', line 14

def remove_foreign_key(table_name, foreign_key_name, options = {})
  execute "ALTER TABLE #{quote_table_name(table_name)} DROP FOREIGN KEY #{foreign_key_name}"
end

#reverse_foreign_keys(table_name, name = nil) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/redhillonrails_core/active_record/connection_adapters/mysql_adapter.rb', line 53

def reverse_foreign_keys(table_name, name = nil)
  results = execute(<<-SQL, name)
SELECT constraint_name, table_name, column_name, referenced_table_name, referenced_column_name
  FROM information_schema.key_column_usage
 WHERE table_schema = SCHEMA()
   AND referenced_table_schema = table_schema
 ORDER BY constraint_name, ordinal_position;
  SQL
  current_foreign_key = nil
  foreign_keys = []

  results.each do |row|
    next unless table_name.casecmp(row[3]) == 0
    if current_foreign_key != row[0]
      foreign_keys << ForeignKeyDefinition.new(row[0], row[1], [], row[3], [])
      current_foreign_key = row[0]
    end

    foreign_keys.last.column_names << row[2]
    foreign_keys.last.references_column_names << row[4]
  end

  foreign_keys
end

#view_definition(view_name, name = nil) ⇒ Object



84
85
86
# File 'lib/redhillonrails_core/active_record/connection_adapters/mysql_adapter.rb', line 84

def view_definition(view_name, name = nil)
  select_one("SHOW CREATE VIEW #{quote_table_name(view_name)}", name)["Create View"]
end

#views(name = nil) ⇒ Object



78
79
80
81
82
# File 'lib/redhillonrails_core/active_record/connection_adapters/mysql_adapter.rb', line 78

def views(name = nil)
  views = []
  execute("SHOW FULL TABLES WHERE TABLE_TYPE='VIEW'", name).each { |row| views << row[0] }
  views
end