Module: ForeignKeyChecker::Utils
- Defined in:
- lib/foreign_key_checker/utils.rb
Defined Under Namespace
Classes: Result, UnsupportedConnectionAdapter
Class Method Summary
collapse
Class Method Details
.get_foreign_keys(model = ActiveRecord::Base) ⇒ Object
10
11
12
13
14
15
16
|
# File 'lib/foreign_key_checker/utils.rb', line 10
def self.get_foreign_keys(model = ActiveRecord::Base)
adapter = model.connection_config[:adapter]
raise(UnsupportedConnectionAdapter, adapter) unless %w[postgresql mysql2].include?(adapter)
connection = model.connection
send("get_#{adapter}_foreign_keys", connection)
end
|
.get_foreign_keys_hash(model = ActiveRecord::Base) ⇒ Object
18
19
20
21
22
23
|
# File 'lib/foreign_key_checker/utils.rb', line 18
def self.get_foreign_keys_hash(model = ActiveRecord::Base)
get_foreign_keys(model).to_a.each_with_object({}) do |datum, obj|
obj[datum.to_table] ||= []
obj[datum.to_table].push(datum)
end
end
|
.get_mysql2_foreign_keys(connection) ⇒ Object
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
# File 'lib/foreign_key_checker/utils.rb', line 25
def self.get_mysql2_foreign_keys(connection)
res = connection.select_all " SELECT\n fks.TABLE_NAME AS from_table,\n fks.COLUMN_NAME AS from_column,\n fks.REFERENCED_TABLE_NAME AS to_table,\n fks.REFERENCED_COLUMN_NAME AS to_column\n FROM information_schema.KEY_COLUMN_USAGE AS fks\n INNER JOIN information_schema.REFERENTIAL_CONSTRAINTS rules ON rules.CONSTRAINT_NAME = fks.CONSTRAINT_NAME\n WHERE\n fks.CONSTRAINT_SCHEMA = DATABASE()\n AND rules.CONSTRAINT_SCHEMA = DATABASE();\n SQL\n res.to_a.map{|i| Result.new(i) }\nend\n"
|
.get_postgresql_foreign_keys(connection) ⇒ Object
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
|
# File 'lib/foreign_key_checker/utils.rb', line 41
def self.get_postgresql_foreign_keys(connection)
res = connection.select_all " SELECT\n tc.table_name AS from_table,\n kcu.column_name AS from_column,\n ccu.table_name AS to_table,\n ccu.column_name AS to_column\n FROM\n information_schema.table_constraints AS tc\n JOIN information_schema.key_column_usage AS kcu\n ON tc.constraint_name = kcu.constraint_name\n AND tc.table_schema = kcu.table_schema\n JOIN information_schema.constraint_column_usage AS ccu\n ON ccu.constraint_name = tc.constraint_name\n AND ccu.table_schema = tc.table_schema\n WHERE tc.constraint_type = 'FOREIGN KEY';\n SQL\n res.to_a.map{ |i| Result.new(i) }\nend\n"
|