Module: ForeignKeyChecker::Checkers::Tables

Defined in:
lib/foreign_key_checker/checkers/tables.rb

Overview

Стоит использовать в тех случаях, когда подключается rails к уже существующей базе Можно проверить, для каких таблиц всё ещё нет ActiveRecord-моделей

Defined Under Namespace

Classes: Result

Constant Summary collapse

SPECIAL_TABLES =
['schema_migrations', 'ar_internal_metadata'].freeze

Class Method Summary collapse

Class Method Details

.checkObject



66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/foreign_key_checker/checkers/tables.rb', line 66

def self.check
  tables = without_models
  fks = ForeignKeyChecker::Utils.get_foreign_keys_hash
  fks.default = []
  tables.map do |table|
    Result.new(
      table_name: table,
      foreign_keys: fks[table] || [],
      internal_references: (fks[table].map(&:from_table) - tables).empty?,
    )
  end
end

.common_tablesObject



40
41
42
# File 'lib/foreign_key_checker/checkers/tables.rb', line 40

def self.common_tables
  ForeignKeyChecker::Utils.get_tables - SPECIAL_TABLES
end

.modelised_tables(specification_name = 'primary') ⇒ Object



29
30
31
# File 'lib/foreign_key_checker/checkers/tables.rb', line 29

def self.modelised_tables(specification_name = 'primary')
  models(specification_name).map(&:table_name)
end

.models(specification_name = 'primary') ⇒ Object



33
34
35
36
37
# File 'lib/foreign_key_checker/checkers/tables.rb', line 33

def self.models(specification_name = 'primary')
  ActiveRecord::Base.descendants.select do |model|
    model.connection_specification_name == specification_name
  end
end

.ordered(results = check) ⇒ Object

TODO вспомнить, что я тут задумал



80
81
82
83
84
85
86
# File 'lib/foreign_key_checker/checkers/tables.rb', line 80

def self.ordered(results = check)
  return results if results.size == 0
  oks = results.select(&:ok?)
  if oks.size == 0
    raise "no ok"
  end
end

.without_foreign_keys(specification_name = 'primary') ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/foreign_key_checker/checkers/tables.rb', line 15

def self.without_foreign_keys(specification_name = 'primary')
  all_fks = ForeignKeyChecker::Utils.get_foreign_keys
  results = []
  models(specification_name).each do |model|
    model.column_names.each do |column_name|
      next unless column_name.ends_with?('_id')
      next if all_fks.find { |fk| fk.from_table = model.table_name && fk.from_column == column_name }

      table_name = column_name.delete_suffix('_id')
      results << ["#{table_name}.#{column_name}"]
    end
  end
end

.without_models(specification_name = 'primary') ⇒ Object

Список таблиц, не описанных моделями (в том числе HABTM-моделями, сгенерированными автоматически rails)



8
9
10
11
12
13
# File 'lib/foreign_key_checker/checkers/tables.rb', line 8

def self.without_models(specification_name = 'primary')
  Rails.application.eager_load!

  actual_tables = ForeignKeyChecker::Utils.get_tables
  actual_tables - modelised_tables(specification_name) - SPECIAL_TABLES
end