Class: YeetDba::MissingForeignKeys

Inherits:
Object
  • Object
show all
Defined in:
lib/yeet_dba/missing_foreign_keys.rb

Class Method Summary collapse

Class Method Details

.configObject



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/yeet_dba/missing_foreign_keys.rb', line 31

def self.config
  @config ||= begin
    config_file = Pathname.new(Rails.root).join('.yeet_dba.yml')

    if File.exist?(config_file)
      YAML.load(File.read(config_file))
    else
      {}
    end
  end
end

.eager_load!Object



19
20
21
# File 'lib/yeet_dba/missing_foreign_keys.rb', line 19

def self.eager_load!
  Rails.application.eager_load! if defined?(Rails) && !Rails.env.test?
end

.foreign_keysObject



3
4
5
6
7
8
9
# File 'lib/yeet_dba/missing_foreign_keys.rb', line 3

def self.foreign_keys
  eager_load!
  tables.map do |table_name|
    Table.new(table_name: table_name,
              tables: tables).missing_keys
  end.flatten
end

.ignored_tablesObject



27
28
29
# File 'lib/yeet_dba/missing_foreign_keys.rb', line 27

def self.ignored_tables
  config['exclude_tables'] || []
end

.invalid_columnsObject



11
12
13
14
15
16
17
# File 'lib/yeet_dba/missing_foreign_keys.rb', line 11

def self.invalid_columns
  eager_load!
  tables.map do |table_name|
    Table.new(table_name: table_name,
              tables: tables).invalid_columns
  end.flatten
end

.tablesObject



23
24
25
# File 'lib/yeet_dba/missing_foreign_keys.rb', line 23

def self.tables
  ActiveRecord::Base.connection.tables - self.ignored_tables
end